Help with classes and get/set methods?

java.lang.ArrayIndexOutOfBoundsException

Page 1 of 1

6 Replies - 1167 Views - Last Post: 16 November 2009 - 08:11 AM Rate Topic: -----

#1 BlueDog   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-November 09

Help with classes and get/set methods?

Posted 16 November 2009 - 04:51 AM

I'm working on a program that is supposed to help someone keep track of rooms rented out in a house. (This is a school assignment by the way.) I've come pretty far with the program, but now I have a recurring error that I just can't get rid of. The error is java.lang.ArrayIndexOutOfBoundsException. And it occurs from this code:

System.out.println("Please state the name of the person: ");
		name = input.next();

		if(rooms[floor][app].getName().getPerson().equals(name))


This is located in a class called House. The getName is located in a class Appartment and it calls on the getPerson located in class Person. More of the code in the House class

public class House {

	private House[][] rooms = new House[15][15];
	int floor = 15; //number of floors
	int app = 15; //number of appartments on each floor
// alot more of Strings and ints but they are not relevant to the question.

	public House() {
		for (int f = 0; f < floor; f++) {
			for (int a = 0; a < app; a++) {
				rooms[f][a] = new Appartment(roomName);
				rooms[f][a].setRoomName("" + (f + 1) + (char) ('A' + a));
			}
		}
	} // end House constructor


From the Appartment class:
	public void setPerson(Person name) {
		this.name = name;
	} 
	public Person getName() {
		return name;
	}


From Person class:
	public String getPerson() {
		return name;
	}


Why do I get that error? Is the program unable to retrieve the name? In that case, why? Any help would be greatly appreaciated. I really want to learn this.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with classes and get/set methods?

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Help with classes and get/set methods?

Posted 16 November 2009 - 06:18 AM

Place the following before your first quoted if statement:

System.out.printf("Floor index is %d, app index is %d, number of floors is %d and number of appts. on current floor is %d\n"
	floor,
	app,
	rooms.length,
	rooms[floor].length);


Was This Post Helpful? 0
  • +
  • -

#3 BlueDog   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-November 09

Re: Help with classes and get/set methods?

Posted 16 November 2009 - 07:20 AM

I've now tried that, g00se, but it didn't do any difference. Still getting the same error.
Was This Post Helpful? 0
  • +
  • -

#4 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Help with classes and get/set methods?

Posted 16 November 2009 - 07:23 AM

The problem is here:

 private House[][] rooms = new House[15][15];
 int floor = 15; //number of floors
 int app = 15; //number of apartments on each floor



The array index is 0 to size-1, in this case 0-14. When you try to access at index 15, you get that error.
Was This Post Helpful? 1
  • +
  • -

#5 BlueDog   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-November 09

Re: Help with classes and get/set methods?

Posted 16 November 2009 - 07:47 AM

That's a rookie mistake even for me. Thank you for pointing that out! But I still get the same error.

Can this be because when I try to rent out one room, the program rents out every room there is? Just wondering 'cause that is my other remaining problem with this program. The code for that part is:

	public void newPerson() {
		System.out.println("Please enter a name: ");
		name = input.next();

		for (int f = 0; f < 15; f++) {
			for (int a = 0; a < 15; a++) {
				if (room[f][a].appUnoccupied()) { 
					System.out.println("Appartment " + room[f][a].getRoomName() +
							" is now rented to " + name);
				}
				else {
					System.out.println("Sorry, we have no available appartments at this time.");
				} 
			}
		}
	} 


Or is the two issues totally unrelated...?

This post has been edited by BlueDog: 16 November 2009 - 07:49 AM

Was This Post Helpful? 0
  • +
  • -

#6 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Help with classes and get/set methods?

Posted 16 November 2009 - 07:52 AM

There's nothing in that loop that would throw an out of bounds error.


What size is room?
Was This Post Helpful? 0
  • +
  • -

#7 BlueDog   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-November 09

Re: Help with classes and get/set methods?

Posted 16 November 2009 - 08:11 AM

Well, good to know I can rule that out then!

It was supposed to be rooms not room. Where that s has gone, I do not know. Lol. In my original post I posted the code to where rooms are defined.

But now I've gotten a new error! And I guess that counts as progress now I get:

java.lang.NullPointerException

This is from the if posted in the first codebox in my first post:

	   System.out.println("Please state the name of the person: ");
		name = input.next();

		for (int f = 0; f< rooms.length; f++) {
			for (int a = 0; a < rooms[f].length; a++) {
				if (rooms[f][a].getName().getPerson().equals(name)){



I have modified it with two for-loops and the rooms.length's.

Edit: Got new error. Editet entire post.

This post has been edited by BlueDog: 16 November 2009 - 09:47 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1