Alex
Alex

Reputation: 4934

Cannot initialise class within ActionListener... please help!

I have a Breadth-First algorithm that scans a graph of nodes.

I am new to UI and Swing. I want the algorithm to run when the OK button is pushed etc. using the text box strings as parameters.

The program will not work becuase it cannot initialise my class.

This code works perfectly in console. I just don't understand UI.

I should add that start.getText() and end.getText() are text boxes where the user enters the start and end stations.

    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);

And for anyone that can help, here is the stack trace:

/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)

Upvotes: 1

Views: 710

Answers (2)

nikhil
nikhil

Reputation: 19

@Alex W : Try using : System.getProperty("user.dir") to get the current directory of the context , then use " ../ " to get the relative path for the location.I think that your using absolute path here is causing nullpointer exception as it could not find that location.

Upvotes: 0

brian_d
brian_d

Reputation: 11385

The issue is NOT with the ActionListener. Your object BreadthFirstShortestPath is not being constructed properly. This is the relevant part of the 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)

I think you are wrong and that the path is not being found. Your command line variables could differ from an IDE environment.

Upvotes: 2

Related Questions