Results 1 to 7 of 7
  1. #1
    DreamNaut is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default Array of Objects Manipulation

    Hello , I have a method that adds a new instance to an array of objects

    public void addReservation(String gInput,int roInput,int bInput,double raInput, int nInput)
    {
    int roomsRented=0;
    if(roomsRented<theRooms.length){
    theRooms[roomsRented]=new Room(gInput,roInput,bInput,raInput,nInput);
    roomsRented++;
    }
    }


    I also have a constructor with the array of objects

    public Hotel(String inputName){
    name=inputName;
    Room[] theRooms=new Room[NUM_ROOMS];
    }


    Now when I test this method

    public class HotelTester{

    public static void main(String[] args){

    Hotel test1=new Hotel("Hilton");

    test1.addReservation("John Doe",4,2,29.95,5);

    }


    it throws a null pointer exception error pointed to my addreservations method.


    any advice on this? I am stumped. I suppose the addreservation method is incorrect but I don't see how it makes sense to me?!?

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    because theRooms is a local variable and only visible in the constructor!

    declare the variable outside:

    private Room[] theRooms;

    and initialize in the constructor theRooms=new Room[NUM_ROOMS]; (without Room[] at the beginning)

  3. #3
    DreamNaut is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    ahh thank you, i did have the array declared as an instance variable but room[] theRooms=new(xxx) messed everything up :(

  4. #4
    DreamNaut is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    I think I am declaring arrays illegally but the compiler is not catching it??

    public int reportReservation(int rInput){

    int i=0;
    int x=theRooms[i].getNumber();
    for (i = 0; i < theRooms.length; i++)
    {
    if (x==rInput){

    System.out.println(theRooms[i].toString());
    }
    }
    System.out.println("Reservation not found!");
    return NOT_FOUND;



    }

    This code simply checks for a room number and if theres a match prints out an instance of the array, the match prints out, but then it throws a null pointer error highlighting System.out.println(theRooms[i].toString());

    (the to string method is from the "room" class)

  5. #5
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    some of the positions in "theRooms[]" are null - you either need to initialise (theRooms[i] = new Room()) them before you can use it, or you need to check if its null (if (theRooms[i] != null {System.out.println (theRooms[i].toString());}

  6. #6
    DreamNaut is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    Thanks for the help so far, my methods are somewhat functional now

    the only problem I have is when the add reservation method is used more then once, it overwrites the pre-existing element.

    public void addReservation(String gInput,int roInput,int bInput,double raInput, int nInput)
    {

    int roomsRented=0;
    if(roomsRented<theRooms.length){
    theRooms[roomsRented]=new Room(gInput,roInput,bInput,raInput,nInput);
    roomsRented++;
    }

    }


    Now there is a code in my book:

    if(roomsRented<theRooms.length)
    {
    for (int i=roomsRented; i>pos; i--){
    theRooms[i] = theRooms[i-1];
    }
    theRooms[pos]= (new element);
    roomsRented++;
    }


    which seems to work when setting the pos=0, but I don't understand how it works without having roomsRented defined? roomsRented is only defined as an instance variable. So shouldn't the compiler throw an error?
    Last edited by DreamNaut; 11-16-2010 at 04:08 AM.

  7. #7
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Sorry, im being a little slow today, and cant really understand the question.

    I think the "int roomsRented = 0" line is supposed to be a class variable (i.e. outside the addReservation method)

Similar Threads

  1. manipulation in double dimensional array
    By akulkarni1234 in forum New To Java
    Replies: 1
    Last Post: 07-19-2009, 07:37 PM
  2. Array of Objects
    By sfe23 in forum New To Java
    Replies: 19
    Last Post: 02-04-2009, 05:57 PM
  3. Array manipulation
    By Ms.Ranjan in forum New To Java
    Replies: 9
    Last Post: 07-18-2008, 09:10 PM
  4. Array of Objects
    By bluefloyd8 in forum New To Java
    Replies: 5
    Last Post: 01-22-2008, 06:27 PM
  5. Array with objects
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 09:50 AM

Posting Permissions

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