java 运行 错误
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
public class Exe9_1{
public static void main(String args[]){
new WindowBox();
}
}
class WindowBox extends JFrame{
Box baseBox,boxV1,boxV2;
WindowBox(){
boxV1=Box.createVerticalBox(); boxV1.add(new JLabel("学号"));
boxV1.add(Box.createVerticalStrut(8)); boxV1.add(new JLabel("姓名"));
boxV1.add(Box.createVerticalStrut(8)); boxV1.add(new JLabel("密码"));
boxV1.add(Box.createVerticalStrut(8)); boxV1.add(new JLabel("再输入一次密码")); boxV2=Box.createVerticalBox(); boxV2.add(new JTextField(16));
boxV2.add(Box.createVerticalStrut(8)); boxV2.add(new JTextField(16));
boxV2.add(Box.createVerticalStrut(8)); boxV2.add(new JTextField(16));
boxV2.add(Box.createVerticalStrut(8)); boxV2.add(new JTextField(16));
boxV2.add(Box.createVerticalStrut(8)); boxV2.add(new JTextField(16)); baseBox.createHorizontalBox(); baseBox.add(boxV1);
baseBox.add(Box.createVerticalStrut(10)); baseBox.add(boxV2);
setLayout(new FlowLayout()); add(baseBox); validate();
setBounds(120,125,200,200); setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
出现了 如下 错误:
Exception in thread "main"
java.lang.NullPointerException
at WindowBox.<init>(Exe9_1.java:39)
at Exe9_1.main(Exe9_1.java:8)
------解决方案--------------------baseBox.createHorizontalBox();
改一下,没有实例化。
baseBox = Box.createHorizontalBox();