Results 1 to 6 of 6
  1. #1
    baseballfan is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default Need a little Help

    I am trying to create a method that creates a random room when called. when I get to the point in my program that the method gets called I get this error java.lang.nullpointerexception.
    here is the code I got for the Transporter class
    Java Code:
     
    
    import java.util.Random;
    import java.util.List;
    import java.util.ArrayList;
    public class Transporter extends Room
    {
       private List rooms;
       private Random random;
       private ArrayList<Room> responses;
    
           public Transporter(String direction)
        {
            super(direction);
            
        }
      
    
          
        public Room getExit (String direction)
        {
            return findRandomRoom();
           
        }
        
       
        public Room findRandomRoom()
        {
            
           return (Room)rooms.get(random.nextInt(rooms.size()));
       
       
       
            
        }
       
    }
    My room class code
    Java Code:
    import java.util.*;
    
    public class Room 
    {
        public String description;
        private Item itemInRoom;    
        private HashMap<String, Room> exits;  
    
    
        public Room(String description) 
        {
            this.description = description;
            
            exits = new HashMap<String, Room> ( ) ;  
        }
    
          public void addItem(Item i)
        {
          itemInRoom = i;
        }
        
         public Item getItem()
        {
            return itemInRoom;
        }
        
           
        public String getLongDescription()
        {
            return "You are " + description + ".\n" + getExitString();
        }
        
        
        
        
        
         public String getExitString()
        {
            String returnString = "Exits:";
            Set<String> keys = exits.keySet();
            for (String exit : keys) {
                 returnString += " " + exit;
            }
            return returnString;    
            
        }
        
        
    
        
         public Room getExit(String direction)
        {
            return (Room)exits.get(direction);  //ex. 7.8 
        }
        
        
    
         * Define the exits of this room.  Every direction either leads
         * to another room or is null (no exit there).
         */
        public void setExit(String direction, Room neighbor) 
        {
            exits.put(direction, neighbor);
        }
    
        
    
    
        /**
         * 
         */
        public String getDescription()
        {
            return description;
        }
    
    }
    any help is greatly appreciated

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Your rooms List is still null, initialize it to a new empty list.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default

    When do you initialize your rooms variable?

    Edit- Too slow :c
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by KevinWorkman View Post
    Edit- Too slow :c
    Ha! I was faster for once! Gimme a J, gimme an O, gimme an S, what's that spell?! :-P

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

    Default

    O L D? S O D?

    db <-- sometimes dyslexic

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Darryl.Burke View Post
    O L D? S O D?

    db <-- sometimes dyslexic
    No, wrong, sorry; if you'd guessed "the great, magnificent" we'd rewarded you with 1/2 point but now, no, nothing, sorry.

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

Posting Permissions

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