首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >结果集sqlite出现Java空指针异常

结果集sqlite出现Java空指针异常
EN

Stack Overflow用户
提问于 2018-06-05 07:57:02
回答 2查看 309关注 0票数 0

嗨,我得到了nullpointerexception,但是我不知道为什么

DAO (不使用实用程序连接类)

代码语言:javascript
复制
 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;
    }


}

模型

代码语言:javascript
复制
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。有人能找出问题出在哪里吗?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50690270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档