简体   繁体   English

调用getter方法时发生NullPointerException

[英]NullPointerException on calling getter method

I have three classes TestGUI , MainFrame and ReportsJPanel . 我有三个类TestGUIMainFrameReportsJPanel MainFrame object is passed as parameter in ReportsJPanel constructor. MainFrame对象在ReportsJPanel构造函数中作为参数传递。 When I try to call getter method of MainFrame object that was passed and get the value of private variable, from ReportsJPanel object I get the NullPointerException error. 当我尝试调用传递的MainFrame对象的getter方法并获取私有变量的值时,从ReportsJPanel对象获取NullPointerException错误。 Error happens on this line in ReportsJPanel instalationLocation=mainFrame.getInstalationLocation(); ReportsJPanel instalationLocation = mainFrame.getInstalationLocation();中的此行发生错误

Code for TestGUI: 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);
            }
        });
    }

}

Code for 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;
    }

}

Code for ReportsJPanel: 报告代码JPanel:

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();
    }
}

Simple typo. 简单的错字。 In your constructor for your ReportsJPanel you have this. 在您的ReportsJPanel的构造函数中,您具有此功能。

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

You need to capitalize the f in the MainFrame object that you pass to the constructor. 您需要在传递给构造函数的MainFrame对象中大写f

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

Otherwise, you will be setting this.mainFrame to itself, and the mainframe you pass in will not be used. 否则,将this.mainFrame设置this.mainFrame自身,并且不会使用传入的mainframe

 public ReportsJPanel (MainFrame mainframe){

        this.mainFrame=mainFrame;

        setBorder(new TitledBorder("Reports"));

    }

names not matching in your code this.mainFrame = mainframe; 名称与您的代码不匹配this.mainFrame = mainframe;

method parameter name is mainframe

I suggest you to use eclipse like IDE tool 我建议您使用Eclipse之类的IDE工具

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM