We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I don't really know how to create a class within another class. For example:
class Houses {
color colour;
int size;
int housenumber;
class Rooms {
color colour;
int windows;
boolean wifi;
}
void build() {
// ...
}
}
// Now I want to something like this:
MyHouse.Bathroom.wifi = true;
MyHouse.colour = #FFFFFF;
MyHouse.build();
HouseOfMyNeighbour.housenumber = 5;`
I still have read many examples but all didn't work. There were problems in declaring the objects of the classes.
Answers
https://Forum.Processing.org/two/discussion/8042/processing-forum-rules-and-advices
Room[] rooms;
Thank you! But now it says "The global variable 'wifi' does not exist".
Your bathroom is not a Room - it is an array of Rooms. This is not what you want.
Think of an array as a list, like a shopping list. The name of the array is the list itself. "Dairy products". "Meats". The items on the list are specific objects. "Cheese". "Steak".
You've labeled a list of Rooms as "bathroom". But this list has no Rooms on it!
What you want is for your House to have a list of Rooms - called
rooms
- and then a specific item on this list is going to be the bathroom. ifbathroom
is 0, thenrooms[bathroom]
is rooms[0] which is the fist room on the list.Or, you can forget about your House having a list of rooms, and just have specific rooms. Room bathroom; Room bedroom; Room hallway;
Or you can have lists of bathrooms and bedrooms. Maybe it is a three bedroom, two bathroom house:
Thank you for your help! But if I run the following code it says NullPointerException in the last two lines:
That is because you have created a new House but you have not create the Bathroom. Change the last line to
Room bathroom = new Room();
You want an array of rooms
You need
setup
anddraw
as functionsYou can give the rooms in the array names
Have you read the tutorial on objects and arrays?
https://www.processing.org/tutorials/objects/
Missing
is also missing (when it’s not an array yet)
Do this in function
setup()
Thank you so much for your help! Now it works.
Could you please post your entire code for others?
I will try. But the problem is that I used the example with the houses and rooms instead of other things in a quite difficult code. So it will take some time.
here is my version with an array of rooms which makes it easier if you have more than 2 rooms in a house: