Shubham Gaikwad
Shubham Gaikwad

Reputation: 123

NullPointerException: thrown where it's not expected

public void setSpecifications(ObservableList<Specifications> data) {
    String sql = "select * from specifications where ( bid=" + bid[0] + " or bid=" + bid[1] + " or bid=" + bid[2] + " or bid=" + bid[3] + " ) and ( mid=" + mid[0] + " or mid=" + mid[1] + " or mid=" + mid[2] + " or mid=" + mid[3] + " ) and ( vid=" + vid[0] + " or vid=" + vid[1] + " or vid=" + vid[2] + " or vid=" + vid[3] + " );";
    int colCount = 0;
    String  specVal[], paramVal = null;
    specVal = new String[]{" ", " ", " ", " "};
    try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery(sql);
        rsmd = rs.getMetaData();
        colCount = rsmd.getColumnCount();
        for (int colIndex = 0; colIndex < colCount;  colIndex++) {
            rs.beforeFirst();
            paramVal=rsmd.getColumnName(colIndex+1);
            for (int rowIndex = 0; rs.next(); rowIndex++) {
                specVal[rowIndex] = rs.getString(paramVal);
            }
            data.add(new Specifications(paramVal, specVal[0], specVal[1], specVal[2], specVal[3]));
            //**NullPointerException**
        }
    } catch (SQLException ex) {
        Logger.getLogger(Compare.class.getName()).log(Level.SEVERE, null, ex);
    }



}

Stack Trace:

java.lang.NullPointerException
file:<path>.jar!/carshowroomsystem/details/compare/Compare.fxml
  at carshowroomsystem.dataInterface.Compare.setSpecifications(Compare.java:138)

  at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
  at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
 ...  rest of the trace

Caused by: java.lang.NullPointerException
    at carshowroomsystem.dataInterface.Compare.setSpecifications(Compare.java:138)
    at carshowroomsystem.details.compare.CompareController.addCar(CompareController.java:74)
    at carshowroomsystem.details.compare.CompareController.initialize(CompareController.java:65)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2152)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2028)
    at carshowroomsystem.WelcomeController.handleCompareAction(WelcomeController.java:106)
    ... 44 more

It's throwing NullPointerException at line data.add(...) where I have checked all the arguments have some value! No idea what to do. And why the trace is showing Compare.fxml file? It should be .java

Upvotes: 0

Views: 113

Answers (1)

kan
kan

Reputation: 28951

You have String specVal[], paramVal = null; which initializes the paramVal to null. The expression paramVal=rsmd.getColumnName(colIndex+1) is in third part of the for expression, so it is executed after iteration, so the very first iteration uses null value for the paramVal. You need rewrite the code as it just doesn't make sense, and learn how for statement works in Java.

Upvotes: 1

Related Questions