I am using NetBeans IDE for swings programming
This is a Add/Edit scenario, MyFrame1
handles MyClass1
obj and MyClass1
has MyClass2
, if a MyClass2
has to be add/edit present in MyClass1
MyFrame1
launches MyFrame2
I tried using the following code and got NullPointerException
class MyFrame2 extends JFrame implements ActionListener{
MyFrame1 parent;
MyClass2 obj;
Test(MyFrame1 parent){ // For Add scenario
//this.parent = parent
obj = new MyClass2();
//do something
}
Test(MyFrame1 parent, MyClass2 obj){ // For Edit scenario
//this.parent = parent
this.obj = obj;
//do something
}
//functions
........
private void foo(){
parent.addValues(obj); //throws NullPointerException
}
//some editor managed code and fields
............
{
this.parent = parent;
//other initalizations
}
}
As far as I know the
initializing block code is copied to the beginning of every constructor
initializing a field inside initializing block, the declaration of the field should present before the initializing block.
Then why I am getting NullPointerException
when I include this.parent = parent;
in initializing block and works fine if directly added to the constructors?
Is initializing block code not copied to the constructor?