1

Forgive me, I am new to Java! and is for a university project, I am having trouble with a few concepts, I have tried to google around to no avail!. How can I instantiate a certain amount of heaters of the heater class, inside the MarsRoom class, and then access them in the main of the MasterControlPanel. But depending on the number of numheaters set in the constructor??. I have tried this but its not recognising the object room1.roomheaters[0]. It does however recognise it if I just instantiate one object like Heaters roomheaters = new Heaters();. Many thanks

public class MasterControlPanel{

    public static void main(String[] args){

    MarsRoom room1 = new MarsRoom(40, 40, 20, 20, 8, 2, 4);
    MarsRoom room2 = new MarsRoom(40, 40, 20, 20, 8, 2, 4);
    MarsRoom room3 = new MarsRoom(40, 40, 20, 20, 8, 2, 4);
    MarsRoom room4 = new MarsRoom(40, 40, 20, 20, 8, 2, 4);
    room1.createheaters();

    System.out.println("Turned " + (room1.roomheaters[0].getHeater() ? " ON" : " OFF"));
    }
}


public class MarsRoom extends Rooms implements RoomInterface{

int roomareasq;
int heatloss;
float insideTemp;
float outsideTemp;
float uvalue;
int numheaters;
int numlights;
Heaters roomheaters[] = new Heaters[numheaters];

public MarsRoom(){
}

public MarsRoom(int windowsH, int windowsW, int wallsH, int wallsW, int windowC, int heaters, int lights){
    windowsHeight = windowsH;
    windowsWidth = windowsW;
    wallsHeight = wallsH;
    wallsWidth = wallsW;
    windowCeiling = windowC;
    numheaters = heaters;
    numlights = lights; 
}

public void createheaters(){
for (int i=0; i < numheaters; i++)
{
roomheaters[i] = new Heaters();
}
}


public void calculateheatloss(){

}

}
2
  • Where is roomheaters declared? Commented Feb 27, 2012 at 18:31
  • What do you mean by "not recognising"? Are you getting a NullPointerException or some other error? Commented Feb 27, 2012 at 18:32

2 Answers 2

2

The array roomheaters is never declared, so it's impossible to populate it, let alone access it as a property of a MarsRoom from another class:

roomheaters[i] = new Heaters();
// variables must be declared to be assigned

Have a look at declaring member variablesOracle


UPDATE

To access a variable as an object property like room1.roomheaters[0], its must be a member variable. You have only created a local variable that disappears as soon as the function returns. Above link should help.

Sign up to request clarification or add additional context in comments.

8 Comments

Hey thanks yeah, I have updated the code above, but its still not recognising it. I am getting the error "cannot find symbol"
@user969729 To access a variable as an object property like room1.roomheaters[0], its must be a member variable. You have only created a local variable that disappears as soon as the function returns.
Awesome, thanks very much for your time, it is working now but just another quick question when I declare the object array as follows Heaters roomheaters[] = new Heaters[2]; it works fine. but if I do Heaters[numheaters] instead, I get the error arrayindexoutofbounds? just wondering why this is happening.
as the numheaters variable is set to 2 aswell.
Arrays in java are zerobased, the first ellement in the list are roomheaters[0] and the last roomheaters[numheaters-1]. You try to access a value outside the array.
|
1

You have to move the instantiation of roomheaters out of the function and to the class variables and create a methode to acces the heaters. Add this in the MarsRoom class:

public Heaters[] getHeaters(){
  return roomheaters;
}

And you can access the heaters by calling room1.getHeaters()

2 Comments

Yeah this method is inside the heaters class which is instantiated inside the MarsRoom class. So I should have access to it?
Yes you have acces for it but it was the roomheaters variable you didn't have acces to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.