Worker类中的Java空指针异常
这是我的驾驶课Worker类中的Java空指针异常,java,swing,user-interface,jpanel,Java,Swing,User Interface,Jpanel,这是我的驾驶课 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
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);
}
}
您尝试在BuildTopPanel()
方法中初始化之前添加topPanel
,因此它显然会导致NPE
其他构建方法也是如此。在使用GUI元素或将它们添加到窗口之前,请确保实际初始化并构建GUI元素
例如:
getContentPane().add(topPanel, BorderLayout.NORTH); // Uh oh! topPanel is not initialized!
BuildTopPanel();
在将组件添加到面板之前初始化组件。见以下文件: 这意味着您需要在getContentPane()之前调用BuildMainPanel().add(mainPanel,BorderLayout.WEST)
与其他组件相同。
topPanel
为null
。您可能需要使用调试器并找出答案。或移动BuildTopPanel()代码>一行。还请注意,通常java方法名称以小写字母开头。您可能希望在方法的第一个字符上使用小写字母。
getContentPane().add(topPanel, BorderLayout.NORTH); // Uh oh! topPanel is not initialized!
BuildTopPanel();
// 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);