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?