File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Facing null pointer exception in static initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Facing null pointer exception in static initialization" Watch "Facing null pointer exception in static initialization" New topic
Author

Facing null pointer exception in static initialization

sumit anand kumar
Ranch Hand

Joined: Apr 28, 2010
Posts: 83
What on earth i am doing wrong


Output :


INDEX null
Exception in thread "main" java.lang.ExceptionInInitializerError
at constants.SerializeDeserialize.writeDuplicateConstantObject(SerializeDeserialize.java:23)
at constants.SerializeDeserialize.main(SerializeDeserialize.java:43)
Caused by: java.lang.NullPointerException
at constants.ConstantObject.<init>(ConstantObject.java:31)
at constants.ConstantObject.<clinit>(ConstantObject.java:20)
... 2 more
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Are you using Java 5+? Make an enumeration.
sumit anand kumar
Ranch Hand

Joined: Apr 28, 2010
Posts: 83
Sorry........my mistake. Not related to any version.
I had to declare that HashMap above the ConstantObject's initialization.
Or ConstantObject's initialization should be in static block after HashMap declaration.

I wasted lot of time before realizing this
Problem solved now
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

here is one more thing.....

see these lines..
private static final Map INDEX;

static {
INDEX = new HashMap();
}


Map INDEX is marked as final,and the reference type once marked as final cannot be changed.
and in the static block you again try to
marked INDEX to new HashMAp();
that cannot be done.

INDEX=new HashMap().......cannot be done


SCJP6.0,My blog Ranchers from Delhi
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Shanky Sohar wrote:.......cannot be done

Have you tried?
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

David O'Meara wrote:
Shanky Sohar wrote:.......cannot be done

Have you tried?

did it possible.as per what i have understood.we cannot changed the refence type marked as final


but here in this problem..compiler first reaches here


before finding the actual declaration of variable "INDEX"

David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

difference between 'declaration' and 'assigning a reference'.
When you provide a declaration for a (static) final variable, you need assign a value or reference for that variable before it can be used. Once a reference has been assigned it cannot be changed.
In the example above the initial declaration does not assign a value, it just says "something will go here, and when it does we don't want it to change". The static block gets run during initialisation before the Class is available for use, therefore it is able to provide code that determines the value for a variable, even a static final variable.
This is useful in cases where it is not easy (or possible) to provide a reference for a static final variable on a single line.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

David O'Meara wrote:difference between 'declaration' and 'assigning a reference'.
When you provide a declaration for a (static) final variable, you need assign a value or reference for that variable before it can be used. Once a reference has been assigned it cannot be changed.
In the example above the initial declaration does not assign a value, it just says "something will go here, and when it does we don't want it to change". The static block gets run during initialisation before the Class is available for use, therefore it is able to provide code that determines the value for a variable, even a static final variable.
This is useful in cases where it is not easy (or possible) to provide a reference for a static final variable on a single line.


Thanks for this..
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

private static final Map INDEX;

please tell whether i am understanding this correctly.
INDEX is declared to refer to Map only and once it will be refernce to HashMap then it will not changed..

David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Consider the following:
A static, final, unmodifiable Map<Integer, String> where the numbers 1...10 are the keys and the Map value is the String name of the number ie "One"..."Ten"
I can't think of an easy way of doing this without calling a static method, or you can set it up in a static block.
Now to understand this you should take a couple of minutes to write it yourself. (Use the java.util.Collections class to create an unmodifiableMap() )
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

@David O'Meara...i like the way you gave me a assignment to understand this.
i have done it

Please tell me how much i am correct

here is the code
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051


using array to insert the key and values

Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

i am preety sure for this


but in case of final variable i was confused....but now i am little clear.

please see my code given above and tell me which one will be perfect,and whether is there any performance issue with any between the two.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Shanky Sohar wrote:
please see my code given above and tell me which one will be perfect,and whether is there any performance issue with any between the two.

Your second one - constructing arrays and using them to add key-value pairs to the map in a static block seems to be more preferred as you always can modify the array initialization(add/remove elements in it at any point). Obviously it performs slightly less than the previous one as this involves creation of extra objects(arrays) and iterating through it, for the map initialization.
In the first one - direct initialization & performs better. The disadvantage being, what would you do if you need to add 100 items to the map?!

Of course, what we are talking about is just performance in nanoseconds! Actually you are not going to have any appreciable performance impact on your application as a whole when you use either.


OCPJP 6
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Vinoth Kumar Kannan wrote:
Shanky Sohar wrote:
please see my code given above and tell me which one will be perfect,and whether is there any performance issue with any between the two.

Your second one - constructing arrays and using them to add key-value pairs to the map in a static block seems to be more preferred as you always can modify the array initialization(add/remove elements in it at any point). Obviously it performs slightly less than the previous one as this involves creation of extra objects(arrays) and iterating through it, for the map initialization.
In the first one - direct initialization & performs better. The disadvantage being, what would you do if you need to add 100 items to the map?!

Of course, what we are talking about is just performance in nanoseconds! Actually you are not going to have any appreciable performance impact on your application as a whole when you use either.


if everything is made to happen inside the main method then is there any performance issue
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

mmap should be the unmodifiable map, not 'm'. This was the main point of the exercise ;)
ie 1) make mmap static final unmodifiable using a static block
2) try to do the same without using a static block or static method
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Facing null pointer exception in static initialization
 
Similar Threads
Nice Serialization Question
What is serialized???
doubt with serialization
Serialization question
Loading POJO object without loading library first