3

The following code produces NullPointerException -

public class MyClass {

    static MyClass instance= new MyClass(); // line 3
    static Boolean have_Instance = true;
    Boolean inst_Avail=have_Instance; // line 5

    Boolean isInstAvail(){
        return inst_Avail;
    }

    public static void main(String[] args) {
        System.out.println(instance.isInstAvail() ? "Instance is there.":""); // gives java.lang.NullPointerException
    }

}

If I move line 3 to after line 5, it runs fine. How does the order matter here? Shouldn't instantiation of a class set iVar values everytime?

1
  • Uuuh... What are you trying to do exactly? Commented Jul 7, 2013 at 10:42

5 Answers 5

3

When an object is created on line 3 the class has not finished initializing yet, and the have_instance variable has its default value, null. This value is assigned to the inst_Avail member variable of the object, so the value returned by instance.isInstAvail() in the main method will be null.

An easy way to fix this is swapping lines 3 and 4, so have_instance already has a value when the object is created. Or you could declare have_instance as boolean instead of Boolean, so it will have the value false and not null. This would make the program print nothing though.

Or maybe you could rethink what you're trying to do. It's rarely a good idea to create instances of a class before the class has finished initializing, especially if the class is not "final" (i.e. may have subclasses).

Sign up to request clarification or add additional context in comments.

1 Comment

When you swap lines 3 and 4 the have_instance variable will be set before the instance is created, so yes, doing that makes the program print Instance is there. Why would there be an endless loop of new instances?
2

The order of fields matter if you initialize these fields either directly by setting their values or using a static initializer block. They are executed in order. So you couldn't do a forward reference:

private int someInt = 10 + otherInt;
private int otherInt = 22;

This won't work, because the fields are initialized in order of their textual declaration. If you have two static initializers, they will be executed in order as well:

static { System.out.println("first"); }
static { System.out.println("second"); }

So in your case, you initialize instance before have_instance, so the latter is still null (default value for non-primitives). The JVM will create a MyClass object to be assigned to instance and initialize its fields, i.e. assign the value of have_instance to inst_Avail which will be set to null as well.

Some readings:

Comments

1

This code is really weird and I dont see use-cases for this, but this would do the fix:

public class MyClass {

    static MyClass instance; // line 3
    static Boolean have_Instance = true;
    Boolean inst_Avail=have_Instance; // line 5

    Boolean isInstAvail(){
        return inst_Avail;
    }

    public static void main(String[] args) {
        instance = new MyClass();
        System.out.println(instance.isInstAvail() ? "Instance is there.":""); // gives     java.lang.NullPointerException
    }

}

Comments

0

Another solution to this could be to make inst_Avail as static, so that at the time of loading the class, this variable is instantiated:

 private static  Boolean inst_Avail=have_Instance;

Comments

0

you are assigning static variable value to non-static variable

i.e.

Boolean inst_Avail=have_Instance;

either make it static or

assign inst_Avail = true in the constructor

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.