Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7434037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:46:09+00:00 2026-05-29T09:46:09+00:00

This is driving me insane. I’m trying to change the panels when you click

  • 0

This is driving me insane. I’m trying to change the panels when you click on 3 different buttons and it works, but for one panel – only once.

If you click addPerson – the Person panel shows,
If you then click addCD – the CD panel shows; (same for view store)
If you then click on addPerson – it doesn’t work. It throws a nullpointer exception.
Even if you click on addCD/viewstore and THEN add Person it shows but it just won’t show a second time.

In a test file, I created a GUI with an add and remove: if I clicked add it threw the null pointer exception but if I just added it already and compiled, it was fine…

/* PERSON PANEL */
public JPanel create_PersonPnl()
{
    personPnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
    personPnl.setBackground(Color.WHITE);
    personPnl.setPreferredSize(minPnl);

    /* VERTICAL BOX for Person boxes */
    Box personBox = Box.createVerticalBox();
    personBox.setBorder(new TitledBorder(new LineBorder(Color.DARK_GRAY), "Person"));

        /* Horizontal Box for Name Lbl & TF */
        Box nameBox = Box.createHorizontalBox();
            nameBox.add(Box.createHorizontalStrut(10));
            nameLbl = new JLabel("Name: ");
            nameBox.add(nameLbl);
            nameBox.add(Box.createHorizontalStrut(5));
            nameTF = new JTextField();
            nameBox.add(nameTF);
            nameBox.add(Box.createHorizontalStrut(10));

        personBox.add(nameBox);
        personBox.add(Box.createVerticalStrut(10));
        Box limitBox = Box.createHorizontalBox();
            limitBox.add(Box.createHorizontalStrut(10));
            limitLbl = new JLabel("CD Limit: ");
            limitBox.add(limitLbl);
            limitTF = new JFormattedTextField();
            limitBox.add(limitTF);
            limitBox.add(Box.createHorizontalStrut(10));
        personBox.add(limitBox);
        personBox.add(Box.createVerticalStrut(10));
        personBox.add(addPersonBtn = new JButton("Add Person"));
        personBox.add(Box.createVerticalStrut(10));
            personList = new JList(temp);
            personList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            scrollp = new JScrollPane(personList);
                scrollp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                scrollp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            scrollp.setSize(100, 45);
            personBox.add(scrollp);
            personBox.add(Box.createVerticalStrut(10));
        Box optionsBox = Box.createHorizontalBox();
            editPrsnBtn = new JButton("Edit");
            editPrsnBtn.addActionListener(this);
            optionsBox.add(editPrsnBtn);
            removePrsnBtn = new JButton("Remove");
            removePrsnBtn.addActionListener(this);
            optionsBox.add(removePrsnBtn);
        personBox.add(optionsBox);

    personPnl.add(personBox);
    return personPnl;
}

This is what is in my ActionPerformed method.

if(e.getSource() == addPersonBtn)
    {
        changePnl.removeAll();
        changePnl.add(create_PersonPnl());
        changePnl.revalidate();
        System.out.println("PersonPnl added");
    }

    if(e.getSource() == addCDBtn)
    {
        changePnl.removeAll();
        changePnl.add(create_CDPnl());
        changePnl.revalidate();
    }

    if(e.getSource() == viewStoreBtn)
    {
        changePnl.removeAll();
        changePnl.add(create_StorePnl());
        changePnl.revalidate();
    }

Only code for ChangePnl.

  changePnl = new JPanel();
  changePnl.setBackground(Color.WHITE);
defaultPnl = new JPanel();
defaultPnl.setBackground(Color.WHITE);
defaultPnl.add(new JLabel("Welcome to the CD Store"));
defaultPnl.add(new JLabel("Click an option from the left"));
changePnl.add(defaultPnl);

The S.O.P was to debug to see what was being run.. and that only prints once and that’s it unless I take out .add(create_PersonPnl()); so I’ve narrowed it but I don’t have a clue since it works the first time.

Thank in advance!

Separate test file to prove it’s create_PersonPnl()

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

