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)
내 프로그램을 실행하려고 할 때 위의 오류가 발생합니다. 내 프로그램이 완전하지는 않지만 내 창문이 어떻게 보이는지 확인하여 올바른 것으로 보이게 할 수 있습니다. 이 지금Java Null 포인터 예외 작업자 클래스
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);
}
}
내 드라이버 클래스 내 작업자 클래스입니다.
'topPanel'은'null'입니다. 디버거를 사용하여 파악할 수 있습니다. 또는 BuildTopPanel(); 한 줄 위로 이동하십시오. 또한 보통 Java 메소드 이름은 소문자로 시작합니다. –
메소드의 첫 문자에는 소문자를 사용하는 것이 좋습니다. –