Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at Volume.<init>(Volume.java:25)
at VolumeDriver.main(VolumeDriver.java:6)
尝试运行程序时出现上述错误。我的程序不完整,但我只想查看我的窗口是什么样的,所以我可以确保它看起来正确。 这是我现在的工人班
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Volume extends JFrame
{
private static final long serialVersionUID = 1L;
private JPanel topPanel;
private JPanel bottomPanel;
private JPanel rightPanel;
private JPanel mainPanel;
private JLabel message;
private final int width = 500;
private final int height = 400;
public Volume()
{
setTitle("Sphere and Box Volumes");
setSize(width,height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(topPanel, BorderLayout.NORTH);
BuildTopPanel();
getContentPane().add(bottomPanel, BorderLayout.SOUTH);
BuildBotPanel();
getContentPane().add(rightPanel, BorderLayout.EAST);
BuildRightPanel();
getContentPane().add(mainPanel, BorderLayout.WEST);
BuildMainPanel();
setVisible(true);
}
private void BuildTopPanel()
{
topPanel = new JPanel(new FlowLayout());
topPanel.setBackground(Color.WHITE);
JTextField reqVolume = new JTextField(8);
message = new JLabel("Enter the required amount of volume:");
topPanel.add(message);
topPanel.add(reqVolume);
}
private void BuildBotPanel()
{
bottomPanel = new JPanel(new FlowLayout());
bottomPanel.setBackground(Color.WHITE);
JButton intialQuant = new JButton("Set Initial Quantities");
intialQuant.setActionCommand("I");
intialQuant.addActionListener(new ButtonListener());
JButton calcVolume = new JButton("Calculate Volumes");
calcVolume.setActionCommand("V");
calcVolume.addActionListener(new ButtonListener());
JButton close = new JButton("Close");
close.setActionCommand("C");
close.addActionListener(new ButtonListener());
bottomPanel.add(intialQuant);
bottomPanel.add(calcVolume);
bottomPanel.add(close);
}
private void BuildRightPanel()
{
rightPanel = new JPanel(new FlowLayout());
rightPanel.setBackground(Color.WHITE);
}
private void BuildMainPanel()
{
mainPanel = new JPanel(new FlowLayout());
mainPanel.setBackground(Color.WHITE);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
}
这是我的驱动程序类
public class VolumeDriver
{
public static void main(String[] args)
{
Volume frame = new Volume();
frame.setVisible(true);
}
}
任何帮助都将不胜感激。
答案 0 :(得分:1)
getContentPane().add(topPanel, BorderLayout.NORTH); // Uh oh! topPanel is not initialized!
BuildTopPanel();
您尝试在topPanel
方法初始化之前添加BuildTopPanel()
,因此显然会导致NPE!
您的其他构建方法也是如此。在使用GUI元素或将它们添加到窗口之前,请务必初始化并构建GUI元素!
例如:
// Initialize GUI elements first:
BuildTopPanel();
BuildBotPanel();
BuildRightPanel();
BuildMainPanel();
// Then add the GUI elements to the window:
getContentPane().add(topPanel, BorderLayout.NORTH);
getContentPane().add(bottomPanel, BorderLayout.SOUTH);
getContentPane().add(rightPanel, BorderLayout.EAST);
getContentPane().add(mainPanel, BorderLayout.WEST);
答案 1 :(得分:1)
在将组件添加到面板之前初始化组件。见文件如下:
http://docs.oracle.com/javase/7/docs/api/java/awt/Container.html#add%28java.awt.Component%29
这意味着你需要在getContentPane()之前调用BuildMainPanel()。add(mainPanel,BorderLayout.WEST);
与其他组件相同。