Results 1 to 7 of 7
Thread: Array of Objects Manipulation
- 11-15-2010, 05:42 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
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?!?
- 11-15-2010, 05:54 AM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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)
- 11-15-2010, 02:49 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
ahh thank you, i did have the array declared as an instance variable but room[] theRooms=new(xxx) messed everything up :(
- 11-15-2010, 03:18 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
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)
- 11-15-2010, 03:28 PM #5
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());}
- 11-16-2010, 02:48 AM #6
Member
- Join Date
- Oct 2010
- Posts
- 12
- Rep Power
- 0
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.
- 11-16-2010, 06:20 PM #7
Similar Threads
-
manipulation in double dimensional array
By akulkarni1234 in forum New To JavaReplies: 1Last Post: 07-19-2009, 07:37 PM -
Array of Objects
By sfe23 in forum New To JavaReplies: 19Last Post: 02-04-2009, 05:57 PM -
Array manipulation
By Ms.Ranjan in forum New To JavaReplies: 9Last Post: 07-18-2008, 09:10 PM -
Array of Objects
By bluefloyd8 in forum New To JavaReplies: 5Last Post: 01-22-2008, 06:27 PM -
Array with objects
By toby in forum New To JavaReplies: 1Last Post: 07-25-2007, 09:50 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks