Skip to main content

When programming java.lang.NullPointerException occurs, what causes this error? how to solve?

 When we programming with java, we can see 'java.lang.NullPointerException' error often. NullPointerException is kinds of RuntimeException.

java.lang.NullPointerException

 This error occurs as a Java program attempts to use a null when an object is required. Let's see what this means.


        Integer invalid_intObject = null;
        System.out.println(invalid_intObject.intValue());

 This codes makes NullPointerException because invalid_intObject is object but it's null. Below is valid code.  


        Integer invalid_intObject = 0;
        System.out.println(invalid_intObject.intValue());

 But in real programming world, this is not that simple to debug. So, what's in real programming world? we always call functions that we did not make.


        Integer invalid_intObject = null;

        // .. some code here
        // .. my logic here

        usefulFunction(invalid_intObject);

 This is real world. UserfulFuncion could be standard library or opensource library. and Exception could be like below:


    Exception in thread "main" java.lang.NullPointerException
        at app.App.usefulFunction(App.java:22)
        at app.App.main(App.java:11)

 Actually in real world, the Exception line could be more than 10 lines. then we should track the Exception lists till my code appears.

 we should see App.java file line 22. that's 


    public static void usefulFunction(Integer intObject){
        System.out.println(intObject.intValue());           // line 22
    }

in my app.App.main(App.java) file line 11 is


        usefulFunction(invalid_intObject);          // line 11

now, we can solve the problem by assigning just before the error. ex) 'invalid_intObject = 0;'

Comments

Popular posts from this blog

Visual Studio Code Prettier vs Beautify plugin

    There are two famous beautify code plugins for Front-end developers in Visual Studio Code. So, I'd like to compare what plugin is better to use for me.   The plugins are First, Prettier Second, Beautify. As you can see, Prettier is more popular than Beautify and Prettier supports more languages than Beautify.  Prettier  Install count: 8,628,031 language support:  JavaScript · TypeScript · Flow · JSX · JSON CSS · SCSS · Less HTML · Vue · Angular GraphQL · Markdown · YAML Beautify  Install count: 5,510,943 language support:  javascript, JSON, CSS, Sass, and HTML Prettier has more strict options users should use while Beautify let users do more free way. But I'd like to use Prettier for now because I don't need free style and Prettier supports more langua...

Can't install Eclipse plugins due to sun.security.validator.ValidatorException

   Sometimes eclipse cannot connect to marketplace or plugin sites for installing plugins because of security validator exception Problem: sun.security.validator.ValidatorException or sun.security.validator.ValidatorException: PKIX path building failed   Resolution: tuststore does not contain the certificate of the SSL service you're connecting to This is due to corporate environments where your workstation is intercepted by proxy, firewall or something that inspects https traffic. or the jdk version does not include the SSL service you're connecting to. jdk release with certificates in the jre/lib/security/cacerts file. the certificates are increase while jdk version goes up How To Solve Download  Keystore Explorer  and Install Open the application (Run as administrator) Examine > Examine SSL > Co...

Visual Studio Code pin on 'open editors' (file) tab

It was released in May 2020 release and updated in September 2020 release. Eclipse doesn't have this functionality at this time.   When I program something with many files, I really need this function. Most editors has 'close Others', 'close to the right' options. But with this pin file and 'close All', I could manage opened files easily. See below Introduce on September 2020 release. Pinned tabs also show a new "pinned" icon to allow you to unpin with one click. This icon will also appear in the "Open Editors" view. Theme:  GitHub Light Also they added compact options for editor tabs. A new setting  workbench.editor.pinnedTabSizing  allows you to configure how large a pinned tab should appear: normal : a pinned tab inherits the look of other tabs (new default) shrink : a pinned tab shrinks to a fixed size showing parts of the editor label compact : a pinned tab will only show as icon or first letter of the ed...