求解 关于 初学者 出现空指针问题 javastring 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我发现是在b.addActionListener(new Text());中要将 new Text() 改为 this 但是为什么呢 this代表当前对象,你的是new Text(),是每次调用时都生成一个新的对象;还有你关闭时应该不能正常关闭吧;添加一段代码,关闭了吧;public class Text extends Frame implements ActionListener{ TextField t1; Button b; public void launchFram() { setBounds(100, 50, 100, 100); setLayout(new FlowLayout()); t1 = new TextField(10); b = new Button("登陆"); add(t1); add(b); b.addActionListener(this); setVisible(true); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void actionPerformed(ActionEvent e) { String s = t1.getText(); // 为什么这会出现空指针错误 System.out.println(s); } public static void main(String[] args) { new Text().launchFram(); } b.addActionListener(new Text());这个 new 出来的 Text 对象的 t1 没有值,null 在 TextField 中要自己写点东西 new了一个新的test之后里面对象为空 你添加的是另外一个text,当你点击以后,处理事件的是那个对象里面的方法,那里面的launch方法没有执行过,当然有空指针了 你没有new 一个t1出来,空指针异常通常是没有赋值就直接使用造成的(同样初学者飘过~) 如何在java中GUI画图程序中显示鼠标移动轨迹 正规表达式,如何解决 一个运行不起来的BBS显示程序,请帮忙给找找问题 请问如何实现自定义事件? JAVA 主要用来开发什么样类型的程序比较多? 大数据量如何解决(在线等) ~请问哪里有SWT Designer的注册码? 急,求帮助 关于JScrollPane自动向下滚动的问题 关于字符串的问题还是没搞明白!please help me! 大家来讨论下java中的值传递和引用传递~ 如何提高java读取大文本文件的效率?
要将 new Text() 改为 this
但是为什么呢
还有你关闭时应该不能正常关闭吧;
添加一段代码,关闭了吧;
public class Text extends Frame implements ActionListener
{ TextField t1;
Button b; public void launchFram()
{
setBounds(100, 50, 100, 100);
setLayout(new FlowLayout()); t1 = new TextField(10);
b = new Button("登陆");
add(t1);
add(b);
b.addActionListener(this);
setVisible(true); this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}); } public void actionPerformed(ActionEvent e)
{ String s = t1.getText(); // 为什么这会出现空指针错误
System.out.println(s); } public static void main(String[] args)
{ new Text().launchFram(); }
这个 new 出来的 Text 对象的 t1 没有值,null