4
I have a class like User
which has as attribute a vector of Sensors
, and the class Sensors
has as attribute a vector of Data
.
After creating five objects of type User and saving in a txt file, I populated 3 objects of type Sensor of a certain User and tried to save in the same txt file, but keeps giving this error of NullPointerException
.
public class test
{
public static void main ( String args[] ) throws IOException
{
Users[] users = new Users[5];
File file = new File("C:\\Users\\Daniel\\Documents\\poo.txt");
int id=0, goal, id2=0, consumo, n=0, i, j, k, novogoal, idedit, iddelete, iduser;
boolean type, status;
String name, password, c, name2, description, novoname, novopassword;
float cost, novocost;
for (i=0; i<5; i++)
{
users[i] = new Users(i, "", "", 0, 0);
for (j=0; j<50; j++)
{
users[i].sensors[j] = new Sensors(j, "", "", false);
for (k=0; k<100; k++)
{
DateTime time = new DateTime("2004-12-13T21:39:45.618-08:00");
users[i].sensors[j].data[k] = new Data(false, 0, time);
}
}
}
FUsers fusers = new FUsers();
FSensors fsensors = new FSensors();
FData fdata = new FData();
//criar e salvar usuário
for (i=0; i<5; i++)
{
c = JOptionPane.showInputDialog("Tipo de usuário: 1 - ADMIN | 0 - COMUM");
type = Boolean.parseBoolean(c);
name = JOptionPane.showInputDialog("Digite seu nome");
password = JOptionPane.showInputDialog("Digite a senha");
c = JOptionPane.showInputDialog ("Custo do kWh");
cost = Float.parseFloat(c);
goal = Integer.parseInt(JOptionPane.showInputDialog("Meta de consumo"));
users[i] = fusers.createUser(i, name, password, cost, goal, type);
}
fusers.saveUser (file, users);
//*/
/*criando um sensor*/
iduser = Integer.parseInt(JOptionPane.showInputDialog("Id do user que terá o sensor"));
for (i=0; i<3; i++)
{
name2 = JOptionPane.showInputDialog ("Aparelho ligado ao sensor");
description = JOptionPane.showInputDialog ("Localização do aparelho");
c = JOptionPane.showInputDialog ("O aparelho está ligado?");
status = Boolean.parseBoolean(c);
consumo = Integer.parseInt(JOptionPane.showInputDialog("Consumo do aparelho"));
users[iduser].sensors[i] = fsensors.createSensor(i, name2, description, status);
}
fsensors.saveSensor(file, users); /*erro aqui*/
}
}
public class FSensors {
public Sensors createSensor(int id, String name, String description, boolean status)
{
Users user = new Users();
user.sensors[id]= new Sensors(id, name, description, status);
return user.sensors[id];
}
/**
*
* @param file
* @param users
* @param sensor
* @throws IOException
*/
public void saveSensor(File file, Users[] user) throws IOException
{
int i, j;
String id, status;
for (i=0; i<5; i++)
{
for (j=0; j<50; j++)
{
try (BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)))
{
id = Integer.toString(user[i].sensors[j].getId()); /*erro aqui*/
status = Boolean.toString(user[i].sensors[j].getStatus());
bw.write(id);
bw.newLine();
bw.write(user[i].sensors[j].getNome());
bw.newLine();
bw.write(user[i].sensors[j].getDescription());
bw.newLine();
bw.write(status);
bw.newLine();
bw.close();
}
}
}
}
public class FUsers {
public Users createUser(int id, String name, String password, float cost, int goal, boolean type)
{
if (type)
{
AdminUser admin = new AdminUser();
admin.setId(id);
admin.setName(name);
admin.setPassword(password);
admin.setCost(cost);
admin.setGoal(goal);
admin.setType(type);
return admin;
}
else
{
CommonUser common = new CommonUser();
common.setId(id);
common.setName(name);
common.setPassword(password);
common.setCost(cost);
common.setGoal(goal);
common.setType(type);
return common;
}
}
public void saveUser (File file, Users[] user) throws IOException
{
int i;
String id, cost, goal, type;
for (i=0; i<5; i++)
{
if (i==0)
{
try (BufferedWriter bw = new BufferedWriter(new FileWriter(file)))
{
id = Integer.toString(user[i].getId());
cost = Float.toString(user[i].getCost());
goal = Integer.toString(user[i].getGoal());
type = Boolean.toString(user[i].getType());
bw.write(id);
bw.newLine();
bw.write(user[i].getName());
bw.newLine();
bw.write(user[i].getPassword());
bw.newLine();
bw.write(cost);
bw.newLine();
bw.write(goal);
bw.newLine();
bw.write(type);
bw.newLine();
bw.close();
}
}
else
{
try (BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)))
{
id = Integer.toString(user[i].getId());
cost = Float.toString(user[i].getCost());
goal = Integer.toString(user[i].getGoal());
type = Boolean.toString(user[i].getType());
bw.write(id);
bw.newLine();
bw.write(user[i].getName());
bw.newLine();
bw.write(user[i].getPassword());
bw.newLine();
bw.write(cost);
bw.newLine();
bw.write(goal);
bw.newLine();
bw.write(type);
bw.newLine();
bw.close();
}
}
}
}
public Users (int id, String name, String password, float cost, int goal)
{
this.id = id;
this.name = name;
this.password = password;
this.cost = cost;
this.goal = goal;
}
public Sensors (int id, String name, String description, boolean status)
{
this.name = name;
this.description = description;
this.status = status;
}
If anyone can help, I’m grateful!
This error occurs because an attribute or object itself was not created, debug the code and check the line that is occurring the error and where you can fix it, I don’t know exactly where the error occurs.
– Giancarlo Abel Giulian
but the object is not being created in those 3 be nested?
– poirot
You could edit the question to put the method code
createUser
? Is there a method calledpublic void saveSensor(File file, Users[] users)
instead ofpublic void saveSensor(File file, Sensors[] sensors)
? What’s in the classFUsers
?– Victor Stafusa
I edited the question and put createUser and saveUser, which is what’s on Fusers. I also tried to put as parameter the vector of Users, but is giving the same error.
– poirot
Your
fsensors.saveSensor(file, users);
does not compile, after allUser[]
is notSensor[]
. Moreover, what is it that the builders ofUsers
andSensors
do?– Victor Stafusa
What do the builders of the subclasses of
Users
?– Victor Stafusa
Opa, it was my mistake, there in the saveSensor method was to have (file, Users[] user), but I already edited. As I tried in several ways I ended up copying wrong.
– poirot
I have also edited with the builders of Users and Sensors. I have not placed constructs in the subclasses of Users, I am assigning values to attributes with set.
– poirot
I posted an answer, take a look.
– Victor Stafusa