【发布时间】:2018-06-04 23:57:02
【问题描述】:
您好,我得到 nullpointerexception 但我不知道为什么
DAO(不使用实用程序连接类)
package application.model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import application.util.SqlConnection;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class OperatoreDAO {
public static ObservableList<Operatore> cercaOperatori() throws SQLException, ClassNotFoundException {
//Declare a SELECT statement
String selectStmt = "SELECT * FROM Operatori WHERE DataCessazione=\"\"";
//Execute SELECT statement
try {
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/Users/Utente/Desktop/DB/prova.db");
PreparedStatement stmt = conn.prepareStatement(selectStmt);
ResultSet rsOperatori = stmt.executeQuery();
//Get ResultSet from dbExecuteQuery method
//ResultSet rsOperatori = SqlConnection.dbExecuteQuery(selectStmt);
ObservableList<Operatore> operatoriList = getOperatoriList(rsOperatori);
return operatoriList;
} catch (SQLException e) {
System.out.println("SQL select operation has been failed: " + e);
//Return exception
throw e;
}
}
private static ObservableList<Operatore> getOperatoriList(ResultSet rs) throws SQLException, ClassNotFoundException {
ObservableList<Operatore> operatoriList = FXCollections.observableArrayList();
while (rs.next()) {
Operatore op = new Operatore();
op.setId(rs.getInt("idOperatore"));
op.setCognome(rs.getString("Cognome"));
op.setNome(rs.getString("Nome"));
op.setSesso(rs.getString("Sesso"));
op.setEta(rs.getInt("Eta"));
op.setDomicilio(rs.getString("Domicilio"));
operatoriList.add(op);
}
return operatoriList;
}
}
型号
package application.model;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.StringProperty;
public class Operatore {
private IntegerProperty id;
private StringProperty cognome;
private StringProperty nome;
private StringProperty sesso;
private IntegerProperty eta;
private StringProperty domicilio;
public int getId() {
return id.get();
}
public void setId(int idIn) {
id.set(idIn);
}
public IntegerProperty idProperty() {
return id;
}
public String getNome() {
return nome.get();
}
public void setNome(String nomeIn) {
nome.set(nomeIn);
}
public StringProperty nomeProperty() {
return nome;
}
public String getCognome() {
return cognome.get();
}
public void setCognome(String cognomeIn) {
cognome.set(cognomeIn);
}
public StringProperty cognomeProperty() {
return cognome;
}
public String getSesso() {
return sesso.get();
}
public void setSesso(String sessoIn) {
sesso.set(sessoIn);
}
public StringProperty sessoProperty() {
return sesso;
}
public int getEta() {
return eta.get();
}
public void setEta(int etaIn) {
eta.set(etaIn);
}
public IntegerProperty etaProperty() {
return eta;
}
public String getDomicilio() {
return domicilio.get();
}
public void setDomicilio(String domicilioIn) {
domicilio.set(domicilioIn);
}
public StringProperty domicilioProperty() {
return domicilio;
}
}
我在这里有两个问题,第一个是我从连接实用程序类中关闭了结果集,因此您可以看到我将连接代码传输到 DAO,并且我评论了使用实用程序类的行(我认为这不是最好的选择但我不知道如何管理 CachedRowSet)
第二个是当我尝试在 DAO 内部进行连接时,我尝试在调用 getOperatoriList 方法之前从结果集中打印一行,它会返回我想要的内容。 然后在调用 getOperatoriList 方法时,它不会为 Operatore 对象设置值,我得到 NullPointerException。 有人能找出问题所在吗?
【问题讨论】:
-
异常在哪一行?
-
您似乎从未在模型类中设置属性字段。如果您从不这样做,那么当您尝试设置(或获取)它们时它们将为空,这将导致
NullPointerException。你为什么没有private final IntegerProperty id = new SimpleIntegerProperty(this, "id");之类的东西? -
我也看不到结果集和连接在哪里关闭,这意味着代码可能会在第二次调用时崩溃。在 SQLite 中,如果我得到一个仍然打开的结果集通常意味着我没有关闭一个(而且通常它甚至不是我当前正在查看的那个,这使得追踪变得更加困难)
标签: java sqlite javafx jdbc resultset