如何解决nullpointer异常

jslywgbw  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(386)

这个问题在这里已经有答案了

什么是nullpointerexception,如何修复它(12个答案)
5个月前关门了。
在处理gui项目时,我遇到了一个称为空指针错误的错误。
我无法解决这个问题。
显示错误的行两边用双星号(**)闭合。(第80行)
我将其与一个sql数据库连接起来,在为jtable赋值和结构时会出现错误。

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);
    }
}
1l5u6lss

1l5u6lss1#

您还没有示例化表,它只是声明的。因此,当你打电话 setModel 它会引起 NullPointerException .

相关问题