importjava.awt.event.*;importjava.awt.*;importjavax.swing.*;publicclasshelloextendsFrame{publicFrameindexf;publicButtonok,cancel;publicTextFieldip;publicstaticvoidmain(St...
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class hello extends Frame
{
public Frame indexf;
public Button ok,cancel;
public TextField ip;
public static void main(String args[])
{
final test t=new test();
Frame indexf=new Frame("start");
indexf.setLayout(new FlowLayout());
TextField ip=new TextField(5);
Button ok=new Button("ok");
Button cancel=new Button("cancel");
indexf.add(ip);
indexf.add(ok);
indexf.add(cancel);
indexf.setVisible(true);
indexf.setSize(200,200);
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(t.checkip()==0)
JOptionPane.showMessageDialog(null,"error!","Information",JOptionPane.INFORMATION_MESSAGE);
}
});
在这里想检测文本框里面输入的内容,对按钮ok添加了监听接口类,对文本框进行检测,如果文本框里输入的信息为空,则弹出信息框提示出错。
编译没问题,但是java hello运行时出错。错误信息如下:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at test.checkip(test.java:43)
at hello$1.actionPerformed(hello.java:26)
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.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)
不懂什么意思,怎么解决啊,对不起诸位了,因为才申请的百度号,积分都问问题用完了,不能给诸位加分了。在这里谢谢大家的回答!
(我是java菜鸟,我知道,所以请不要再说我是菜。呵呵)
展开