Results 1 to 4 of 4
  1. #1
    ShadowCopy is offline Member
    Join Date
    Aug 2009
    Posts
    6
    Rep Power
    0

    Unhappy 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
    Java 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
        }
    In the main class
    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.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Java Code:
     private ArrayList<GameObject> roomObjects;
    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 operator
    Java Code:
    roomObjects = new ArrayList<GameObject>();

  3. #3
    ShadowCopy is offline Member
    Join Date
    Aug 2009
    Posts
    6
    Rep Power
    0

    Default

    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.

    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();
    That fixed that problem. Also in my output method I was outputting the object itself, and not the objects short description.

    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
            }
    So thanks anyhow :) Figured I'd at least post this in case someone else ran into a similar problem.

  4. #4
    ShadowCopy is offline Member
    Join Date
    Aug 2009
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    Java Code:
     private ArrayList<GameObject> roomObjects;
    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 operator
    Java Code:
    roomObjects = new ArrayList<GameObject>();
    Thank you very much. I figured it out, stupid mistake :)

Similar Threads

  1. null pointer exception
    By anthonym2121 in forum New To Java
    Replies: 7
    Last Post: 04-06-2009, 04:25 AM
  2. Null pointer exception
    By Stephenmak in forum New To Java
    Replies: 5
    Last Post: 04-01-2009, 03:17 PM
  3. null pointer exception
    By jyothi.priyanka in forum New To Java
    Replies: 12
    Last Post: 03-11-2009, 06:04 PM
  4. Null Pointer Exception
    By andre1011 in forum Advanced Java
    Replies: 4
    Last Post: 02-07-2009, 04:30 AM
  5. Null Pointer Exception
    By ScKaSx in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 12:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •