I have a class of type User
that has as attribute a vector of Sensors
, and class Sensors
has as attribute a vector of Data
.
After creating five User objects and saving them to a txt file, I populated 3 Sensor objects of a certain User and tried to save them to the same txt file, but this error gives 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!