Jump to content

  • Log In with Google      Sign In   
  • Create Account


Why does this code not work?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Enerjak   Members   -  Reputation: 213

Like
0Likes
Like

Posted 19 April 2013 - 08:02 PM

OperatingRoom[] room = new OperatingRoom[3];
		for(int i = 0; i < 3; i++)
		{
			room = new OperatingRoom[i];
			System.out.println("Op room #" + i);
			room[i] = new OperatingRoom();
			room[i].fillRoom(opRoom);
			room[i].display();
		}

 

it gives me this error: 

 

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
	at readPatient.main(readPatient.java:54)

 

I don't get why it gives this error when, as you can see, there's no ArrayIndexOutOfBounds



Sponsor:

#2 phil_t   Crossbones+   -  Reputation: 2484

Like
4Likes
Like

Posted 19 April 2013 - 08:11 PM

I haven't touched java in a long time, but it looks like on your first iteration through the loop, you're assigning a zero size array to the room variable. Then you reference it at index 0  -> that's out of bounds. Why do you have the "room = new OperationRoom[i]" line?



#3 Enerjak   Members   -  Reputation: 213

Like
0Likes
Like

Posted 19 April 2013 - 09:34 PM

I haven't touched java in a long time, but it looks like on your first iteration through the loop, you're assigning a zero size array to the room variable. Then you reference it at index 0  -> that's out of bounds. Why do you have the "room = new OperationRoom[i]" line?

I thought java had that but i removed it and it sorta worked. (It compiled good but didn't add the data in. :-D)



#4 Cornstalks   Crossbones+   -  Reputation: 6882

Like
4Likes
Like

Posted 19 April 2013 - 09:47 PM

You want:

 

room[i] = new OperatingRoom();

 

When you say new OperatingRoom[i], you're creating a new array of OperatingRooms (not a single OperatingRoom), and then you're assigning it to the variable room. This means that just before the loop, you set room to an array of size 3 of OperatingRooms. However, on the first run through the loop, you reset room to a new array of size zero (because i is zero). Then, a couple lines later, you say room[i], but room is an empty array of length zero! You can't get anything out of it.

 

So to recap:

  1. You want to populate the array with OperatingRoom objects instead of resetting the array (which is why you say room[i] = instead of room =)

  2. You want to say new OperatingRoom() to create an individual object, not new OperatingRoom[i] (which creates an array)


Edited by Cornstalks, 19 April 2013 - 09:48 PM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#5 PsychotikRabbit   Members   -  Reputation: 156

Like
0Likes
Like

Posted 20 April 2013 - 08:00 AM

OperatingRoom[] rooms = new OperatingRoom[3];

for(int i = 0; i < rooms.length; i++)
{
   System.out.println("Op room #" + i);
   rooms[i] = new OperatingRoom();
   rooms[i].fillRoom(opRoom); //What is opRoom ? I guess you have a variabe not included in your snippet.
   rooms[i].display();
}





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS