如何解决空指针异常

huangapple go评论58阅读模式
英文:

How to resolve the nullpointer exception

问题

在进行 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);
    }
}
英文:

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

答案1

得分: 2

你还没有实例化你的表格,只是声明了而已。因此,当你在其上调用 setModel 时,会导致 NullPointerException

英文:

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

huangapple
  • 本文由 发表于 2020年8月19日 21:07:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63487669.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定