Sandul Renuja
Sandul Renuja

Reputation: 1

Java Null Pointer Exception when trying to add rows to a DefaultTableModel model

I am trying to query a database using a user input from a text field and display the results in a JTable. I can display all results in DB (without using a search) without error but when using a text to search I get the Null Pointer Exception.

public static void loadServiceTable(JTable tblService,String searchData,String searchField){       
    try {
        //get tblItem model
        DefaultTableModel model=(DefaultTableModel) tblService.getModel();
        //remove all rows
        model.getDataVector().removeAllElements();
        System.out.println("Rows cleared");
        //create service array from services in DB according to search
        Service[] serviceArray=ServiceController.getAllService(searchData,searchField);
        //add one row each for service in array
        System.out.println("Service array success, length = "+serviceArray.length);
        for(Service s:serviceArray){
            model.addRow(new Object[]{
                s.getServiceID(),
                //error
                s.getName(),
                s.getDetail(),
                s.getPrice(),  
            });
        }
        tblService.repaint();
        System.out.println("Repaint finished");


    } catch (ClassNotFoundException ex) {
        Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
    }   
    }

/*(edited) This is the output.

"After getting table model
After getting service arr, arr length = 1
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at lk.ijse.ssc.view.MainApp.loadServiceTable(MainApp.java:2800)"*/

I am using the following method for displaying all data (without a search) and it works properly.

public static void loadServiceTable(JTable tblService){       
    try {

        DefaultTableModel model=(DefaultTableModel) tblService.getModel();

        model.getDataVector().removeAllElements();

        Service[] serviceArray=ServiceController.getAllService();

        for(Service s:serviceArray){
            model.addRow(new Object[]{
                s.getServiceID(),
                s.getName(),
                s.getDetail(),
                s.getPrice(),


            });
        }
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
    }   
}

The getAllService(searchData,searchField) method is given below.

public static Service[] getAllService(String searchData,String searchField) throws ClassNotFoundException, SQLException{
        //establish DB connection
        Connection con=DBConnection.getInstance().getConnection();
        //get no of services in DB and create array
        Service[] serviceArray=new Service[getRowCount(searchData,searchField)];
        //SQL statement to load matching services info from DB
        PreparedStatement pstm;


        if(searchField.equals("serviceID")) pstm=con.prepareStatement("SELECT service_id,name,detail,price FROM service WHERE service_id=?;");

        else pstm=con.prepareStatement("SELECT service_id,name,detail,price FROM service WHERE name=?;");

        pstm.setString(1,"%"+searchData+"%");
        ResultSet set=pstm.executeQuery();
        //create Item models for each record in DB and store them in Item array
        int index=0;
        while(set.next()){
            String serviceID=set.getString(1);
            String name=set.getString(2);
            String detail=set.getString(3);
            Double price=set.getDouble(4);

            serviceArray[index]=new Service(serviceID,name,detail,price);
            index++;

        }
        return serviceArray;
}

I am quite new to Java and this is my third program using Java Swing and MVC architecture. Any help is much appreciated.

Upvotes: 0

Views: 22

Answers (0)

Related Questions