public class Test implements ActionListener
{
JButton add, remove;
JButton addPersonBtn, editPrsnBtn, removePrsnBtn;
JFrame frame;
JFormattedTextField limitTF;
JLabel nameLbl, limitLbl;
JList personList;
JPanel  TotalGUI, welcomePnl, mainPnl, imagePnl, changePnl, defaultPnl, cdPnl,       storePnl;
JPanel personPnl;
JScrollPane scrollp;
JTextField nameTF;

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
Dimension minPnl = new Dimension(300, 400);

/* Test for JList */
String[] temp = {"1", "2", "3", "4", "1", "2", "3", "4","1", "2", "3", "4","1", "2", "3", "4" };

public Test()
{
    frame = new JFrame("CD Store");
    frame.setExtendedState(JFrame.NORMAL);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //frame.pack(); //sets size based on components size in TotalGUI
    //frame.setJMenuBar(create_MenuBar());
    //frame.setMinimumSize(minDim);

    frame.setSize(725, 550);

    //set frame location (central to screen)
    int fw = frame.getSize().width;
    int fh = frame.getSize().height;
    int fx = (dim.width-fw)/2;
    int fy = (dim.height-fh)/2;


    frame.getContentPane().add(create_Content_Pane());
    frame.setVisible(true);

    //moves the frame to the centre
    frame.setLocation(fx, fy);
}

public JPanel create_Content_Pane()
{
    JPanel TotalGUI = new JPanel();

    TotalGUI.add(remove = new JButton("Remove"));
    remove.addActionListener(this);

    TotalGUI.add(add = new JButton("Add")); <- this crashes when selected
    add.addActionListener(this);

    //TotalGUI.add(create_PersonPnl()); <- works just fine

    return TotalGUI;
}

public JPanel create_PersonPnl()
{
    personPnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
    personPnl.setBackground(Color.WHITE);
    personPnl.setPreferredSize(minPnl);

    /* VERTICAL BOX for Person boxes */
    Box personBox = Box.createVerticalBox();
    personBox.setBorder(new TitledBorder(new LineBorder(Color.DARK_GRAY), "Person"));

        /* Horizontal Box for Name Lbl & TF */
        Box nameBox = Box.createHorizontalBox();
            nameBox.add(Box.createHorizontalStrut(10));
            nameLbl = new JLabel("Name: ");
            nameBox.add(nameLbl);
            nameBox.add(Box.createHorizontalStrut(5));
            nameTF = new JTextField();
            nameBox.add(nameTF);
            nameBox.add(Box.createHorizontalStrut(10));

        personBox.add(nameBox);
        personBox.add(Box.createVerticalStrut(10));
        Box limitBox = Box.createHorizontalBox();
            limitBox.add(Box.createHorizontalStrut(10));
            limitLbl = new JLabel("CD Limit: ");
            limitBox.add(limitLbl);
            limitTF = new JFormattedTextField();
            limitBox.add(limitTF);
            limitBox.add(Box.createHorizontalStrut(10));
        personBox.add(limitBox);
        personBox.add(Box.createVerticalStrut(10));
        personBox.add(addPersonBtn = new JButton("Add Person"));
        personBox.add(Box.createVerticalStrut(10));
            personList = new JList(temp);
            personList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            scrollp = new JScrollPane(personList);
                scrollp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                scrollp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            scrollp.setSize(100, 45);
            personBox.add(scrollp);
            personBox.add(Box.createVerticalStrut(10));
        Box optionsBox = Box.createHorizontalBox();
            editPrsnBtn = new JButton("Edit");
            editPrsnBtn.addActionListener(this);
            optionsBox.add(editPrsnBtn);
            removePrsnBtn = new JButton("Remove");
            removePrsnBtn.addActionListener(this);
            optionsBox.add(removePrsnBtn);
        personBox.add(optionsBox);

    personPnl.add(personBox);
    return personPnl;
}

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

public void actionPerformed(ActionEvent e) 
{
    if(e.getSource() == remove)
    {
        TotalGUI.removeAll();
        TotalGUI.revalidate();
    }

    if(e.getSource() == add)
    {
        TotalGUI.add(create_PersonPnl());
        TotalGUI.revalidate();
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:46:10+00:00Added an answer on May 29, 2026 at 9:46 am

    In your method

    public JPanel create_Content_Pane()
    {
        JPanel TotalGUI = new JPanel();
    
        TotalGUI.add(remove = new JButton("Remove"));
        remove.addActionListener(this);
    
        TotalGUI.add(add = new JButton("Add")); <- this crashes when selected
        add.addActionListener(this);
    
        //TotalGUI.add(create_PersonPnl()); <- works just fine
    
        return TotalGUI;
    }
    

    The NullPointerException occurs because your private field TotalGUI is null… Remove the JPanel declaration in front of TotalGUI = new JPanel(); That will solve the null pointer problem. This probably solves only your problem in the test scenario… To solve the problem in your original scenario it would be nice to have the complete source code of the class..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is driving me insane. I'm trying to INSERT a value into the following
This is driving me absolutely insane. I know that, to change the formatting of
This one is driving me insane, and i can't figure out why it's throwing
This has been driving me insane for about the last hour. I'm trying to
This is driving me insane. I am trying to get a basic Report to
This is driving me insane. I am trying to grab an object in javascript
This is driving me insane....it was working earlier, but not it doesn't work. I
This is driving me farking insane. I'm trying to create a simple beginner app
This one is driving me absolutely insane, especially because I have a suspicion that
I'm hoping that this is a VERY simple question, but it's driving me insane!

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.