Java – cannot add the same panel again
It drives me crazy When you click three different buttons, I'm trying to change the panel, but it applies to one panel - only once
If you click addperson – the "people" panel displays, if you then click addcd – the CD panel displays; If you then click add person – it doesn't work It throws a nullpointer exception Even if you click add CD / viewstore and add the person it displays, it will not display again
In a test file, I created a GUI with add and delete: if I click add, it will throw a null pointer exception, but if I just add it and compile, that's good
/* PERSON PANEL */ public JPanel create_PersonPnl() { personPnl = new JPanel(new FlowLayout(FlowLayout.CENTER)); personPnl.setBackground(Color.WHITE); personPnl.setPreferredSize(minPnl); /* VERTICAL @R_459_2419@ for Person @R_459_2419@es */ @R_459_2419@ person@R_459_2419@ = @R_459_2419@.createVertical@R_459_2419@(); person@R_459_2419@.setBorder(new TitledBorder(new LineBorder(Color.DARK_GRAY),"Person")); /* Horizontal @R_459_2419@ for Name Lbl & TF */ @R_459_2419@ name@R_459_2419@ = @R_459_2419@.createHorizontal@R_459_2419@(); name@R_459_2419@.add(@R_459_2419@.createHorizontalStrut(10)); nameLbl = new JLabel("Name: "); name@R_459_2419@.add(nameLbl); name@R_459_2419@.add(@R_459_2419@.createHorizontalStrut(5)); nameTF = new JTextField(); name@R_459_2419@.add(nameTF); name@R_459_2419@.add(@R_459_2419@.createHorizontalStrut(10)); person@R_459_2419@.add(name@R_459_2419@); person@R_459_2419@.add(@R_459_2419@.createVerticalStrut(10)); @R_459_2419@ limit@R_459_2419@ = @R_459_2419@.createHorizontal@R_459_2419@(); limit@R_459_2419@.add(@R_459_2419@.createHorizontalStrut(10)); limitLbl = new JLabel("CD Limit: "); limit@R_459_2419@.add(limitLbl); limitTF = new jformattedtextfield(); limit@R_459_2419@.add(limitTF); limit@R_459_2419@.add(@R_459_2419@.createHorizontalStrut(10)); person@R_459_2419@.add(limit@R_459_2419@); person@R_459_2419@.add(@R_459_2419@.createVerticalStrut(10)); person@R_459_2419@.add(addPersonBtn = new JButton("Add Person")); person@R_459_2419@.add(@R_459_2419@.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); person@R_459_2419@.add(scrollp); person@R_459_2419@.add(@R_459_2419@.createVerticalStrut(10)); @R_459_2419@ options@R_459_2419@ = @R_459_2419@.createHorizontal@R_459_2419@(); editPrsnBtn = new JButton("Edit"); editPrsnBtn.addActionListener(this); options@R_459_2419@.add(editPrsnBtn); removePrsnBtn = new JButton("Remove"); removePrsnBtn.addActionListener(this); options@R_459_2419@.add(removePrsnBtn); person@R_459_2419@.add(options@R_459_2419@); personPnl.add(person@R_459_2419@); return personPnl; }
This is the content of 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 the code of 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);
S. O.p is debugging to see what is running And print it only once unless I take it out add(create_PersonPnl()); So I narrowed it down, but I had no clue because it worked for the first time
Thank you in advance!
Separate test files to prove that it is 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","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 @R_459_2419@ for Person @R_459_2419@es */ @R_459_2419@ person@R_459_2419@ = @R_459_2419@.createVertical@R_459_2419@(); person@R_459_2419@.setBorder(new TitledBorder(new LineBorder(Color.DARK_GRAY),45); person@R_459_2419@.add(scrollp); person@R_459_2419@.add(@R_459_2419@.createVerticalStrut(10)); @R_459_2419@ options@R_459_2419@ = @R_459_2419@.createHorizontal@R_459_2419@(); editPrsnBtn = new JButton("Edit"); editPrsnBtn.addActionListener(this); options@R_459_2419@.add(editPrsnBtn); removePrsnBtn = new JButton("Remove"); removePrsnBtn.addActionListener(this); options@R_459_2419@.add(removePrsnBtn); person@R_459_2419@.add(options@R_459_2419@); personPnl.add(person@R_459_2419@); 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(); } } }
Solution
In your way
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; }
NullPointerException occurs because your private field totalgui is null... Delete the JPanel declaration before totalgui = new jpanel(); This will solve the null pointer problem This may only solve the problems in the test scenario... To solve the problems in the original scenario, it is best to have the complete source code of this class