Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: How to use a cardholder with multiple classes
    primarykey
    data
    text
    <p>I'm programming in Java, trying to use a cardholder in order to switch between 2 JPanels which are each an extension of their own class. I think I understand the basic concepts but I am having errors in my current revision, when calling the classes. I'm getting a null pointer exception and I think it's a structural problem but I'm not sure how or why.</p> <p>The main method points to this class</p> <pre><code>public class Skeleton implements ActionListener{ JPanel cardHolder; CardLayout cards; String cardA = "A"; String cardB = "B"; JPanel Jboard; JPanel Jmenu; JFrame frame2; Board board; Menu menu; boolean menuSet; boolean boardSet; Timer timer; public class Switcher implements ActionListener{ String card; Switcher(String card){ this.card = card; } @Override public void actionPerformed(ActionEvent e) { cards.show(cardHolder, card); } } public Skeleton(JFrame frame){ JPanel menu = new Menu(); JPanel board = new Board(); JFrame frame2 = frame; timer = new Timer(5, this); timer.start(); cardHolder = new JPanel(); cards = new CardLayout(); cardHolder.setLayout(cards); cardHolder.add(menu, cardA); cardHolder.add(board, cardB); frame2.add(cardHolder); frame2.revalidate(); frame2.setVisible(true); } public JFrame getSkeleton(){ return frame2; } public JPanel getCardHolder(){ return cardHolder; } public void checkStatus(){ if (menuSet == true){ new Switcher(cardB); boardSet = false; } if (boardSet == true){ new Switcher(cardA); menuSet = false; } } @Override public void actionPerformed(ActionEvent e) { menuSet = menu.getMenuset(); boardSet = board.getBoardset(); checkStatus(); } } </code></pre> <p>This is the board class, one of the JPanels I'm trying to switch between</p> <pre><code>public class Board extends JPanel{ boolean boardset; Menu menu = new Menu(); public Board(){ setBackground(Color.WHITE); } public JPanel getPanel(){ return this; } public void setBoardset(boolean x){ boardset = x; } public boolean getBoardset(){ return boardset; } } </code></pre> <p>Here is the other JPanel class, which contains a button used to switch to the other JPanel class. This is also the original starting JPanel used.</p> <pre><code>public class Menu extends JPanel implements ActionListener{ boolean menuset; public Menu(){ setBackground(Color.BLACK); JButton button = new JButton("hello"); button.addActionListener(this); this.add(button); } public JPanel getPanel(){ return this; } @Override public void actionPerformed(ActionEvent e) { menuset = true; } public void setMenuset(boolean x){ menuset = x; } public boolean getMenuset(){ return menuset; } } </code></pre> <p>Like I said, I'm getting a null pointer exception. It is occuring on this line of the Skeleton() class</p> <pre><code>menuSet = menu.getMenuset(); </code></pre> <p>The line above is right after the actionPerformed event above (from the timer), and I have tested the timer a little, it works doing basic print statements but whenever I try to use the 'menu' or 'board' instance inside the actionPerformed, I get this null pointer exception.</p> <p>I would appreciate any advice. I get the idea that the way I'm doing this may be a little convoluted. If anyone has any suggestions on a better way to do this it would also be helpful. My main goal is to be able to call 2 separate classes from one main class containing a cardholder. That way I can separate the code in order to keep everything isolated and in order.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload