1

I can not seem to spot where I get NullPointerException Error when call the method fillTable(). fillTable is called from the button what takes in String from a textField. fillTable is meant to populate a JTable by retrieving Object[][] containing data from a handler class.

private void fillTable(String dataType, String searchBy)
    {
        String[] columnName = {"module name", "Workshop %", "Lecture %", "Tutorial %", "Average %"};
        if(searchBy.startsWith("@"))
        {
            tableModel.setDataVector(pLSHand.gIStudData(dataType, searchBy), columnName);
            table.setModel(tableModel);
        }
        else
        {
            tableModel.setDataVector(pLSHand.gNStudData(dataType, searchBy), columnName);
            table.setModel(tableModel); 
        }
        table.repaint();
    }

Button:

String searchBy = (String)textField.getText();
if(!searchBy.isEmpty())
{
   fillTable("Year",searchBy);
}
else
{

}

Method in the Handler class

public Object[][] gIStudData(String dType, String studID)
    {

        String rQuery = "SELECT * FROM StudAttYear WHERE studentID = '"+ studID +"'";
        data = new Object[1][5];

        if(dType.equalsIgnoreCase("Semester"))
        {
            rQuery = "SELECT * FROM StudAttSem WHERE studentID = '"+ studID +"'";
            data = new Object[2][5];
        }
        else if(dType.equalsIgnoreCase("Year"))
        {
            rQuery = "SELECT * FROM StudAttYear WHERE studentID = '"+ studID +"'";
            data = new Object[1][5];
        }
        else if(dType.equalsIgnoreCase("Week"))
        {
            rQuery = "SELECT * FROM StudAttWeek WHERE studentID = '"+ studID +"' AND number = '"+ weekNo +"'";
            data = new Object[12][5];
        }

        try{
               Class.forName(DRIVER_CLASS);

                connection = DriverManager.getConnection( url,"agile", "adila");
                statement = connection.createStatement();
                results = statement.executeQuery(rQuery);
                int i=0;
                while(results.next())
                {
                     data[i][0] = results.getString("module");
                     data[i][1] = results.getString("workshop%");
                     data[i][2] = results.getString("tutroial%");
                     data[i][3] = results.getString("lecture%");
                     data[i][4] = results.getString("avg%");
                 i++;
                }
           results.close();
           statement.close();
           connection.close();
           } catch (SQLException sqlException) {
           sqlException.printStackTrace();
           System.exit(1);
           }catch(Exception exception) {
           System.err.println("An error happened");
           System.exit(1);
         }
        return data;
    }

Using the same technique in other classes and they seem to work.

durron597
  • 30,764
  • 16
  • 92
  • 150

1 Answers1

1

As stated, pLSHand is not declared elsewhere in your code.

durron597
  • 30,764
  • 16
  • 92
  • 150