5

I'm getting NullPointerException whith below code.

Parent.java

public abstract class Parent {

    public Parent(){
        parentFunc();
    }

    public abstract void parentFunc();
}

Child.java

public class Child extends Parent {
    ArrayList<String> list = new ArrayList<String>();

    @Override
    public void parentFunc() {
        list.add("First Item");
    }
}

when I create and instance of Child like new Child() I'm getting NullPointerException Here is my console output

Exception in thread "main" java.lang.NullPointerException
    at Child.parentFunc(Child.java:8)
    at Parent.<init>(Parent.java:5)
    at Child.<init>(Child.java:3)
    at Main.main(Main.java:8)

I know that exception occurs because of Child.parentFunc() which called in parent constructor, but I really got confused. So I wonder what is going on

What is the order of creation;

  1. When list variable is created
  2. When constructors are called
  3. When functions are created and called which called in constructors
1
  • Parents are constructed first, then children. The List wasn't created when the function was called, hence the NPE. Commented Dec 20, 2013 at 20:29

4 Answers 4

5

When list variable is created?

It will be created once constructor of Child class runs.

When constructors are called?

When you try to make an object using new Child(), constructor is called for Child and it internally calls super() which calls the superclass constructor Parent().Note that the very first statement in Child() constructor is super().

The system will generate no-arguement constructor for you like this:

public child()
{
 super();// calls Parent() constructor.
 //your constructor code after Parent constructor runs
}

When functions are created and called which called in constructors

Clear out a bit what are you trying to ask .

Order Of Execution:

Parent Constructor->Child Constructor-->List

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

Comments

2

When parent constructor invoked the function called , You list not initialized and defailu value to the Object null is there,

 @Override
    public void parentFunc() {
        list.add("First Item");   // list not yet initialized
    }

2 Comments

actucally I know, but I wonder when child constructor is created
Child constructor is provided by System in your case. And the very first statement in system-provided Child constructor called the super();
2

You have an implicit Child() constructor. It calls Parent(), Parent() calls parentFunc() which is called in the sub-class. At that moment your list is still null and you get the NullPointerException (NPE).

See also:

Java Constructor and Field Initialization Order

Comments

2

It's usually a bad idea to call abstract methods from a constructor, as you exposed yourself to this kind of problem.

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.