0

I've got a class with a JTextField as a instance variable, that I pass a reference to in the class's constructor.

Problem is: I keep getting a NullPointerException if I try to getText() or setText(). I fee like I passed it properly but I'm obviously doing something wrong. Anyone have any idea what I should do to fix it?

code below:

public class GraphicCalculator implements Runnable
{
    private JFrame frame;

    @Override
    public void run()
    {
        frame = new JFrame("Calculator");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        createComponents(frame.getContentPane());

        frame.pack();
        frame.setVisible(true);
    }

    private void createComponents(Container container)
    {
        GridLayout gl = new GridLayout(3,1);
        container.setLayout(gl);

        JTextField out = new JTextField();
        out.setEnabled(false);
        JTextField in = new JTextField("0");

        container.add(out);
        container.add(in);
        container.add(new ButtonPanel(in, out));

    }

    public JFrame getFrame()
    {
        return frame;
    }
}    

Problems Start down here:

public class ButtonPanel extends JPanel
{
    private JTextField in;
    private JTextField out;

    public ButtonPanel(JTextField input, JTextField output)
    {
        super(new GridLayout(1, 3));
        this.createComponents();

        this.in = input;
        this.out = output;


    }

    public void createComponents()
    {
        JButton addition = new JButton("+");
        JButton sub = new JButton("-");
        JButton zero = new JButton("Z");


        System.out.println(this.in.getText());
        String s = this.out.getText();



        add(addition);
        add(sub);
        add(zero);
    }

}

Output:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ButtonPanel.createComponents(ButtonPanel.java:33)
    at ButtonPanel.<init>(ButtonPanel.java:12)
    at GraphicCalculator.createComponents(GraphicCalculator.java:32)
    at GraphicCalculator.run(GraphicCalculator.java:15)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    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)
  • What is line 33 of ButtonPanel.java? – Code-Apprentice Mar 23 '18 at 18:34
  • 4
    You call `createComponents()` _before_ `in` and `out` are set, but use them in `createComponents()`. Of course they are `null`. – Jim Garrison Mar 23 '18 at 18:35
  • I have several suggestions to help you figure out the problem: 1. Learn to debug your own code. [This article](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) has some great tips about how to start. 2. When you ask a question, include a [mcve]. What you have here is a good start. I think the biggest thing that is missing is a `main()` method so that we can run your code for ourselves. – Code-Apprentice Mar 23 '18 at 18:38
  • Oh god, I'm an idiot...thanks Jim Garrison... that makes a lot of sense – Kevin Wakatama Mar 23 '18 at 18:45
  • I'll give those a read Code-Apprentice (and hopefully get less shit at this) – Kevin Wakatama Mar 23 '18 at 18:47

0 Answers0