Java NPE

Some stuffs related to NPE Refer1 Refer2 1. Optional This class will be an container object which may or may not contain a non-null value Examples: // Potential dangers of null String version = computer.getSoundcard().getUSB().getVersion(); // Solution 1: Ugly due to the nested checks, decreasing the readability String version = "UNKNOWN"; if(computer != null){ Soundcard soundcard = computer.getSoundcard(); if(soundcard != null){ USB usb = soundcard.getUSB(); if(usb != null){ version = usb.getVersion(); } } } // Solution 2(JavaSE7): String version = computer?.getSoundcard()?.getUSB()?.getVersion(); String version = computer?.getSoundcard()?.getUSB()?.getVersion() ?: "UNKNOWN"; // Solution 3 (JavaSE8) public class Computer { private Optional<Soundcard> soundcard; public Optional<Soundcard> getSoundcard() { ... } ... } public class Soundcard { private Optional<USB> usb; public Optional<USB> getUSB() { ... } } public class USB{ public String getVersion(){ ... } } String name = computer.flatMap(Computer::getSoundcard) .flatMap(Soundcard::getUSB) .map(USB::getVersion) .orElse("UNKNOWN"); 1.1 Creating Optional objects - Optional.of(var)/ Optional.ofNullable(var) SoundCard soundCard = new SoundCard(); Optional<SoundCard> sc = Optional.of(soundCard); // NPE if soundCard is null Optional<SoundCard> sc = Optional.ofNullable(soundCard); // may hold a null value 1.2. Check presenting - .ifPresent(//) // ugly code SoundCard soundcard = ...; if(soundcard != null){ System.out.println(soundcard); } // solution 1 SoundCard soundcard = ...; if(soundcard != null){ System.out.println(soundcard); } // solution 2 if(soundcard.isPresent()){ System.out.println(soundcard.get()); } 1.3. Default - .orElse/ .orElseThrow // ugly code Soundcard soundcard = maybeSoundcard != null ? maybeSoundcard : new Soundcard("basic_sound_card"); // solution 1 Soundcard soundcard = maybeSoundcard.orElse(new Soundcard("defaut")); // default value Soundcard soundcard = maybeSoundCard.orElseThrow(IllegalStateException::new); // default throw E … ...

February 27, 2025 · 2 min · Phong Nguyen

Java Extension Points - Extensions

Explain how to use the Extension Point and Extensions. Refer1 Refer2 Refer3 1. Introduction Extensions are the central mechanism for contribute behavior to the platform. We used it to contribute functionality to a certain type of API. Extension points define new functions points for the platform that other plug-ins can plug into. E.g. When you want to create modular that can be added or removed at runtime, you can use the OSGi service. These extensions and extension points are defined via the plugin.xml file. ...

February 27, 2025 · 3 min · Phong Nguyen

DSF

Explain how to use the DSF. Refer1 1. Introduction Scenario: Debugger View Fetching Data Context: We want to show variable values in the Variables View as the user steps through the code. 1.1. Asynchronous Req: When the user steps through the program, the debugger needs to fetch variable values from the target (like a remote system). This operation might take a few milliseconds or more, depending on the connection. If we block the UI thread, the UI freezes. Solution: Use an asynchronous method to fetch variable data void getVariableValue(String varName, DataRequestMonitor<String> rm) { // Imagine this takes time (e.g., contacting GDB or a remote debugger) // Simulate async call executor.execute(() -> { String value = remoteFetchValue(varName); // Slow operation rm.setData(value); rm.done(); }); } Why Async? Prevents blocking the UI. Allows the Eclipse debug framework to continue updating other views. 1.2. Synchronous We want to implement a feature like evaluate expression that requires the value immediately. We don’t want to rewrite your entire logic using async callbacks just to get the result of getVariableValue(). ...

May 13, 2025 · 8 min · Phong Nguyen

Eclipse Project

Explain how to create the eclipse project. References: Wizards and Dialogs Eclipse fragment projects - Tutorial 1. New Project Creation Wizards Every plugin, fragment, feature and update site is represented by a single project in the workspace and allow PDE(Plugin Development Environment) to validate their manifest file(s). File > New > Project... > Plug-in Development Use a Plugin Project if we’re building new functionality. Use a Fragment Project if we need to modify an existing plugin without changing its core code. ...

February 5, 2025 · 3 min · Phong Nguyen