Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: nullpointer exception when adding Object to ArrayList

  1. #1
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: nullpointer exception when adding Object to ArrayList

    it says nullpointer exception
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.


  2. #2
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: nullpointer exception when adding Object to ArrayList

    Done

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: nullpointer exception when adding Object to ArrayList

    Done
    Are you saying the project is done?

    If you are still getting the error: Please copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: nullpointer exception when adding Object to ArrayList

    Quote Originally Posted by Norm View Post
    Are you saying the project is done?

    If you are still getting the error: Please copy the full text of the error message and paste it here.
    Oh sorry, I meant that I've editted the post, why can't i see the post anymore? did I made a mistake?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: nullpointer exception when adding Object to ArrayList

    Waiting.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: nullpointer exception when adding Object to ArrayList

    The error message is: java.lang.NullPointerException: null

    and the code is

     
    public class Game 
    {
        private Parser parser;
        private Player player;
        private int moveLimit;
        private ArrayList<Room> rooms;
     
        /**
         * Create the game and initialise its internal map.
         */
        public Game() 
        {
            Room start = createRooms();
            parser = new Parser();
            player = new Player(start);
            moveLimit = 30;
            rooms = new ArrayList<Room>();
        }
     
        public static void main(String[] args)
        {
            Game game = new Game();
            game.play();
        }
     
        /**
         * Create all the rooms and link their exits together.
         */
        private Room createRooms()
        {
            Room mainRoom, livingRoom, bedroom, playRoom, kitchen, diningRoom, hallway, tortureRoom, 
            cellar, dungeon, lobby, bathroom, library, chamberOfSecrets, garden, artRoom, trapdoor;
     
            // create the rooms
            mainRoom = new Room("in the main room, in the middle of the castle");
            livingRoom = new Room("in the living room");
            playRoom = new Room("teleport");
            kitchen = new Room("in the kitchen");      
            garden = new Room("in the garden");
            diningRoom = new Room("in the dining room");
            hallway = new Room("in the hallway");
            tortureRoom = new Room("in the torture room, the room is filled with scary objects");
            cellar = new Room("in the cellar");
            dungeon = new Room("in the dungeon");
            library = new Room("you fell into a trapdoor, you can get out using the rope");
            lobby = new Room("in the lobby");
            bedroom = new Room("in the bedroom");
            bathroom = new Room("in the bathroom");
            chamberOfSecrets = new Room("in the CHAMBER OF SECRETS");
            artRoom = new Room("in the art room");
            trapdoor = new Room("You fell into a trapdoor");
     
            // lock certain rooms
            cellar.lockRoom();
            chamberOfSecrets.lockRoom();
     
            // initialise room exits
            mainRoom.setExit("east", diningRoom);
            mainRoom.setExit("south", kitchen);
            mainRoom.setExit("west", livingRoom);
     
            livingRoom.setExit("north", playRoom);
            livingRoom.setExit("east", mainRoom);
     
            playRoom.setExit("south", livingRoom);
     
            kitchen.setExit("north", mainRoom);
            kitchen.setExit("south", garden);
            kitchen.setExit("down", cellar);
     
            garden.setExit("north", kitchen);
     
            diningRoom.setExit("east", hallway);
            diningRoom.setExit("west", mainRoom);
     
            hallway.setExit("west", diningRoom);
            hallway.setExit("up", lobby);
     
            cellar.setExit("north", dungeon);
            cellar.setExit("east", library);
            cellar.setExit("south", tortureRoom);
            cellar.setExit("up", kitchen);
     
            dungeon.setExit("south", cellar);
     
            library.setExit("west", cellar);
     
            tortureRoom.setExit("north", cellar);
     
            lobby.setExit("north", bedroom);
            lobby.setExit("south", bathroom);
            lobby.setExit("west", artRoom);
            lobby.setExit("down", hallway);
     
            bedroom.setExit("south", lobby);
     
            bathroom.setExit("north", lobby);
     
            artRoom.setExit("east", lobby);
            artRoom.setExit("west", chamberOfSecrets);
     
            // add items to the room
            chamberOfSecrets.addItem("GOLD", ", THERE IS GOLD EVERYWHERE", 1000000);
     
            kitchen.addItem("chicken", ", a big roosted chicken", 20);
            kitchen.addItem("cooky", ", this is a magic cooky that will increase your ability's when you eat it", 5);
     
            bathroom.addItem("soap", ", a purple duck shaped soap", 6);
            bathroom.addItem("silverkey", ", you can use this key to open a door!", 5);
     
            dungeon.addItem("goldkey", ", you can use this key to open a door!", 10);
     
            library.addItem("map", ", There is a map on the wall of the bottom floor \n to see the map type 'show'", 5000); 
     
            rooms.add(garden);
            rooms.add(library);
            rooms.add(bedroom);
     
            // start game outside
            return mainRoom;   
        }

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: nullpointer exception when adding Object to ArrayList

    Please copy full text of error message and paste it here. It should show where the error happened.
    Here's a sample:
    Exception in thread "main" java.lang.NullPointerException
    	at TestCode12.main(TestCode12.java:108)
    This shows the error happened at line 108 in the method: main in TestCode12
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: nullpointer exception when adding Object to ArrayList

    Quote Originally Posted by Norm View Post
    Please copy full text of error message and paste it here. It should show where the error happened.
    Here's a sample:
    Exception in thread "main" java.lang.NullPointerException
    	at TestCode12.main(TestCode12.java:108)
    This shows the error happened at line 108 in the method: main in TestCode12
    Oke sorry, I'm still learning

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: nullpointer exception when adding Object to ArrayList

    Without that error message, there is no way to know where the error is happening.

    How are you executing the program? How does it display the error message?

    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: nullpointer exception when adding Object to ArrayList

    I've uploaded the game, can you please check it out?

    http://rapidshare.com/files/1529024034/Versie%2008.zip

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: nullpointer exception when adding Object to ArrayList

    If you need help with the error, please copy the full text of the error message and paste it here.
    I do NOT go to links posted on the forum.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: nullpointer exception when adding Object to ArrayList

    Is this what you need?

    java.lang.NullPointerException
    at Game.createRooms(Game.java:136)
    at Game.<init>(Game.java:33)

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: nullpointer exception when adding Object to ArrayList

    java.lang.NullPointerException
    at Game.createRooms(Game.java:136)
    There is a variable at line 136 that has a null value when that statement is executed. Look at line 136, find the variable with the null value (use a println to print out the values if needed)
    and then backtrack in the code to see why that variable does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: nullpointer exception when adding Object to ArrayList

    I knew that is had a null value, I just don't know why because the method first creates a new Room object and than adds it to the ArrayList

    garden = new Room("description");
    rooms.add(garden);

    I don't know how this possible can't work?

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: nullpointer exception when adding Object to ArrayList

    What variable has the null value?

    Where does the program assign that variable a valid value?

    Post the source code for the line with the error
    and the source code that assigns that variable a value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Adding Object to ArrayList
    By jaydac12 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: January 20th, 2013, 10:02 AM
  2. NullPointer exception error
    By davx in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 18th, 2012, 01:08 PM
  3. NullPointer Exception problem
    By anshu in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 26th, 2012, 06:38 AM
  4. [SOLVED] Adding an object to Arraylist from text files
    By andreas90 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: January 18th, 2012, 09:27 AM
  5. adding an object to an arrayList
    By urpalshu in forum Object Oriented Programming
    Replies: 1
    Last Post: November 5th, 2011, 01:04 AM