永发信息网

下面这段代码编译没错,不过运行就出错,找不出原因?怎么解决?

答案:3  悬赏:30  手机版
解决时间 2021-06-01 14:05
  • 提问者网友:夢醒日落
  • 2021-06-01 00:17

代码为:import java.awt.*;
import javax.swing.*;
//import java.awt.event.*;
//import javax.swing.event.*;
//import java.util.*;
public class HappyRegisterGuI extends JFrame
{
JLabel userNameLab;
JLabel passwordLab;
JLabel pwTipLab;
JLabel repPwLab;
JLabel sexLab;
JLabel ageLab;
JLabel provinceLab;
JLabel joyLab;
JLabel introLab;

JTextField userNameTf;
JTextField ageTf;

JPasswordField passwordField;
JPasswordField repPwField;

JButton checkUserNameButton;
JButton regButton;

JRadioButton maleRb,femaleRb;

ButtonGroup bg;

JComboBox provinceCb;

JCheckBox musicCb,danceCb,swimCb,shoppingCb;

JTextArea introduceTa;

public HappyRegisterGuI()
{
super("Happy 用户注册");
Container c=this.getContentPane();
c.setLayout(new FlowLayout());

userNameLab=new JLabel("用户名:");
passwordLab=new JLabel("密 码:");
pwTipLab=new JLabel("密码不少于6位");
repPwLab=new JLabel("密码重复:");
sexLab=new JLabel("性 别:");
ageLab=new JLabel("年 龄:");
provinceLab=new JLabel("所在省份:");
joyLab=new JLabel("个人爱好:");
introLab=new JLabel("个人说明:");

userNameTf=new JTextField(20);
repPwField=new JPasswordField(20);

Image img=Toolkit.getDefaultToolkit().
getImage(this.getClass().getResource("/pictures/check.jpg"));
checkUserNameButton=new JButton(new ImageIcon(img));
checkUserNameButton.setToolTipText("检测用户名");
regButton=new JButton("注 册");

provinceCb=new JComboBox();
provinceCb.addItem("北京");
provinceCb.addItem("湖南");
provinceCb.addItem("台湾");
provinceCb.addItem("上海");
provinceCb.addItem("广东");
provinceCb.addItem("新疆");
provinceCb.addItem("西藏");
provinceCb.addItem("广西");

musicCb=new JCheckBox("音乐");
danceCb=new JCheckBox("舞蹈");
swimCb=new JCheckBox("游泳");
shoppingCb=new JCheckBox("购物");
introduceTa=new JTextArea(8,30);

c.add(userNameLab);
c.add(userNameTf);
c.add(checkUserNameButton);
c.add(passwordLab);
c.add(passwordField);
c.add(passwordField);
c.add(pwTipLab);
c.add(repPwField);
c.add(sexLab);
c.add(maleRb);
c.add(femaleRb);
c.add(ageLab);
c.add(ageTf);
c.add(provinceLab);
c.add(provinceCb);
c.add(joyLab);
c.add(musicCb);
c.add(danceCb);
c.add(swimCb);
c.add(shoppingCb);
c.add(introLab);
c.add(introduceTa);
c.add(regButton);

this.setSize(430,480);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args)
{

new HappyRegisterGuI();

}

}

运行时出现的问题为:--------------------Configuration: <Default>--------------------
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1027)
at java.awt.Container.add(Container.java:352)
at HappyRegisterGuI.<init>(HappyRegisterGuI.java:82)
at HappyRegisterGuI.main(HappyRegisterGuI.java:110)

Process completed.
麻烦那位编程高手帮在下解决 找下错在哪里 谢谢!

最佳答案
  • 五星知识达人网友:大漠
  • 2021-06-01 01:37
有几个组件在Constructor中没有初始就直接应用,导致NullPointerException: passwordField, maleRb, femaleRb, ageTf
全部回答
  • 1楼网友:冷風如刀
  • 2021-06-01 03:18
passwordField 没初始化
  • 2楼网友:三千妖杀
  • 2021-06-01 02:06
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