java - 结果集sqlite的Java空指针异常
问题描述
嗨,我得到 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。有人可以弄清楚是什么问题吗?
解决方案
Slaw 已经在评论回复中发布了此内容,但您的模型对象中的字段从未初始化。里面调用Operatore.setId()
的id
字段为null,导致NullPointerException。
推荐阅读
- plot - 一个块中的两个或多个图形的标题,带有 PDF 输出
- if-statement - 如何为主页做 IF 语句?
- html - 如何分离h4和p标签
- android - 在再次观看 AdMob 激励视频之前添加一段冷却时间
- javascript - Laravel AJAX Bootstrap 切换开关不起作用
- git - Jenkins 通用 Webhook 触发器,用于特定于分支
- algorithm - 这个递归代码究竟是如何工作的?
- sql - 根据数据组生成从 1 开始的新版本号
- python - 如何识别输入语句的不同响应?
- apache-flink - PyFlink 1.11.2 注册自定义 UDF 函数时无法配置 [taskmanager.memory.task.off-heap.size] 属性