java - Why NullPointerException? -



java - Why NullPointerException? -

this question has reply here:

what's wrong overridable method calls in constructors? 5 answers nullpointerexception : overriding constructor calling method of base of operations class in derived class 2 answers

i have abstract class , derived class. @ provided code:-

public abstract class parent{ public parent(){ init(); } public abstract void init(); } public class kid extends parent{ private string mtitle = null; public child(){ super(); system.out.println(mtitle.tostring()); } public void init(){ mtitle = "it' test"; } }

when execute above code throw nullpointerexception on printing value of mtitle. if check code in constructor of parent have called the abstract method called init method of derived class, in abstract method have initialize value of mtitle value ="it's test";

after calling parent constructor derived class have phone call system.out.println.

if doing in way why throwing nullpointerexception.

but, if leave assignment of mtitle not throw exception like:-

private string mtitle;

if initialization of variable occur on calling of contruct of class , know default global object have initialize null. in case not throw exception.

as in jls §12.5 (creation of new class instances) next procedure used when instance created:

assign arguments constructor newly created parameter variables constructor invocation.

if constructor begins explicit constructor invocation (§8.8.7.1) of constructor in same class (using this), evaluate arguments , process constructor invocation recursively using these same 5 steps. if constructor invocation completes abruptly, procedure completes abruptly same reason; otherwise, go on step 5.

this constructor not begin explicit constructor invocation of constructor in same class (using this). if constructor class other object, constructor begin explicit or implicit invocation of superclass constructor (using super). evaluate arguments , process superclass constructor invocation recursively using these same 5 steps. if constructor invocation completes abruptly, procedure completes abruptly same reason. otherwise, go on step 4.

execute instance initializers , instance variable initializers class, assigning values of instance variable initializers corresponding instance variables, in left-to-right order in appear textually in source code class. if execution of of these initializers results in exception, no farther initializers processed , procedure completes abruptly same exception. otherwise, go on step 5.

execute rest of body of constructor. if execution completes abruptly, procedure completes abruptly same reason. otherwise, procedure completes normally.

that means phone call super() , subsequent phone call overridden init() method both done before instance variable initialized null discards outcome of init method , overwrites value beingness assigned mtitle null value.

this leads next golden rule: never phone call non-final, non-private methods in constructor!

java exception inheritance abstract

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -