免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3750 | 回复: 2
打印 上一主题 下一主题

内部类访问外部类的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-09-23 23:09 |只看该作者 |倒序浏览
测试代码:

package com.test.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class TestTimer extends JFrame implements ActionListener {
    private static final long serialVersionUID = -7533926242538206319L;

    Timer timer;

    private JTextField send;
    private JTextArea reply;

    public TestTimer() {
        super("Test Send");
        setSize(300, 300);

        timer = new Timer();
        timer.schedule(new ScheduleRunTask(), 0, 1 * 60000);

        send = new JTextField();

        getContentPane().add(send, "South");
        send.addActionListener(this);

        reply = new JTextArea(8, 40);
        reply.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(reply);
        getContentPane().add(scrollPane, "Center");

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }

    public void stop() {
        timer.cancel();
    }

    class ScheduleRunTask extends TimerTask {
        public void runCheckAutobahnFXAPITask() {
            TestTimer.this.reply.append("Test\n");
        }

        public void run() {
            System.out.println("run()");
            runCheckAutobahnFXAPITask();
        }
    }

    public static void main(String[] args) {
        JFrame frame = new TestTimer();
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == send) {
            reply.append(send.getText() + "\n");
            send.setText("");
        }
    }

    public JTextArea getReply() {
        return reply;
    }

    public void setReply(JTextArea reply) {
        this.reply = reply;
    }
}




运行结果:
run()
Exception in thread "Timer-0" java.lang.NullPointerException
        at com.test.gui.TestTimer$ScheduleRunTask.runCheckAutobahnFXAPITask(TestTimer.java:55)
        at com.test.gui.TestTimer$ScheduleRunTask.run(TestTimer.java:60)
        at java.util.TimerThread.mainLoop(Unknown Source)
        at java.util.TimerThread.run(Unknown Source)


毛病出在这一行:
TestTimer.this.reply.append("Test\n");

而这个是可以直接使用外部类的成员的:

package com.test.general;
class Outer {
    private int index = 100;

    class Inner {
        private int index = 50;

        void print() {
            int index = 30;
            System.out.println(index); // 30

            System.out.println(this.index); // 50

            System.out.println(Outer.this.index); // 100

        }
    }
    void print() {
        index = 101;
        Inner inner = new Inner();
        inner.print();
    }
    public static void main(String[] args) {
        Outer outer = new Outer();
        outer.print();
    }
}


请问哪位给些指点?

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
2 [报告]
发表于 2008-09-24 00:22 |只看该作者
你的TestTimer构造函数中先调用timer.schedule(new ScheduleRunTask(), 0, 1 * 60000);
意思是马上执行任务(第一个参数为0),所以执行了内部类的run,也就执行了你的runCheckAutobahnFXAPITask方法,这里面用到的reply是外部类的成员,还在构造函数后面没初始化呢,当然是空指针异常了,所以你把0改成10就不会有异常了,只要不马上执行任务就会先初始化了。

论坛徽章:
0
3 [报告]
发表于 2008-09-24 12:44 |只看该作者
的确是初始化问题.
原来我还以为有很高深莫测的原因使得 GUI 下面的东西无法访问, 弄到底还是基本问题, 谢谢大天使的思路和启发.

delay设为 10 的话我这边还是出现空指针异常. 改为10000就OK了, 可能是我的电脑没那么快吧.

我认为更稳妥的办法是把 Timer 的定义放在外部类初始化代码的最后.

[ 本帖最后由 ghostsaint 于 2008-9-24 12:47 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP