日期:2014-05-20  浏览次数:20862 次

Java AWT 编程 空指针异常 哪里出错了?
RT
源代码:
Java code
package TEST;

import java.awt.*;
import java.awt.event.*;
import java.text.MessageFormat;

public class Start implements ActionListener{
    TextField user;
    TextField password;
    public Start(){
       Frame f = new Frame("登录界面");
       f.setLayout(new BorderLayout());
       Panel p1 = new Panel();
       p1.setLayout(new GridLayout(2,1));
       p1.add(new Label("用户名:"));
       p1.add(new Label("密码:"));
       Panel p2 = new Panel();
       p2.setLayout(new GridLayout(2,1));
       TextField user = new TextField();
       TextField password = new TextField(10);
       password.setEchoChar('*');
       user.addActionListener(this);
       password.addActionListener(this);
       p2.add(user);
       p2.add(password);
       Panel p3 = new Panel();
       p3.setLayout(new FlowLayout());
       Button submit = new Button("确定");
       Button reset = new Button("重置");
       submit.addActionListener(this);
       reset.addActionListener(this);
       p3.add(submit);
       p3.add(reset);
       f.add(p1,BorderLayout.WEST);
       f.add(p2,BorderLayout.CENTER);   
       f.add(p3,BorderLayout.SOUTH);
       f.add(new Label("请输入你的用户信息:"),BorderLayout.NORTH);
       f.setVisible(true);
       f.setLocation(320,240);
       f.pack();
       f.addWindowListener(new FormAdapter());
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Start st = new Start();
    }
    private class FormAdapter extends WindowAdapter{
        public void windowClosing(WindowEvent e){
            System.exit(0);
        }
    }
    //@Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        String str = e.getActionCommand();
        if(str.equals("确定")){
            System.out.println(str);
            System.out.println(user.getText());
            //System.out.println(password.getText());
        }else if(str.equals("重置")){
            System.out.println(str);
        }
    }
}


Java code
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at TEST.Start.actionPerformed(Start.java:58)
    at java.awt.Button.processActionEvent(Unknown Source)
    at java.awt.Button.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)




------解决方案--------------------
System.out.println(user.getText());
这句报错