我正在编码,然后发现一个问题。当我想设置文本时,出现了nullpointerexception。
我已经尝试修复它。但这没有用。第一次可以使用,但是第二次可以使用nullpointerexception。我不知道为什么?
public void setBalanceText() {
balanceRepository.updateBalance();
String total="Total Balance : "+String.valueOf(balanceRepository.sumBalance();
totalBalance.setText(total);//where the problem starts
}
public void updateBalance() {
String query = "update info set total=(select sum(amount)from profits)-(select sum(amount)from expenses) where id = 1";
try {
connect();
statement = connection.prepareStatement(query);
statement.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
disconnect();
} catch (Exception e) {
}
}
}
public double sumBalance() {
String query = "select total from info ";
try {
connect();
statement = connection.prepareStatement(query);
resultSet = statement.executeQuery();
if (resultSet.next()) {
return resultSet.getDouble("total");
}
} catch (IOException | SQLException e) {
e.printStackTrace();
} finally {
try {
disconnect();
} catch (SQLException e) {
}
}
return 0.0;
}
}
错误:
java.lang.NullPointerException
at az.controller.BalanceController.setBalanceText(BalanceController.java:267)