【问题标题】:Java Null Pointer Exception in Worker ClassWorker 类中的 Java 空指针异常
【发布时间】:2014-10-01 20:30:55
【问题描述】:
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);
 }
}

任何帮助将不胜感激。

【问题讨论】:

  • topPanelnull。您可能想使用调试器并弄清楚。或将BuildTopPanel(); 移动一排。另请注意,通常 java 方法名称以小写字母开头
  • 您可能希望在方法的第一个字符上使用小写。

标签: java swing user-interface jpanel


【解决方案1】:
getContentPane().add(topPanel, BorderLayout.NORTH); // Uh oh! topPanel is not initialized!
BuildTopPanel();

您尝试在BuildTopPanel() 方法中的初始化之前添加您的topPanel,所以它显然会导致NPE!

您的其他构建方法也是如此。请务必在使用它们或将它们添加到窗口之前实际初始化和构建您的 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);

【讨论】:

    【解决方案2】:

    在将组件添加到面板之前对其进行初始化。请参阅以下文件:

    http://docs.oracle.com/javase/7/docs/api/java/awt/Container.html#add%28java.awt.Component%29

    这意味着你需要在 getContentPane().add(mainPanel, BorderLayout.WEST); 之前调用 BuildMainPanel();

    与其他组件相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多