So i have to write a program that designs a basic Home CAD System. The user will be able to build a house, add/remove rooms and items, etc. I have began writing the program and all seems gravy, i decided to use an ArrayList for the list of rooms the user adds but i am unsure of how to add to this ArrayList. We have been provided with a test harness class which tests to see if our program functions correctly, and at the moment the program successfully "adds" the rooms, but when i try to retrieve the information about the added rooms i am faced with a nullpointerexception, which i believe is because i am not using the ArrayList properly. Here is some of my code which i believe may be the problem:

private static RoomReference ref1 = new RoomReference(1, 1, 1);

//creating the first room

System.out.print("Creating first room...");
			model.addRoom(new RectangularRoom(ref1, "RoomA", 3, 4, 2));


// find first room
			System.out.print("Finding first room with getRoom()...");
			if (failureCheck(model.getRoom(ref1).toString().equalsIgnoreCase(
					"1,1,1:3,4,2:RoomA::"), true))
				return false;

//array stuff

	ArrayList<Room> room = new ArrayList<Room>();

	public void addRoom(Room room) {
		room.add(new RectangularRoom(ref1, "Room1", 4, 3, 2));
	}

Thanks in advance for your help.

I assume that's part of a method?
You declare the ArrayList within the method, so it will vanish each tine the method terminates. You also initialize it to a new (ie empty) ArrayList every time.
It needs to be declared and initialized somewhere outside the method.

faced with a nullpointerexception,

please copy and paste here the full text of the error message. It has useful info to solve your problem.

Initialising HomeCAD engine with owner: TEST OWNER, and budget 1000...
Creating first room...Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method add(RectangularRoom) is undefined for the type Room

at homecad.model.facade.HomeCADengine.addRoom(HomeCADengine.java:28)
at homecad.test.TestHarness.testAddRoom(TestHarness.java:104)
at homecad.test.TestHarness.main(TestHarness.java:1142)
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]

You use the same name "room" for both the parameter and the arraylist.

Ok that took away most of the error message but the nullpointer exception is still there, this is the current error message:

Initialising HomeCAD engine with owner: TEST OWNER, and budget 1000...
Creating first room...Creating second room...Creating third room...Finding first room with getRoom()...Unexpected exception! null
java.lang.NullPointerException
**FAIL**

===============================================================================
Tests
at homecad.test.TestHarness.testAddRoom(TestHarness.java:114)
===============================================================================


Line 114 is underlined below:

// find first room
	System.out.print("Finding first room with getRoom()...");
[U]	if (failureCheck(model.getRoom(ref1).toString().equalsIgnoreCase(
					"1,1,1:3,4,2:RoomA::"), true))[/U]
				return false;

There are three things that could be null in that line that I can see. Those are model, ref1, and the return from getRoom(ref1). Try preceding the line with some test code.
Something like

System.out.println( (model==null)? "Model is null" : model.toString());

is a good frame. Put this before the line with the exception and you'll know what's in each of those variables. (if you haven't defined a toString() for the classes, you'll get some non-helpful stuff, but you'll know there's something there anyway)

Break the chained statement up into simple single statements to see which method is returning a null and test the model variable for null.

model and ref1 are initialised within the test harness provided so it wasnt them but i put this in the getRoom method:

@Override
	public Room getRoom(RoomReference location) {
		
		while(((ArrayList<Room>) rooms).contains(null)){
			
			return null;
		}
		
		return rooms;
	}

and i got this error:

Initialising HomeCAD engine with owner: TEST OWNER, and budget 1000...
Unexpected exception! java.util.ArrayList cannot be cast to homecad.model.Room
java.lang.ClassCastException: java.util.ArrayList cannot be cast to homecad.model.Room
**FAIL**

===============================================================================
Tests
===============================================================================
at homecad.model.facade.HomeCADengine.<init>(HomeCADengine.java:15)
at homecad.test.TestHarness.initialiseEngine(TestHarness.java:56)
at homecad.test.TestHarness.testAddRoom(TestHarness.java:97)


Line 15: Room rooms = (Room) new ArrayList<Room>();
Line 56: model = new HomeCADengine(name, budget);
Line 97: initialiseEngine("TEST OWNER", 1000);

Sorry, I'm not familiar with the program that generated that output.
The message says something about your casting of variables.
Check the types of all the variables and make sure the casts are valid.

Ok so what if i removed all the casting and stuff within the method getRoom(). How would i retrieve the information stored in the ArrayList?

I didn't mean to say remove the casting. I meant to say: do the casting correctly.

You need to make a small sample program the compiles and executes to demonstrate the problem.
Your small code pieces don't show all that needs to be known about the code.

How would i retrieve the information stored in an ArrayList in general?

Use one of the ArrayList methods.

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.