-1

While working on a GUI project i am stuck with an error called null pointer error.

I am unable to resolve this problem.

The line which shows the error is closed by double asterisk on both sides(**).(line 80)

I have connected this with a SQL database and error erupts while assigning Jtable its value and structure.

package connectsqlite;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JTable;

import java.sql.*;

import javax.swing.*;

import net.proteanit.sql.DbUtils;

public class afterLogin extends JFrame {

    public JPanel contentPane;
    private JTable table;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    afterLogin frame = new afterLogin();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    Connection connect = null;
    
    
    public afterLogin() {
        connect = sqliteConnection.conn();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 695, 422);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        JButton btnBack = new JButton("Log out");
        btnBack.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                 Connecting window = new Connecting();
                 window.frame.setVisible(true);
                                                 
                
            }
        });
        btnBack.setBounds(10, 62, 114, 23);
        contentPane.add(btnBack);
        
        JButton btnNewButton = new JButton("Load Details");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try{
                    String query = "SELECT * FROM Appointments";
                    PreparedStatement pst = connect.prepareStatement(query);
                    ResultSet rs = pst.executeQuery();
                    **table.setModel(DbUtils.resultSetToTableModel(rs));**
                    
                }
                catch (Exception e1) {
                    e1.printStackTrace();
                }                           
            }
        }); 
        btnNewButton.setBounds(10, 28, 114, 23);
        contentPane.add(btnNewButton);
        
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(162, 57, 435, 305);
        contentPane.add(scrollPane);
        
        JScrollPane scrollPane_1 = new JScrollPane();
        scrollPane.setViewportView(scrollPane_1);
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 3
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Piro Aug 19 '20 at 13:10
  • 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combos](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Aug 20 '20 at 04:34

1 Answers1

2

You haven't instantiated your table, it's only declared. Therefore, when you call setModel on it it's going to cause a NullPointerException.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
JustAnotherDeveloper
  • 2,061
  • 2
  • 10
  • 24