Search code examples
javajtextfield

JTextField from separate class


Can anyone tell me why I keep getting a Null pointer exception for this. I have a JTextField in one class with a set and get method and in my second class i want to assign the JTextField value to a string variable. What am i missing here?

Heres the first class

public class GUI extends JPanel
{

    private final JLabel startLocLabel;
    private final JLabel endLocLabel;
    public String startStringCity1;
    public String endStringCity;

    private final String startLocString = "Enter Starting Location: ";
    private final String endLocString = "Enter Ending Location: ";

    public JTextField startLocTextField = new JTextField(5);
    public JTextField endLocTextField = new JTextField(5);

    private final JButton runButton;

    public GUI(Container pane)
    {
        final JFrame mainFrame = new JFrame("GPS");
        startLocLabel = new JLabel(startLocString);
        endLocLabel = new JLabel(endLocString);
        runButton = new JButton("Run");
        JPanel labelPane = new JPanel();
        labelPane.add(startLocLabel, BorderLayout.LINE_START);
        labelPane.add(startLocTextField, BorderLayout.LINE_END);
        labelPane.add(endLocLabel);
        labelPane.add(endLocTextField);
        labelPane.add(runButton);
        runButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                Graph graph = new Graph();
                graph.Graph();
                setStartCity(startLocTextField.getText());
                setEndCity(endLocTextField.getText());
            }//end actionPerformed
        });//end action listener;
        //Main Frame GUI
        mainFrame.add(labelPane, BorderLayout.PAGE_START);
        mainFrame.pack();
        mainFrame.setVisible(true);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setLocationRelativeTo(null);
    }

    public void setStartCity(String city1)
    {
        startStringCity1 = city1;

    }

    public String getStartCity()
    {
        return startStringCity1;

    }

    public void setEndCity(String endCity)
    {
        endStringCity = endCity;

    }

    public String getEndCity()
    {
        return endStringCity;

    }
}

And the second

public class Graph
{

    public GUI gui;
    private String getStartCity;

    public void Graph()
    {
        getStartCity = gui.getStartCity();
        System.out.println(getStartCity);
    }
}

Here is the stack trace

java.lang.NullPointerException
    at minigps.Graph.Graph(Graph.java:42)
    at minigps.GUI$1.actionPerformed(GUI.java:90)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6525)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3322)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:702)
    at java.awt.EventQueue$3.run(EventQueue.java:696)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:724)
    at java.awt.EventQueue$4.run(EventQueue.java:722)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Solution

  • Based upon the code you posted (which I presume is the full code) in the Graph class:

    getStartCity = gui.getStartCity();
    

    When is gui ever instantiated? A call to the graph method (note, methods should start with lowercase) will result in an NPE. Perhaps you meant to set it explicitly:

            @Override
            public void actionPerformed(ActionEvent e)
            {
                Graph graph = new Graph();
                graph.gui = GUI.this;
                graph.Graph();
                setStartCity(startLocTextField.getText());
                setEndCity(endLocTextField.getText());
            }
    

    That might solve the NPE in the code posted, but there are other issues here, such as

    gui.getStartCity();
    

    The method getStartCity() returns startStringCity1, but at the time it is called this value will be null. Perhaps, as the other answer pointed out, you meant to set it before calling the Graph method (did I mention methods should begin with a lowercase letter?)