0

Compiles and pops up the panelMain with the MenuBar. I am able to push Edit and go to subMenu where I click Preferences. It comes up with an error stating, "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException". What I am wanting to happen after clicking Preferences is to create a new JFrame which give me an option of choosing three different background colors for mainPanel.

I added a paint drawing of a diagram of what I am trying to createenter image description here

P4App class

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class P4App extends JFrame
{
    P4File fileFrame;
    JPanel panelMain;
    JPanel panelselect;
    public P4App()
    {
        panelMain = new JPanel();
        panelMain.setLayout(new BorderLayout());

        JMenuBar menubar = new JMenuBar();
        JMenu edit = new JMenu("Edit");
        edit.setMnemonic(KeyEvent.VK_E);
        menubar.add(edit);

        JMenuItem pref = new JMenuItem("Preferences");
        pref.setMnemonic(KeyEvent.VK_P);
        edit.add(pref);

        setJMenuBar(menubar);

        this.setSize(400,150);
        this.setLocation(400,200);
        panelMain.setBackground(Color.RED);
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        fileFrame = new P4File(this);
        pref.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    remove(menubar);
                    remove(pref);
                    add(panelselect);
                    revalidate();
                    repaint();
                }
            });
    }

    public static void main(String [] args) {
        new P4App();
    }
}

P4File class

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class P4File extends JFrame

{
    JRadioButton radiored;
    JRadioButton radioblue;
    JRadioButton radiogreen;
    JPanel panelMain;
    JButton buttonok;
    JButton buttoncancel;
    JPanel panelselect;
    JPanel pbuttons;
    P4App parent;
    public P4File(P4App parent)
    {
        super("P4File");
        this.parent = parent;

        ActionHandler actionHandler = new ActionHandler();

        panelselect = new JPanel();
        panelselect.setLayout(new GridLayout(4,1,5,5));

        panelselect.add(radiored = new JRadioButton("Red"));
        panelselect.add(radioblue = new JRadioButton("Blue"));
        panelselect.add(radiogreen = new JRadioButton("Green"));

        ButtonGroup group = new ButtonGroup();
        group.add(radiored);
        group.add(radioblue);
        group.add(radiogreen);

        panelselect.add(radiored);
        panelselect.add(radioblue);
        panelselect.add(radiogreen);

        pbuttons = new JPanel();
        pbuttons.setLayout(new FlowLayout(FlowLayout.RIGHT,2,2));
        pbuttons.add(buttonok = new JButton("OK"));
        pbuttons.add(buttoncancel = new JButton("CANCEL"));

        panelselect.add(buttonok, BorderLayout.SOUTH);
        panelselect.add(buttoncancel, BorderLayout.SOUTH);

        buttoncancel.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    panelMain.setBackground(Color.RED);
                    System.exit(0);
                }
            });
    }

    private class ActionHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            setColor();
        }
    }

    public void setColor() {
        if(radiored.isSelected())
        {
            setBackground(Color.RED);
        }

        if(radiogreen.isSelected())
        {
            setBackground(Color.GREEN);
        }

        if(radioblue.isSelected())
        {
            setBackground(Color.BLUE);
        }
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Tanner10
  • 29
  • 5
  • If you've done a little searching on solving a NullPointerException (NPE), you'll know that the most important bit of information that we need is the exception's associated stacktrace and some identification of the line that causes it, something that the stacktrace will tell you, and unfortunately neither of which you've posted here with your question. Please fix this so that we can help you. – Hovercraft Full Of Eels Sep 18 '15 at 02:06
  • 1
    My "guess" is, `panelselect` in `P4App` is `null` when you try and add it. You may also like to have a look at [How to Use CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html), it will make switching views much easier – MadProgrammer Sep 18 '15 at 02:07
  • Most importantly, you need to learn the general concepts of how to debug a NPE (NullPointerException). **You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully**, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Sep 18 '15 at 02:07
  • I believe this may be what you're looking for? P4App$1.actionPerformed(P4App.java:37) – Tanner10 Sep 18 '15 at 02:08
  • How would I fix the issue regarding panelselec being null MadProg? – Tanner10 Sep 18 '15 at 02:09
  • Create an instance of it – MadProgrammer Sep 18 '15 at 02:18
  • could you elaborate like which class to make an instance of and where? @MadProgrammer – Tanner10 Sep 18 '15 at 02:22
  • I am guessing something along the lines of selectpanel sp = new selectpanel(); – Tanner10 Sep 18 '15 at 02:23
  • I have no idea what your intentions are, all I know is `panelselect` can be a type of `JPanel`. Presumably you want to create an instance of what ever you want to show – MadProgrammer Sep 18 '15 at 02:34
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Sep 18 '15 at 03:54

0 Answers0