Hello, so i have a RectangularRoom, AbstractRoom (which is an abstract class) and Room (which is an interface). AbstractRoom and RectangularRoom both implement Room. Within my HomeCADengine (which is where my addRoom/removeRoom/etc. methods are kept). Now, my addRoom method seems to be a problem. I am trying to add a room to my ArrayList within this method, but cannot because Room is an interface, it says it can not be instantiated, and so can not add the room to the ArrayList. How can i add a room to my ArrayList?

public void addRoom(Room room){
		List<Room> rooms = new ArrayList<Room>();
		RoomReference ref;
		String name;
		int x, y, z;
		
		rooms.add(new Room(ref, name, x, y, z));
		
	}

Error message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot instantiate the type Room

at homecad.model.facade.HomeCADengine.addRoom(HomeCADengine.java:31)
at homecad.test.TestHarness.testAddRoom(TestHarness.java:105)
ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
JDWP exit error AGENT_ERROR_NO_JNI_ENV(183): [../../../src/share/back/util.c:820]

Line 31: rooms.add(new Room(ref, name, x, y, z));
Line 105: model.addRoom(new RectangularRoom(ref1, "RoomA", 3, 4, 2));

Thanks in advance.

Line 7 you have new Room(...), but Room isn't a class, it's an interface. You can only "new" a class such as RectangularRoom.
But why aren't you adding the Room that's passed in as a parameter?
And, you create a new list each tine this is called, and that goes out of scope at the end of the method.
And you don't initalise name,x,y,z so that won't work either.
So, scrap all this and do something more like:

List<Room> rooms = new ArrayList<Room>(); // in the class, not the method
Room r = new RectangularRoom(etc...);     // in some method
rooms.add(r);

Ok so I put that piece of code into my method, and would like to check if it has worked, there are no direct error messages but what could i code into my getRoom() method that would retrieve the information from the ArrayList. In my getRoom() method i currently have (i know this is incorrect):

public Room getRoom(RoomReference location) {
		
		for(int i = 0; i < rooms.size(); i++)
		rooms.get(i);
		
		return name;
		
	}

Error message when run:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
i cannot be resolved to a variable

at homecad.model.facade.HomeCADengine.getRoom(HomeCADengine.java:44)

Line 44: return i;

Can you post all of the code before line 44 back to the start of the method the line 44 is in?

Is the variable i defined?

public Room getRoom(RoomReference location) {

		for(int i = 0; i < rooms.size(); i++)
		rooms.get(i);
		
		return i;
		
	}

sorry i don't know why i put 'name' there in the post, its 'i' in the getRoom() method :)

Member Avatar for coil

The method above simply traverses the ArrayList then returns a variable "i", which is probably undefined because "i" exists only in the for loop.

rooms.get(i) returns a Room, but you don't need a for-loop to access it. I'm not sure why you have a RoomReference as a parameter.

This might be better.

public Room getRoom(int index)
return room.get(index);

i is out of scope when you get to the return (line six of your last post)
In any case, it would be an int, while getRoom should return a Room (as you correctly specify in the method declaration)

I can't tell what sort of thing a RoomReference is, but you are passing one in, called "location" and then ignoring it. Perhaps you should use that reference to get at the room it's referring to.

(What's a RoomReference? Is it like an integer?)

RoomReference is its own class which stores the x,y,z locations of the room.

So you want to use this to locate the room in the arraylist? Maybe by walking down the list, trying values until you get one that matches x, y, and z?
That would explain why you were iterating the list.

Have I understood you correctly?

yes, how would i code that though?

Member Avatar for coil

Do you have methods in Room that return x, y, and z? If so, you could do something like:

Room r;

for(<each element in ArrayList>)
if(element.getX()==location.getX() && etc.)
r=element;

return r;

Yeah, that's pretty much where I was going with that... wouldn't be bad to let the poster work some of this out, though.

Does the Room class hold a RoomReference? (and if not, why not).
If so, you should be comparing the RoomRef directly, rather than delving down into the x,y,z details. You can override the equals(...) method in your RoomRef class to define what you mean by "these two RoomRefs are the same" - presumably by comparing their x,y,z etc. But after you have done this one time, you can then use RoomRefs as "first class" objects that you can use directly to search lists or use as keys into a map.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.