Array of my object

P

Piotre Ugrumov

I have created a class Room. In a main I have created an array of Room.
When I try to print the object(through the method toString) or to set the
parametres of the objects I receive an error during the execution.
Ho can I resolve this error?
Thanks.

This is the main:
public static void main(String[] args){
Room museo[]=new Room[9];

for (int i=0; i<9; i++){
museo.setRoom(0, "--", "--", "--", 0, 0, 0, 0);
}
System.out.println(museo[0].toString());
JOptionPane.showMessageDialog (null, museo[0].toString(),
"Risultato", JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}

This is the class Room:
public class Room {

private int id;
private String nome;
private String periodo;
private String opera;
private int vicinoa;
private int vicinod;
private int vicinos;
private int vicinods;

public Room() {
id=100;
nome="";
periodo="";
opera="";
vicinoa=100;
vicinod=100;
vicinos=100;
vicinods=100;
}


public Room(int a, String s, String p, String o, int va, int vd, int vs,
int vds){
setId(a);
setNome(s);
setPeriodo(p);
setOpera(o);
setVicinoAvanti(va);
setVicinoDietro(vd);
setVicinoSinistra(vs);
setVicinoDestra(vds);
}

public void setRoom(int a, String s, String p, String o, int va, int vd,
int vs, int vds){
setId(a);
setNome(s);
setPeriodo(p);
setOpera(o);
setVicinoAvanti(va);
setVicinoDietro(vd);
setVicinoSinistra(vs);
setVicinoDestra(vds);
}

public void setId(int a){
id=a;
}

public void setNome(String s){
nome=s;
}

public void setPeriodo(String p){
periodo=p;
}

public void setOpera(String o){
opera=o;
}

public void setVicinoAvanti(int va){
vicinoa=va;
}

public void setVicinoDietro(int vd){
vicinod=vd;
}

public void setVicinoSinistra(int vs){
vicinos=vs;
}

public void setVicinoDestra(int vds){
vicinods=vds;
}

public int getId(){
return id;
}

public String getNome(){
return nome;
}

public String getPeriodo(){
return periodo;
}

public String getOpera(){
return opera;
}

public int getVicinoAvanti(){
return vicinoa;
}

public int getVicinoDietro(){
return vicinod;
}

public int getVicinoSinistra(){
return vicinos;
}

public int getVicinoDestra(){
return vicinods;
}

public String toString(){
String f="";
f+="ID: " + id + ", nome stanza: " + nome + ", periodo stanza: " +
periodo + ", opera principale: " + opera;
f+="/nVicino Avanti: " + vicinoa + "\n";
f+="/nVicino Dietro: " + vicinod + "\n";
f+="/nVicino Sinistra: " + vicinos + "\n";
f+="/nVicino Destra: " + vicinods + "\n";
return f;
}

}
 
A

Andrew Thompson

Piotre said:
I have created a class Room.

As you mentioned in the message you
multi-posted to comp.lang.java.help.

Please do not multi-post Piotre, it
wastes everybodys time.
 
E

EdUarDo

Piotre said:
I have created a class Room. In a main I have created an array of Room.
When I try to print the object(through the method toString) or to set the
parametres of the objects I receive an error during the execution.
Ho can I resolve this error?
Thanks.

This is the main:
public static void main(String[] args){
Room museo[]=new Room[9];

for (int i=0; i<9; i++){

You forgot create new objects...

museo = new Room();
museo.setRoom(0, "--", "--", "--", 0, 0, 0, 0);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top