无法在ActionListener中初始化类...请帮忙!

时间:2011-01-14 18:55:56

标签: java swing

我有一个广度优先算法,用于扫描节点图。

我是UI和Swing的新手。我希望算法在按下OK按钮等时使用文本框字符串作为参数运行。

该程序无法正常工作,因为它无法初始化我的课程。

此代码在控制台中完美运行。我只是不懂UI。

我应该补充一点,start.getText()和end.getText()是用户输入起点和终点的文本框。

    JButton okButton = new JButton("Get Route...");

    okButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            final BreadthFirstShortestPath init = new BreadthFirstShortestPath("/Users/wakemana/Documents/GC02 Java/Tube Stations List/station_names.txt"
                    , "/Users/wakemana/Documents/GC02 Java/Tube Stations List/tube_edges.txt");

            init.breadthFirst(start.getText(), end.getText());

            ArrayList<String> path = init.getPath();

            for (String station : path) {

                System.out.println(station);

            }

        }
    });
    buttonPane.add(okButton);

对于任何可以提供帮助的人来说,这里是堆栈跟踪:

/Users/wakemana/Documents/GC02 Java/Tube Stations List/station_names.txt
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.io.File.<init>(File.java:222)
    at alex.graph.breadthfirst.StationGraph.<init>(StationGraph.java:19)
    at alex.graph.breadthfirst.BreadthFirstShortestPath.<init>(BreadthFirstShortestPath.java:22)
    at alex.graph.breadthfirst.Main$1.actionPerformed(Main.java:103)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
    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:6352)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6117)
    at java.awt.Container.processEvent(Container.java:2085)
    at java.awt.Component.dispatchEventImpl(Component.java:4714)
    at java.awt.Container.dispatchEventImpl(Container.java:2143)
    at java.awt.Component.dispatchEvent(Component.java:4544)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4621)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4282)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4212)
    at java.awt.Container.dispatchEventImpl(Container.java:2129)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4544)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:635)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

2 个答案:

答案 0 :(得分:2)

问题不在于ActionListener。您的对象BreadthFirstShortestPath未正确构建。这是stacktrace的相关部分:

/Users/wakemana/Documents/GC02 Java/Tube Stations List/station_names.txt
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.io.File.<init>(File.java:222)
    at alex.graph.breadthfirst.StationGraph.<init>(StationGraph.java:19)
    at alex.graph.breadthfirst.BreadthFirstShortestPath.<init>(BreadthFirstShortestPath.java:22)
    at alex.graph.breadthfirst.Main$1.actionPerformed(Main.java:103)

我认为你错了,找不到路径。您的命令行变量可能与IDE环境不同。

答案 1 :(得分:0)

@ Alex W:尝试使用: System.getProperty(“user.dir”)来获取上下文的当前目录,然后使用“../”得到该位置的相对路径。我认为你在这里使用绝对路径会导致nullpointer异常,因为它无法找到该位置。