调用 getter 方法时出现 NullPointerException

NullPointerException on calling getter method

我有三个 类 TestGUIMainFrameReportsJPanel。 MainFrame 对象在 ReportsJPanel 构造函数中作为参数传递。当我尝试调用传递的 MainFrame 对象的 getter 方法并获取私有变量的值时,从 ReportsJPanel 对象我得到 NullPointerException 错误。 ReportsJPanel 中的这一行发生错误 instalationLocation=mainFrame.getInstalationLocation();

TestGUI 代码:

package testgui;

public class TestGUI {

    public static void main(String[] args) {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(RiskManagerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RiskManagerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RiskManagerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RiskManagerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

                MainFrame mainFrame = new MainFrame();
                mainFrame.initComponents();
                mainFrame.setVisible(true);
            }
        });
    }

}

MainFrame 代码:

package testgui;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import left_panel_package.ReportsJPanel;

public class MainFrame extends JFrame{

    private String instalationLocation;

    public MainFrame(){
        setInstalationLocation("TEST_Location");
    }

    public void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("TEST");

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBackground(new Color(0, 0, 0));

        JPanel leftPanel = new JPanel();
        leftPanel.setBackground(new Color(255, 0, 0));
        leftPanel.setPreferredSize(new Dimension(200,100));
        leftPanel.setLayout(new GridLayout(3,0));

        JPanel rightPanel = new JPanel();
        rightPanel.setBackground(new Color(0, 255, 0));
        rightPanel.setPreferredSize(new Dimension(200,100));

        JPanel centerPanel = new JPanel();
        centerPanel.setBackground(new Color(0, 0, 255));

        JPanel toolBar = new JPanel();
        toolBar.setBackground(new Color(0, 255, 255));
        toolBar.setPreferredSize(new Dimension(100,100));

        ReportsJPanel reportsPanel = new ReportsJPanel(this);
        reportsPanel.initComponents();

        leftPanel.add(reportsPanel);

        mainPanel.add(leftPanel, BorderLayout.WEST);
        mainPanel.add(rightPanel, BorderLayout.EAST);
        mainPanel.add(centerPanel, BorderLayout.CENTER);
        mainPanel.add(toolBar, BorderLayout.NORTH); 

        setContentPane(mainPanel);

        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        int width = gd.getDisplayMode().getWidth();
        int height = gd.getDisplayMode().getHeight();
        setSize(width, height);
        setLocationRelativeTo(null);
    }

    public String getInstalationLocation() {
        return instalationLocation;
    }

    private void setInstalationLocation(String instalationLocation) {
        this.instalationLocation = instalationLocation;
    }

}

ReportsJPanel 的代码:

import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import testgui.MainFrame;

public class ReportsJPanel extends JPanel{

    private MainFrame mainFrame;
    private String instalationLocation;

    public ReportsJPanel (MainFrame mainframe){

        this.mainFrame=mainFrame;

        setBorder(new TitledBorder("Reports"));

    }
    public void initComponents(){
        instalationLocation=mainFrame.getInstalationLocation();
    }
}

简单的错字。在你的 ReportsJPanel 的构造函数中你有这个。

public ReportsJPanel(MainFrame mainframe) {
    this.mainFrame = mainFrame;
    setBorder(new TitledBorder("Reports"));
}

您需要将传递给构造函数的 MainFrame 对象中的 f 大写。

public ReportsJPanel(MainFrame mainFrame) {
    this.mainFrame = mainFrame;
    setBorder(new TitledBorder("Reports"));
}

否则,您将this.mainFrame设置为自身,您传入的mainframe将不会被使用。

 public ReportsJPanel (MainFrame mainframe){

        this.mainFrame=mainFrame;

        setBorder(new TitledBorder("Reports"));

    }

名称与您的代码不匹配 this.mainFrame = 大型机;

method parameter name is mainframe

我建议你使用eclipse之类的IDE工具