0

Yow, I am trying to add a jTable in my jFrame and I wanna display records from my database table to the jTable.

I was able to create a jTable(I put the code in the constructor) and manually inputted in the code the values to be displayed. It worked. But then when I try to call a method that retrieves data from the database, and try to display them inside the jtable. It gives me an error "NullPointerException".

CODE in my constructor that creates the table:

    String[] columnname = {"Subject Code", "Prelim", "Midterm", "SemiFinal", "Finals"};
    setTable();
    Object[][] data = {};

    gradetable = new JTable(data, columnname){
        public boolean isCellEditable(int data, int columns){
            return false;
        }

        public Component prepareRenderer(TableCellRenderer r, int data, int columns){
            Component c = super.prepareRenderer(r, data, columns);

            if (data % 2 == 0){
                c.setBackground(Color.GRAY);
            }
            else{
                c.setBackground(Color.WHITE);
            }

            if (isCellSelected(data, columns)){
                c.setBackground(Color.ORANGE);
            }
            return c;
        }
    };
    gradetable.setPreferredScrollableViewportSize(new Dimension (400, 150));
    gradetable.setFillsViewportHeight(true);
    jsp = new JScrollPane(gradetable);

Method I used to retrieve the data from the database and add them to the table:

private void setTable(){
    DefaultTableModel model = (DefaultTableModel)gradetable.getModel();
    try
    {
        Class.forName("com.mysql.jdbc.Driver");            
        Connection con = (Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/grading","root","");
        Statement st = con.createStatement();      
        String sql = "SELECT * FROM student INNER JOIN grade ON student.idnumber = grade.idnumber WHERE student.idnumber = '2010-00125'";
        ResultSet rs = st.executeQuery(sql);
        model.setRowCount(0);
        while (rs.next()){

            //this.setName(rs.getString("name"));
            //this.setCourse(rs.getString("course"));
            //this.setYear(rs.getString("year"));

            String d1, d2, d4, d3, d5;

            d1 = rs.getString("subjectcode");
            d2 = rs.getString("prelim");
            d3 = rs.getString("midterm");
            d4 = rs.getString("semifinal");
            d5 = rs.getString("finals");

            model.addRow(new Object[]{d1,d2,d3,d4,d5});
        }           
        rs.close();
        con.close();
    }catch(ClassNotFoundException | SQLException ex){
            JOptionPane.showMessageDialog(null, ex);            
    }
}
2
  • 2
    Always post full StackTraces if you have any. Commented Feb 24, 2013 at 11:15
  • 1
    Show the code where you calling setTable() method Commented Feb 24, 2013 at 12:22

1 Answer 1

1

The reason for the NPE is that you are calling setTable which tries to get the JTable model here:

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

This is before you have actually instantiated gradetable, so the NPE is thrown.

Aside from this, you need to use DefaultTableModel when setting the model of the JTable, otherwise you will get a ClassCastException on this line.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.