1 2 Previous Next 15 Replies Latest reply: Jul 24, 2008 10:20 PM by 843785 RSS

    Help! Cant call Iterator from external object!

    843785
      Hi,

      I'm very new to Java so i'm well aware this might just be stupid mistake that many of you may scoff at :P

      I have a collection of Room objects in a Hotel object. The iterator for this collection is stored in the hotel object (htl), and when I use it inside the hotel object, it works fine. However, try to call it from an object of another class (in this case an object of my "Operations" class), and it throws up a NullPointerException error. Why? Heres the code...
           //.........Method to display all of the rooms.........
           public void allrooms(){
                     Iterator<Object> iter = htl.theRooms();
                     while(iter.hasNext() == true) {
                          Room RM = (Room) iter.next();
                          RM.display();
                
           }
        • 1. Re: Help! Cant call Iterator from external object!
          843785
          You should certainly be able to access an Iterator in another class/method. Porbably the error occurs in your Hotel class. code for which you did not post. Also post the exact error message (copy and paste) and indicate on which line it occurs.
          • 2. Re: Help! Cant call Iterator from external object!
            843785
            Hi Flounder,

            Heres my hotel class -
            import java.util.ArrayList;
            import java.util.Collection;
            import java.util.Iterator;
            
            public class Hotel {
                 Operations oper;
                 private String Name;
                 public Iterator<Object> theRooms;
                 public Collection<Object> Rooms;
                 
            
                 //............Constructor............
                 public Hotel(String Name) {
                      this.Name = Name;
                      Rooms = new ArrayList<Object>();
                 }
                 
                 //............Methods to add each type of room.............
                 public void addConfRoom(int RoomNumber, int FloorNumber, int MaxPerson, String Name){
                      Room RM = new ConferenceRoom (RoomNumber, FloorNumber, MaxPerson, Name);
                      Rooms.add(RM);
                 }
                 public void addStudyRoom(int RoomNumber, int FloorNumber, int MaxPerson, String ConfRoom){
                      int studyRN = RoomNumber;
                      int studyFN = FloorNumber;
                      int studyMP = MaxPerson;
                      if (ConfRoom != null){
                           Iterator<Object> iter = theRooms();
                           while(iter.hasNext() == true) {
                                ConferenceRoom RM = (ConferenceRoom) iter.next();
                                if (RM.getName() == ConfRoom){
                                     RM.addLinkedStudy(studyRN, studyFN, studyMP);
                                }
                      
                           }
                      }
                      Room RM = new StudyRoom (RoomNumber, FloorNumber, MaxPerson);
                      Rooms.add(RM);
                      
                      
                      
                      }
                 
                 public void addBedroom(int RoomNumber, int FloorNumber, int MaxPerson){
                      Room RM = new Bedroom (RoomNumber, FloorNumber, MaxPerson);
                      Rooms.add(RM);
                      
                      }
                 
                 
                 //............Iterator to access the collections............
                 public Iterator<Object> theRooms(){
                      return Rooms.iterator();
                 }
            }
            Also, I maybe should add that Room is an abstract class with subclasses Bedroom, StudyRoom and ConferenceRoom.

            My NullPointerException error occurs at the "Iterator<Object> iter = htl.theRooms();" line.
            • 3. Re: Help! Cant call Iterator from external object!
              843785
              CBSmith wrote:
              My NullPointerException error occurs at the "Iterator<Object> iter = htl.theRooms();" line.
              In that case htl is causing the NPE and not your iterator. You need to look in whatever class that varible exists to see why it is null. Probably you have masked it with a local variable with the same name.
              class Foo {
                  String s;
              
                  public void guff() {
                      String s = "hello";
                  }
              
                  public void bar() {
                      System.out.println(s.length()); // NPE
                  }
              
                  public static void main(String[] args) {
                      Foo f = new Foo();
                      f.guff();
                      f.bar();
                  }
              }
              • 4. Re: Help! Cant call Iterator from external object!
                843785
                I havent used the htl variable at all throughout my program. htl is the name of the Hotel object.
                • 5. Re: Help! Cant call Iterator from external object!
                  843785
                  CBSmith wrote:
                  I havent used the htl variable at all throughout my program.
                  If this is your code
                  Hotel htl;
                  and you never have
                  htl = new Hotel();
                  somewhere in your code before the allRooms method is called then htl will be null. Hence your problem.
                  • 6. Re: Help! Cant call Iterator from external object!
                    843785
                    Actually I do have that and thats where some new form of confusion has come down on me!

                    I have a class called "Application" which is the class which has my "main" method. It is as follows -
                    import java.util.Iterator;
                    
                    
                    public class Application {
                    
                         public static void main(String[] args) {
                              //............Creation of Initial Objects............
                              MainMenu menu = new MainMenu();
                              Operations oper = new Operations ();
                              Hotel htl = new Hotel("theHotel");
                              htl.addConfRoom(2, 2, 1, "Test");
                              htl.addStudyRoom (3, 2, 15, "Test");
                              htl.addBedroom(2, 2, 1);
                              oper.allrooms();
                              
                         }
                         }
                    The method in which I am calling my iterator is the "allrooms()" method in the oper object, which is an instance of a class called Operations. For some reason, replacing the line "oper.allrooms();" with my iterator call make everything work fine. However I dont want that...I want to be able to call it in my oper object...
                    • 7. Re: Help! Cant call Iterator from external object!
                      843785
                      As I have been trying to point out, the htl variable you have declared inside the main method is local to that method. It is not available to the allRooms method. It must be accessing some other htl variable that has not been initialised and is null.
                      • 8. Re: Help! Cant call Iterator from external object!
                        843785
                        I am absolultely certain I have not used the htl variable anywhere else in my code, apart from the creation of my Hotel object and passing it into the Operations class using Hotel htl.
                        • 9. Re: Help! Cant call Iterator from external object!
                          843785
                          CBSmith wrote:
                          passing it into the Operations class using Hotel htl.
                          Not in the code you posted above. Then again if you are so positive then your compiler must be lying to you and I have been wasting my time and effort in helping you!
                          • 10. Re: Help! Cant call Iterator from external object!
                            843785
                            I havent posted my entire Operations class code, here it is -
                            import java.util.Iterator;
                            
                            public class Operations {
                                 Hotel htl;
                            
                                 //.........Method to display all of the rooms.........
                                 public void allrooms(){
                                           
                                           Iterator<Object> iter = htl.theRooms();
                                           while(iter.hasNext() == true) {
                                                Room RM = (Room) iter.next();
                                                RM.display();
                                      
                                 }
                                 }
                                 
                                                
                            }
                            Is there not another reason that this isn't working? I definetly havent used htl anywhere else in my code,

                            Edited by: CBSmith on Jul 24, 2008 8:00 PM
                            • 11. Re: Help! Cant call Iterator from external object!
                              843785
                              You have to set Operations htl to the value you created elsewhere. Just because two classes have a variable with the same name does not make them have the same value!

                              Do you have a line of code somewhere in Operations that does something to set what htl (in Operations mind you) is?
                              htl = // something ?
                              • 12. Re: Help! Cant call Iterator from external object!
                                843785
                                No I don't, how would I go about using htl = to tell it that I'm referring to the object htl?
                                • 13. Re: Help! Cant call Iterator from external object!
                                  843785
                                  Sick of bashing head. Here change your main to be like this.
                                  public static void main(String[] args) {
                                      MainMenu menu = new MainMenu();
                                      Hotel htl = new Hotel("theHotel");
                                      Operations oper = new Operations (htl);
                                      htl.addConfRoom(2, 2, 1, "Test");
                                      htl.addStudyRoom (3, 2, 15, "Test");
                                      htl.addBedroom(2, 2, 1);
                                      oper.allrooms();
                                  }
                                  
                                  //Operations class
                                  Hotel htl;
                                  
                                  public Operations(Hotel h) {
                                      htl = h;
                                  }
                                  • 14. Re: Help! Cant call Iterator from external object!
                                    843785
                                    Flounder gave you your answer but you really need to review the Java tutorial on [_Classes and Objects_|http://java.sun.com/docs/books/tutorial/java/javaOO/index.html]. Especially the parts on declaring variables, understanding instance and class variables and passing information to a method or constructor.

                                    You're missing some piece of fundamental understanding of Java here that is going to be problematic for you if you don't address it.
                                    1 2 Previous Next