Topic: 如何调试java程序?(NullPointerException)

  Print this page

1.如何调试java程序?(NullPointerException) Copy to clipboard
Posted by: lidan_myname
Posted on: 2006-11-25 14:57

出现下面的提示不知道到底是什么地方出了错,请指教!

2.Re:如何调试java程序?(NullPointerException) [Re: lidan_myname] Copy to clipboard
Posted by: why
Posted on: 2006-11-25 18:07

Please show us your source.

3.Re:如何调试java程序?(NullPointerException) [Re: lidan_myname] Copy to clipboard
Posted by: ddddddv
Posted on: 2006-11-26 20:17

有没有先用javac编译长生一个class的文件啊
再之后可以用 java命令的

4.Re:如何调试java程序?(NullPointerException) [Re: lidan_myname] Copy to clipboard
Posted by: 小神
Posted on: 2006-11-26 23:38

这是一个空引用异常。。。是不是引用的对象没有实例化

5.Re:如何调试java程序?(NullPointerException) [Re: lidan_myname] Copy to clipboard
Posted by: lidan_myname
Posted on: 2006-11-28 09:42

这是我的源代码.用javac编译过,可通过,但用java就出现以上问题.我郁闷的是电脑显示的信息到底是指错出在哪?有没有总结这些信息的资料参考一下.
import javax.swing.*;
import java.awt.*;

public class GridBagLayoutDemo extends JFrame{
private Container container;
private GridBagLayout layout=new GridBagLayout();
private GridBagConstraints constraints=new GridBagConstraints();
JLabel labUser,labPassword;
JTextField getUser,getPassword;
JButton butLogin;

public GridBagLayoutDemo(){
super("网格布局");
this.setSize(600,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private void setConstraints(GridBagConstraints gbc,int egridx,
int egridy,int egridwith,int egridheight,
int eweightx,int eweighty){
gbc.gridx=egridx;
gbc.gridy=egridy;
gbc.gridwidth=egridwith;
gbc.gridheight=egridheight;
gbc.weightx=eweightx;
gbc.weighty=eweighty;
}

public void setLayout(){
container=getContentPane();
container.setLayout(layout);

constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.CENTER;
this.setConstraints(constraints,0,0,1,1,0,0);
labUser=new JLabel("用户名:");
layout.setConstraints(labUser,constraints);
container.add(labUser);

constraints.fill=GridBagConstraints.HORIZONTAL;
this.setConstraints(constraints,1,0,1,1,100,100);
getUser=new JTextField();
layout.setConstraints(getUser,constraints);
container.add(getUser);

constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.CENTER;
this.setConstraints(constraints,0,1,1,1,0,0);
labUser=new JLabel("密码:");
layout.setConstraints(labPassword,constraints);
container.add(labPassword);

constraints.fill=GridBagConstraints.HORIZONTAL;
this.setConstraints(constraints,1,1,1,1,100,100);
getPassword=new JTextField();
layout.setConstraints(getPassword,constraints);
container.add(getPassword);

constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.CENTER;
this.setConstraints(constraints,0,2,2,1,0,0);
butLogin=new JButton("登录");
layout.setConstraints(butLogin,constraints);
container.add(butLogin);
}

public static void main(String args[]){
GridBagLayoutDemo frame=new GridBagLayoutDemo();
frame.setLayout();
frame.setVisible(true);
}
}

6.Re:如何调试java程序?(NullPointerException) [Re: lidan_myname] Copy to clipboard
Posted by: zcjl
Posted on: 2006-11-28 11:18

layout.setConstraints(labPassword,constraints);

这一行中的labPassword还没有进行初始化和赋值,所以会出现NullPointerException
估计前面的
labUser=new JLabel("密码:");
应该是
labPassword=new JLabel("密码:");

coding要仔细些

7.Re:如何调试java程序?(NullPointerException) [Re: zcjl] Copy to clipboard
Posted by: lidan_myname
Posted on: 2006-11-28 11:32

就是这个问题.谢谢!Blush


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923