当前位置: 代码迷 >> Java相关 >> JAVA登录事件报错!解决方案
  详细解决方案

JAVA登录事件报错!解决方案

热度:7771   发布时间:2013-02-25 21:49:33.0
JAVA登录事件报错!
源代码:

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.util.EventListener;
import java.awt.event.ActionEvent;
import ChatProject.MyMain;

public class MyClient extends JFrame implements ActionListener {

  JTextField username; //声明文本框,用来输入用户名
  JTextField password; //用来输入用户密码

  public MyClient(String title) {
  super(title);
  this.setSize(300, 200);
  this.setLocation(500, 250);
  Container con = this.getContentPane();
  con.setLayout(new GridLayout(2, 1));
  JPanel p1 = new JPanel();  
  JPanel p2 = new JPanel();
  con.add(p1);  
  con.add(p2);
  JLabel l1 = new JLabel("用户名:");
  JLabel l2 = new JLabel("密码:");
  JTextField username = new JTextField(20);
  JTextField password = new JTextField(15);
  JButton bok = new JButton("登录");
  JButton bcancel = new JButton("取消");
  p1.add(l1);
  p1.add(username);
  p1.add(l2);
  p1.add(password);
  p2.add(bok);
  p2.add(bcancel);
  bok.addActionListener(this);
  bcancel.addActionListener(this);

  }

  public static void main(String[] args) {
  MyClient M = new MyClient("登录窗口");
  M.show();
  }

  public void actionPerformed(ActionEvent e) {
  System.out.println(e.getActionCommand()); 
  String uid = username.getText(); 
  String pwd = password.getText();
  if (e.getActionCommand().equals("登录")) 
  {
  if (uid.equals("123") && pwd.equals("123456")) 
  {
  MyMain MM = new MyMain(uid);
  MM.show(); 
  this.hide();  
  }
  } else if (e.getActionCommand().equals("取消")) 
  System.exit(0);
  }
  }
}
运行之后,如下报错,求高手指教!谢谢了!
run:
登录
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  at ChatProject.MyClient.actionPerformed(MyClient.java:52)
  at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
  at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
  at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
  at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
  at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
  at java.awt.Component.processMouseEvent(Component.java:6263)
  at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
  at java.awt.Component.processEvent(Component.java:6028)
  at java.awt.Container.processEvent(Container.java:2041)
  at java.awt.Component.dispatchEventImpl(Component.java:4630)
  at java.awt.Container.dispatchEventImpl(Container.java:2099)
  at java.awt.Component.dispatchEvent(Component.java:4460)
  at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
  at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
  at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
  at java.awt.Container.dispatchEventImpl(Container.java:2085)
  相关解决方案