无法解决 Java Swing 应用程序中的 NullPointerException
Posted
技术标签:
【中文标题】无法解决 Java Swing 应用程序中的 NullPointerException【英文标题】:Unable to resolve NullPointerException in java swing application 【发布时间】:2022-01-10 10:57:45 【问题描述】:我在 Eclipse 中使用 Java Swing 制作了一个简单的登录页面,一旦我运行并输入输入,我就会收到错误 java.lang.NullPointerException
import java.lang.String;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class trail extends JFrame
Connection connection =null;
private JPanel contentPane;
private JTextField id;
private JPasswordField passwordField;
public static void main(String[] args)
String jdbcURL = "jdbc:postgresql://localhost:5432/postgres";
String username = "postgres";
String password = ".......";
try
Connection connection = DriverManager.getConnection(jdbcURL, username, password);
System.out.print("Connected");
connection.close();
catch(SQLException e)
System.out.println("Error in connection");
e.printStackTrace();
EventQueue.invokeLater(new Runnable()
public void run()
try
trail frame = new trail();
frame.setVisible(true);
catch (Exception e)
e.printStackTrace();
);
/**
* Create the frame.
*/
public trail()
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 519, 550);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("username");
lblNewLabel.setBounds(94, 114, 45, 13);
contentPane.add(lblNewLabel);
id = new JTextField();
id.setBounds(246, 111, 96, 19);
contentPane.add(id);
id.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("password");
lblNewLabel_1.setBounds(94, 178, 45, 13);
contentPane.add(lblNewLabel_1);
passwordField = new JPasswordField();
passwordField.setBounds(246, 175, 96, 19);
contentPane.add(passwordField);
JButton btnlogin = new JButton("login");
btnlogin.addActionListener(new ActionListener()
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e)
try
String query = "select * from users where username = ? and password = ?";
PreparedStatement pst=connection.prepareStatement(query);
pst.setString(1, id.getText());
pst.setString(2, passwordField.getText());
ResultSet rs = pst.executeQuery();
int count = 0;
while(rs.next())
count = count + 1;
if(count == 1)
JOptionPane.showMessageDialog(null, "Password and username is correct");
else if(count>1)
JOptionPane.showMessageDialog(null, "redundancy detected");
else
JOptionPane.showMessageDialog(null,"Incorrect credentials");
rs.close();
pst.close();
catch(Exception e1)
JOptionPane.showMessageDialog(null, e1);
);
btnlogin.setBounds(179, 259, 85, 21);
contentPane.add(btnlogin);
【问题讨论】:
查看图片下导致异常的代码会很有帮助。编辑您的帖子并将代码复制粘贴到其中。在执行查询之前,您需要建立与数据库的连接。 数据库连接建立成功!!! @DevilsHnd 为什么一打开就关闭那个连接!!!! 我改变了它知道...仍然是相同的 nullpointe 异常错误@DevilsHnd 好吧,错误被底部的catch(e1)捕获了……所以在btnlogin底部@DevilsHnd的actionlistener下方的try块中可能出了问题 【参考方案1】:你稍后在你使用的 try 块中声明了Connection connection =null;
Connection connection = DriverManager.getConnection(jdbcURL, username, password)
是完全不同的 Connection
对象。你应该使用类似的东西。
this.connection=DriverManager.getConnection(jdbcURL, username, password)
你也可以用不同的方法移动你的代码,比如
public void initialize()
String jdbcURL = "jdbc:postgresql://localhost:5432/your_db";
String username = "postgres";
String password = "password";
try
this.connection = DriverManager.getConnection(jdbcURL, username, password);
System.out.print("Connected");
connection.close();
catch(SQLException e)
System.out.println("Error in connection");
e.printStackTrace();
并在构造函数内部调用
public trail()
initialize();
....
【讨论】:
以上是关于无法解决 Java Swing 应用程序中的 NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章
Java中Swing组件中的JTextArea,JList控件中的滚动条问题?帮忙解决!
在全屏模式下运行应用程序时,Java Swing 无法找出 JPanel 的问题