Reputation: 2373
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?
Upvotes: 3
Views: 2424
Reputation: 381
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
}
}
Upvotes: 1
Reputation: 3442
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:
Upvotes: 2
Reputation: 111219
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).
Upvotes: 3
Reputation: 2555
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
Upvotes: 0
Reputation: 1967
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;
Upvotes: 0