Results 1 to 4 of 4
- 08-27-2009, 09:01 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 6
- Rep Power
- 0
Null Pointer Exception when adding items to ArrayList
Hi everyone,
I'm pretty new here, but I was hoping you could help me out with a little problem I seem to be having. As the title so aptly states, I'm receiving NullPointerException when I try to add an object to my ArrayList. I'm very new to ArrayLists so It's probably something ridiculously obvious, but your help is greatly appreciated.
Here's the code that I think matters. If you need more, let me know, I'll be happy to post it.
In the class Room.java
In the main classJava Code:// Holds the GameObjects that are present in the room private ArrayList<GameObject> roomObjects; ... // Public method to add objects to the roomObjects ArrayList public void addRoomObject(GameObject obj){ roomObjects.add(obj); // <- NullPointerException }
Java Code:// Create a test object GameObject testObj = new GameObject(); testObj.setSDesc("A shiny ruby has been carelessly discarded here."); // Add the object to the room test.addRoomObject(testObj); // <- NullPointerException
The compiler (NetBeans) shows me two locations for the exception, which I commented in the above code samples. Again, thanks so much for your help.
- 08-27-2009, 09:16 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
does not create an ArrayList object. It creates a reference to an ArrayList object. At this point the reference is initialized as pointing to null (the default value for reference types). You need to initialize roomObjects by using the new operatorJava Code:private ArrayList<GameObject> roomObjects;
Java Code:roomObjects = new ArrayList<GameObject>();
- 08-27-2009, 09:21 AM #3
Member
- Join Date
- Aug 2009
- Posts
- 6
- Rep Power
- 0
Ok, well I figured it out. It was a very noobish mistake and I shouldn't be up at 3:30 in the morning coding, but I forgot to actually create the arraylists lol. I just created the references without creating them.
That fixed that problem. Also in my output method I was outputting the object itself, and not the objects short description.Java Code:// Holds the GameObjects that are present in the room private ArrayList<GameObject> roomObjects = new ArrayList(); // Holds the RoomExits that are present in the room private ArrayList<RoomExits> roomExits = new ArrayList();
So thanks anyhow :) Figured I'd at least post this in case someone else ran into a similar problem.Java Code:Iterator<GameObject> itr = roomObjects.iterator(); System.out.println(); // Print the contents while (itr.hasNext()){ System.out.println(itr.next().getSDesc()); // Didn't have the .getSDesc }
- 08-27-2009, 09:23 AM #4
Member
- Join Date
- Aug 2009
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
null pointer exception
By anthonym2121 in forum New To JavaReplies: 7Last Post: 04-06-2009, 03:25 AM -
Null pointer exception
By Stephenmak in forum New To JavaReplies: 5Last Post: 04-01-2009, 02:17 PM -
null pointer exception
By jyothi.priyanka in forum New To JavaReplies: 12Last Post: 03-11-2009, 05:04 PM -
Null Pointer Exception
By andre1011 in forum Advanced JavaReplies: 4Last Post: 02-07-2009, 03:30 AM -
Null Pointer Exception
By ScKaSx in forum New To JavaReplies: 1Last Post: 01-24-2009, 11:27 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks