Joey Clark
Joey Clark

Reputation: 69

Can someone tell me my error with swing in java?

I'm having some problems with my swing GUI file in that I keep getting an error, I used oracle's tutorial for reference but, no matter what i keep getting the same error.

here's the error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Calculator.CalcGUI.CalGUI(CalcGUI.java:29)
at Calculator.CalcGUI.access$0(CalcGUI.java:27)
at Calculator.CalcGUI$1.run(CalcGUI.java:74)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

and here is my GUI code keep in mind, it's a little messy because I was trying everything I could to fix it.

package Calculator;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;



public class CalcGUI implements ActionListener {

    public static boolean geometry = false;
    public static boolean is2D = false;
    public static boolean is3D = false;
    static boolean working = false;
    static JFrame cfrm;
    static JButton cb1;
    static JButton cb2;
    static JButton cb3;
    static JLabel cl1;
    static JLabel cl2;
    static JTextField ctf1;
    static JTextField ctf2;
    static JTextField ctf3;
    static ActionListener a, b, c, d, e, f;

    private static void CalGUI() {

    cfrm.setLayout(new FlowLayout());
    cfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    cfrm.setSize(300, 600);

    cb1 = new JButton();
    cb2 = new JButton();
    cb3 = new JButton();

    do {
        if(geometry && is2D != true) {

            cfrm.removeAll();   
            cfrm.add(cb1);
            cb1.setText("geometry");
            cb1.addActionListener(a);

        }

        else if(geometry = true && is2D != true) {

            cfrm.removeAll();
            cfrm.add(cb1);
            cfrm.add(cb2);
            cb1.setText("2D Shape");
            cb2.setText("3D Shape");
            cb1.addActionListener(b);
            cb2.addActionListener(c);

        }
    }
    while(working = false);


}

@Override
public void actionPerformed(ActionEvent arg0) {

}


    public static void main(String args[]) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
               CalGUI();
            }
        });
    }

}

Upvotes: 0

Views: 109

Answers (2)

kosa
kosa

Reputation: 66637

cfrm is null, so you are getting NullPointerException. Instantiate like below before peforming any set... method calls.

cfrm = new JFrame();
cfrm.setLayout(new FlowLayout());
cfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cfrm.setSize(300, 600);

Upvotes: 3

Chris Dargis
Chris Dargis

Reputation: 6043

You have to instantiate cfrm before you start accessing its data members or functions:

cfrm = new JFrame();
cfrm.setLayout(new FlowLayout());
//...

Also, why is your constructor private and static?

Upvotes: 2

Related Questions