0

Receiving a null pointer error when trying to switch between windows. When I click on the Client button it does not switch windows and gives the error. Not sure what is wrong, I've spent hours looking through and feel like it's something small. Thanks for the help!

package GUI;

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

public class MainWindow extends JFrame {

    private Container mainPane;

    private JButton mainButton;

    private JButton clientButton;

    private JButton residentialButton;

    private JButton commercialButton;

    private Vector<JPanel> panelList;

    public MainWindow() {
        panelList = new Vector<JPanel>(4);

        JPanel tempPanel = createMainPanel();
        panelList.add(tempPanel);

        ClientWindow clientWindow = new ClientWindow();
        panelList.add(clientWindow.createClientPanel());

        ResidentialWindow residentialWindow = new ResidentialWindow();
        panelList.add(residentialWindow.createResidentialPanel());

        CommercialWindow commercialWindow = new CommercialWindow();
        panelList.add(commercialWindow.createCommercialPanel());
    }

    public void createWindow() {
        this.setTitle("Management Software");
        int frameWidth = 936;
        int frameHeight = 592;
        double screenWidth = this.getToolkit().getScreenSize().getWidth();
        double screenHeight = this.getToolkit().getScreenSize().getHeight();
        int startX = (int) (screenWidth / 2 - frameWidth / 2);
        int startY = (int) (screenHeight / 2 - frameHeight / 2);
        this.setSize(frameWidth, frameHeight);
        this.setLocation(startX, startY);

        mainPane = this.getContentPane();
        mainPane.setLayout(new BorderLayout(5, 5));

        JPanel buttonPanel = new JPanel(new GridLayout(1, 4));
        Border buttonEdge = BorderFactory.createRaisedBevelBorder();

        mainButton = new JButton("Main");
        mainButton.setBorder(buttonEdge);
        mainButton.addActionListener(new ButtonListener());
        buttonPanel.add(mainButton);

        clientButton = new JButton("Client");
        clientButton.setBorder(buttonEdge);
        clientButton.addActionListener(new ButtonListener());
        buttonPanel.add(clientButton);

        residentialButton = new JButton("Residential");
        residentialButton.setBorder(buttonEdge);
        residentialButton.addActionListener(new ButtonListener());
        buttonPanel.add(residentialButton);

        commercialButton = new JButton("Commercial");
        commercialButton.setBorder(buttonEdge);
        commercialButton.addActionListener(new ButtonListener());
        buttonPanel.add(commercialButton);

        mainPane.add(buttonPanel, BorderLayout.NORTH);
        mainPane.add(panelList.get(0), BorderLayout.CENTER);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    private JPanel createMainPanel() {
        JPanel mainPanel = new JPanel();
        JLabel mainL = new JLabel("The Main Screen",SwingConstants.CENTER);
        mainL.setForeground(Color.blue);
        mainL.setFont(new Font("Copperplate Gothic Bold",Font.BOLD,48));
        mainPanel.add(mainL);
        return mainPanel;
    }

    /***************************** INNER CLASSES *****************************/

            private JPanel  tp;

            @Override
            public void actionPerformed(ActionEvent e){
                for(int i = 0; i < panelList.size(); i++){
                    tp = panelList.get(i);
                    mainPane.remove(tp);
                    tp.setVisible(false);
                }

                if(e.getSource() == mainButton){
                    tp = panelList.get(0);
                    mainPane.add(tp,BorderLayout.CENTER);
                    tp.setVisible(true);

                }

                if(e.getSource() == clientButton){
                    tp = panelList.get(1);
                    mainPane.add(tp,BorderLayout.CENTER);
                    tp.setVisible(true);

                }
                if(e.getSource() == residentialButton){
                    tp = panelList.get(2);
                    mainPane.add(tp,BorderLayout.CENTER);
                    tp.setVisible(true);

                }
                if(e.getSource() == commercialButton){
                    tp = panelList.get(3);
                    mainPane.add(tp,BorderLayout.CENTER);
                    tp.setVisible(true);
                    }
                }
            }
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Don't know what the problem is since the code won't compile and we don't know the statement that is causing the problem. In any case the proper solution is to use a `CardLayout` to swap panels in the frame. Read the section from the Swing tutorial on [How to use CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) for more information and working examples. – camickr Apr 05 '17 at 18:31
  • Please add the classes: ClientWindow, ResidentialWindow, CommercialWindow and ButtonListener – sirandy Apr 05 '17 at 19:21
  • 1
    @sirandy, no we are not interested in those classes. We don't want to see the entire application and the details of the panel are irrelevant to the question. We wan't to see a proper [mcve] that demonstrates the problem. So, for example, the panels can easily be replace by a single JLabel to demonstrate the concept of swapping components using a CardLayout. The point of the [mcve] is to force the OP to simplifying the problem. Many time while simplifying the problem they will find the problem. – camickr Apr 05 '17 at 19:44
  • I can supply the other classes but they are hundreds of lines long. After more tinkering the error seems to be happening on the button click like it doesn't know where it's going. – Sean Jeske Apr 05 '17 at 19:47
  • 1
    `I can supply the other classes` - read all comments. That is not required and is irrelevant to the problem. Post a proper [mcve]!!! – camickr Apr 05 '17 at 20:01
  • 1
    1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 3) Always copy/paste error and exception output! 4) And as mentioned by @camickr, post an MCVE. – Andrew Thompson Apr 06 '17 at 00:23

0 Answers0