aspose file tools*
The moose likes Beginning Java and the fly likes getting a Null pointer exception at this point Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getting a Null pointer exception at this point" Watch "getting a Null pointer exception at this point" New topic
Author

getting a Null pointer exception at this point

Deepika Namasani
Greenhorn

Joined: May 19, 2005
Posts: 25
Hello

can you please let me know y am i getting null pointer exception at this point

" ar[k2-1].add(new Integer(input[i]));"

from the following code.



Deepika N
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1008
    
    3
That is because you have allocated an array of three references to ArrayLists, but you haven't instantiated the actual ArrayList objects yet.

Here is the new code. I've taken the liberty of reworking how input[] is indexed in the first set of nested loops.



Cool?
[ June 24, 2005: Message edited by: Ryan McGuire ]
Deepika Namasani
Greenhorn

Joined: May 19, 2005
Posts: 25
oh thanks
yah that works
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Ryan please can you tell me why do we need to create a new ArrayList object, so that we don't get a NullPointerException.


Havn't we initialised ar[] prior to the above line of code
Deepika Namasani
Greenhorn

Joined: May 19, 2005
Posts: 25
Abdulla


I created Array of ArrayList

actual ArrayList syntax is ArrayList a = new ArrayList().


So i created an array of ArrayList
ie



i created array size of 3 each of type arrayList so at this point
ar[0],ar[1],ar[2] are just variables of type ArrayList are created but are pointing to null .
so until unless we redirect this from null to actual Arraylists they will be with null. and accessing a null object produces null pointer exception.

by assiginig
where k2 = 0,1,2

we are redirecting the variable to ArrayList object.

I hope i cleared ur doubt.

thanks
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Thanks Deepika
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting a Null pointer exception at this point