2011-01-14 40 views
1

我有一個可以掃描節點圖的廣度優先算法。無法在ActionListener中初始化類...請幫助!

我是UI和Swing的新手。我希望算法在按下OK按鈕等時使用文本框字符串作爲參數運行。

由於程序無法初始化我的課程,因此該程序無法正常工作。

此代碼在控制檯中完美工作。我只是不明白用戶界面。

我應該添加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) 
+1

你得到了什麼錯誤信息? – 2011-01-14 18:59:41

+0

已更新...請注意:絕對不是文件名。 – Alex 2011-01-14 19:00:29

回答

2

的問題是不與ActionListener。您的對象BreadthFirstShortestPath未正確構建。這是堆棧跟蹤的相關部分:

/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環境不同。

0

@Alex寬:嘗試使用:System.getProperty(「user.dir來」)來獲取上下文的當前目錄下,然後用「../」得到相對路徑爲location.I認爲你在這裏使用絕對路徑導致空指針異常,因爲它無法找到該位置。

相關問題