我有三个类 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);
}
});
}
}
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();
}
}
答案 0 :(得分:3)
简单的拼写错误。在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
设置为自己,并且不会使用您传入的 public ReportsJPanel (MainFrame mainframe){
this.mainFrame=mainFrame;
setBorder(new TitledBorder("Reports"));
}
。
答案 1 :(得分:2)
method parameter name is mainframe
您的代码中不匹配的名称this.mainFrame = mainframe;
{{1}}
我建议你使用像IDE工具一样的eclipse