我有三个类TestGUI,MainFrame和ReportsJPanel。
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);
}
});
}
}
主机代码:
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;
}
}
报告代码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();
}
}
最佳答案
简单的错字。在ReportsJPanel
的构造函数中,您具有此功能。
public ReportsJPanel(MainFrame mainframe) {
this.mainFrame = mainFrame;
setBorder(new TitledBorder("Reports"));
}
您需要在传递给构造函数的
f
对象中大写MainFrame
。public ReportsJPanel(MainFrame mainFrame) {
this.mainFrame = mainFrame;
setBorder(new TitledBorder("Reports"));
}
否则,将为自己设置
this.mainFrame
,并且不会使用您传入的mainframe
。关于java - 调用getter方法时发生NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33244442/