来自单独类的JTextField

时间:2015-04-24 22:23:13

标签: java jtextfield

任何人都可以告诉我为什么我一直为此获得Null指针异常。我在一个类中有一个带有set和get方法的JTextField,在我的第二个类中,我想将JTextField值赋给一个字符串变量。我在这里缺少什么?

继承第一堂课

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;

    }
}

第二个

public class Graph
{

    public GUI gui;
    private String getStartCity;

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

这是堆栈跟踪

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)

3 个答案:

答案 0 :(得分:1)

基于您在Graph类中发布的代码(我认为是完整代码):

getStartCity = gui.getStartCity();

gui何时被实例化?对图形方法的调用(注意,方法应以小写字母开头)将导致NPE。也许你打算明确地设置它:

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

这可能会解决发布的代码中的NPE,但此处还有其他问题,例如

gui.getStartCity();

方法getStartCity()返回startStringCity1,但在调用它时,该值将为null。也许,正如另一个答案指出的那样,你打算在调用Graph方法之前设置它(我提到的方法应该以小写字母开头吗?)

答案 1 :(得分:0)

问题是您在创建图表后设置了城市名称。变化:

def parse_node(self, response, selector):
    item = CrawlerItem()
    item['to'] = selector.xpath('//to/text()').extract()
    item['who'] = selector.xpath('//from/text()').extract()
    item['heading'] = selector.xpath('//heading/text()').extract()
    item['body'] = selector.xpath('//body/text()').extract()
    return item

为:

Graph graph = new Graph();
graph.Graph();
setStartCity(startLocTextField.getText());
setEndCity(endLocTextField.getText());

根本原因是方法setStartCity(startLocTextField.getText()); setEndCity(endLocTextField.getText()); Graph graph = new Graph(); graph.Graph(); 在通过Graph方法分配之前读取字符串的值:

setStartCity

执行public void Graph() { getStartCity = gui.getStartCity(); 时,getStartCity = gui.getStartCity();的值仍为空。

答案 2 :(得分:0)

这不是更好的方法,但运作良好。 :)。

课程主要内容:

    public class main {

    private static GUI gui;
    private static Graph graph;

    public static void main(String[] args) {
        gui = new GUI();
        graph = new Graph(gui);
        gui.addActionListner(graph);
        gui.setVisible(true);
    }

}

类Graph

    public class Graph
{

    private String getStartCity;
    private final GUI gui;

    public Graph(GUI gui)
    {
        this.gui = gui;
    }

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

类GUI

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

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

        // 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 addActionListner(final Graph graph) {
        runButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                setStartCity(startLocTextField.getText());
                setEndCity(endLocTextField.getText());
                graph.start();

            }// end actionPerformed
        });
    }

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

    }

    public String getStartCity()
    {
        return startStringCity1;

    }

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

    }

    public String getEndCity()
    {
        return endStringCity;

    }
}