如何处理空指针异常"Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException"
package GUI;import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextComponent;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.WindowConstants;public class JFrame02 extends Frame{private static TextField text_user;private static TextField text_pass;public void Jframe() {//设定窗体容器JFrame frame = new JFrame();frame.setTitle("社团成员登录界面"); //容器文字frame.setSize(350,120); //容器大小frame.setResizable(false); // 设置禁止调整窗体大小frame.setLocationRelativeTo(null); // 设置窗体相对于另一个组件的居中位置,frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //容器关闭frame.setLocationRelativeTo(null); //居中显示//使用流式布局,对齐方式居中,组件间隔为10FlowLayout fl = new FlowLayout(FlowLayout.CENTER,10,10);frame.setLayout(fl);//创建标签对象,该对象显示为用户名/密码,并将其添加到窗体上JLabel lbUser = new JLabel("用户名:");frame.add(lbUser);//创建用户名文本框TextField text_name = new TextField();//Dimension 类封装单个对象中组件的宽度和高度(精确到整数)Dimension dim1 = new Dimension(100,20);text_name.setPreferredSize(dim1); //设置除顶级容器组件其他组件的大小frame.add(text_name); //添加到容器JLabel lbPass = new JLabel("密码:");frame.add(lbPass);//创建密码文本框JPasswordField text_pass = new JPasswordField();text_pass.setPreferredSize(dim1);frame.add(text_pass);//设置登录注册按钮JButton btn_Up = new JButton();btn_Up.setText("登录"); //调用setText()方法在按钮上显示登录//添加监听事件btn_Up.addActionListener((new ActionListener() {public void actionPerformed(ActionEvent e) {btn_UpActionPerformed(e);}}));frame.add(btn_Up);JButton btn_Sign = new JButton();btn_Sign.setText("注册"); //调用setText()方法在按钮上显示注册frame.add(btn_Sign);//使窗体可见frame.setVisible(true);}public void btn_UpActionPerformed(ActionEvent e) {String user = new String(text_user.getText());String pass = new String(text_pass.getText());if(user.equalsIgnoreCase("admin")) {if(pass.equalsIgnoreCase("0000")) {new Success();}}else {JOptionPane.showMessageDialog(null, "用户名不存在");return;}}public static void main(String[] args) {new JFrame02().Jframe();}
}
做了一个社团管理界面,当我添加监听事件,运行时爆出"Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException",具体到代码句“btn_UpActionPerformed(e);”,之间也在网上搜过相关问题,主要还是没有实例化对象,检查代码后依然无法解决,求助各位大佬,,有没有解决方案