Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why NullPointerException? [duplicate]

Tags:

I have a abstract class and a derived class. Look at provided code:-

public abstract class Parent{     public Parent(){         init();     }      public abstract void init();     }    public class Child extends Parent{     private String mTitle = null;      public Child(){         super();         System.out.println(mTitle.toString());     }             public void init(){         mTitle = "It' a test";     }     } 

When I will execute the above code it will throw NullPointerException on printing the value of mTitle. If you check the code in constructor of parent I have called the the abstract method which will called the init method of derived class, In abstract method I have initialize the value of mTitle value as ="It's a test";

After calling parent constructor derived class have to call the System.out.println.

If it is doing in that way then why it is throwing NullPointerException.

But, If I just leave the assignment of mTitle it will not throw Exception like:-

private String mTitle; 

If initialization of variable occur on calling of the contruct of class and we know by default global object have initialize to null. But in this case it will not throw Exception.

like image 536
VikasGoyal Avatar asked Nov 09 '14 10:11

VikasGoyal


People also ask

What is the reason for NullPointerException?

What Causes NullPointerException. The NullPointerException occurs due to a situation in application code where an uninitialized object is attempted to be accessed or modified. Essentially, this means the object reference does not point anywhere and has a null value.

How do I fix NullPointerException?

In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

What is root cause of it and NullPointerException?

NullPointerException is raised in an application when we are trying to do some operation on null where an object is required. Some of the common reasons for NullPointerException in java programs are: Invoking a method on an object instance but at runtime the object is null.

What can help us in avoiding NullPointerException?

Java 8 introduced an Optional class which is a nicer way to avoid NullPointerExceptions. You can use Optional to encapsulate the potential null values and pass or return it safely without worrying about the exception. Without Optional, when a method signature has return type of certain object.


2 Answers

As in JLS §12.5 (Creation of New Class Instances) the following procedure is used when an instance is created:

  1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.

  2. If this constructor begins with an explicit constructor invocation (§8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.

  3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.

  4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.

  5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.

That means your call to super() and the subsequent call to the overridden init() method are both done before the instance variable is initialized with null which discards the outcome of the init method and overwrites any value being assigned to mTitle with the null value.

This leads to the following Golden Rule: Never call non-final, non-private methods in a constructor!

like image 58
Seelenvirtuose Avatar answered Oct 01 '22 01:10

Seelenvirtuose


According to Section 12.5 of the JLS, the superclass constructor will run before the initialiser for mTitle, which means that it will be set back to null after it is set to "It's a test".

like image 36
Dawood ibn Kareem Avatar answered Oct 01 '22 01:10

Dawood ibn Kareem