Results 1 to 4 of 4
- 02-08-2012, 11:09 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 28
- Rep Power
- 0
NullPointerException when there is data in array
I have an array which has 6 items in it. When I print the array size, it displays 6. But when I try to access the item at index 1, it gives me the NullPointerException error:
Java Code:Tile[] tiles = (Tile[])input; print("tile array length: "+ tiles.length); print("tile-->"+tiles[0].getTileNum()); print("tile-->"+tiles[1].getTileNum());
tile array length: 6
tile-->45
java.lang.NullPointerException
It traces back to the line:Java Code:print("tile-->"+tiles[1].getTileNum());
-
Re: NullPointerException when there is data in array
The length of an array just returns how many items the array can hold, nothing more. It tells you nothing about whether the elements in the array are filled or not. Think of it as if it were a parking lot. The length tells you how many parking spots are present in the lot, but tells you nothing about how many have cars parked in them yet.
Your problem is that you're trying to call a method on a null variable -- on an array element that does not yet refer to an object.
- 02-08-2012, 11:29 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 28
- Rep Power
- 0
Re: NullPointerException when there is data in array
Thank you, i got to my error :)
- 02-08-2012, 11:33 PM #4
Senior Member
- Join Date
- Aug 2011
- Posts
- 251
- Rep Power
- 7
Similar Threads
-
NullPointerException when accessing array
By aianta in forum New To JavaReplies: 3Last Post: 10-15-2011, 10:19 PM -
Data structures(sort) nullpointerexception
By Fabken in forum New To JavaReplies: 6Last Post: 07-28-2011, 09:01 PM -
text into array...nullpointerexception
By csuever in forum New To JavaReplies: 4Last Post: 04-01-2011, 12:08 AM -
Please help on nullpointerexception error.. Cannot initallize the array properly.
By tvenhc in forum New To JavaReplies: 8Last Post: 03-31-2011, 06:09 AM -
Add data to an array
By adlb1300 in forum New To JavaReplies: 8Last Post: 11-05-2007, 02:01 AM
Bookmarks