0

Hi I get nullpointerexception but I can't figure out why

DAO (without using utility connection class)

 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;
    }


}

MODEL

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;
    }


}

I have two problems here, the first is I get resultset closed from the connection utility class so as you can see i transferred connection code to the DAO and I commented the rows that were using utility class(not the best option I think but I don't know how to manage CachedRowSet)

the second is that when I tried to connect inside the DAO, I tried to print a row from the resultset just before calling the getOperatoriList method and it returns what I want. Then when calling getOperatoriList method, it doesn't set values to Operatore object and I get NullPointerException. Someone can figure out what's the problem?

Luca5om3
  • 71
  • 7
  • 1
    What line is the exception on? – Dawood ibn Kareem Jun 05 '18 at 00:01
  • 1
    You never seem to set your property fields in the model class. If you never do then they will be null when you attempt to set (or get) them which will result in a `NullPointerException`. Why don't you have things like, `private final IntegerProperty id = new SimpleIntegerProperty(this, "id");`? – Slaw Jun 05 '18 at 00:14
  • Also I can't see where the resultset and connection get closed, which means that the code might bomb out the second time it is called. In SQLite if I get a resultset still being open usually it means I didn't close one (and often it's not even the one I'm currently looking at, which makes it harder to track down) – Rick Jun 05 '18 at 00:19

2 Answers2

1

Slaw already posted this in a comment reply, but the fields in your model object are never initialized. Inside the to call Operatore.setId() the id field is null, resulting in the NullPointerException.

cotej
  • 26
  • 1
-1

It's possible to get back a null ResultSet instead of one that is merely empty.

Try checking to see if rs == null before passing it to your method.

Rick
  • 576
  • 3
  • 12
  • According to the Javadoc for `PreparedStatement`, `executeQuery` returns _"a ResultSet object that contains the data produced by the query; never null"_ – Dawood ibn Kareem Jun 05 '18 at 00:06