当前位置: 代码迷 >> Java相关 >> 一堆NullpointSQLException快看看
  详细解决方案

一堆NullpointSQLException快看看

热度:161   发布时间:2007-02-05 23:43:27.0

再test的文件夹中,有.jpx的文件,那个工程的入口,用jbuilder打开那个就可以拉


----------------解决方案--------------------------------------------------------
如果我的错误过于幼稚,请及时指出,因为我一向爱犯幼稚问题
----------------解决方案--------------------------------------------------------

[CODE]package test5;
import java.sql.*;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class DBOperate extends SQlDb {
public DBOperate() {
}
public boolean check(String username) {
boolean checkpoint = true;
ResultSet get = null;
String sql = "select * from UserInfo where Loginname='" + username +
"'";
this.opensql();
get = this.quary(sql);
// this.closesql();道理同下
String huode = null;
if (get != null) {
try {
while (get.next()) {
huode = get.getString(1);
}
} catch(SQLException ex) {
System.out.println(ex);
}
}
if (huode.equals("")) {
checkpoint = false;
} else {
checkpoint = true;
}
return checkpoint;
}
public boolean check(String username,String userpassword)
{
boolean checkpoint=true;
ResultSet resu=null;
String duibi=null;
String sql="select Password from UserInfo where Loginname='"+username+"'";
this.opensql();
resu=this.quary(sql);
// this.closesql();你数据库关了以后,怎么可能调用resu.next()呢
if(duibi==null)//你这里写的是!=,呵呵,这个错误也犯?
{
try
{
while(resu.next())
{
duibi=resu.getString(1);
}
}
catch(SQLException ex)
{
System.out.println(ex);
}
}
if(duibi.equals(userpassword))
{
checkpoint=true;
}
else
{
checkpoint=false;
}
return checkpoint;
}
public void changepassword(String username,String password)
{
String sql="update UserInfo set PassWord='"+password+"' where LoginName='"+username+"'";
this.opensql();
this.quary(sql);
this.closesql();
}
}[/CODE]

改了一两个地方,现在没有异常,并且可以用了,但是我觉得你的程序设计很有问题,好好看看,好好想想,如果能行的话,自己好好改改吧


----------------解决方案--------------------------------------------------------
你现在知道你为什么会空指针异常吧

因为你duibi和huode这两个值是null,它们为什么会null呢,因为你没有赋值给它们

并且如果用户名输错了的话,那一定会空指针异常,这一点我没有帮你改,你自己试着改改

我看了你的数据库,用户名输入"春"就不会错了,
----------------解决方案--------------------------------------------------------
以下是引用韩剧鼻祖在2007-2-5 23:43:27的发言:

再test的文件夹中,有.jpx的文件,那个工程的入口,用jbuilder打开那个就可以拉

我没有Jbuilder,我是用NB来看你的程序的
----------------解决方案--------------------------------------------------------

算了,我还是帮你改一个用户名出错的地方吧,其实只要改一个字

[CODE]package test5;
import java.sql.*;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class DBOperate extends SQlDb {
public DBOperate() {
}
public boolean check(String username) {
boolean checkpoint = true;
ResultSet get = null;
String sql = "select * from UserInfo where Loginname='" + username +
"'";
this.opensql();
get = this.quary(sql);
// this.closesql();
String huode = "";//初始化给它""而不是null,就不会出空指针异常了,下面的你也改改
if (get != null) {
try {
while (get.next()) {
huode = get.getString(1);
}
} catch(SQLException ex) {
System.out.println(ex);
}
}
if (huode.equals("")) {
checkpoint = false;
} else {
checkpoint = true;
}
return checkpoint;
}
public boolean check(String username,String userpassword)
{
boolean checkpoint=true;
ResultSet resu=null;
String duibi=null;
String sql="select Password from UserInfo where Loginname='"+username+"'";
this.opensql();
resu=this.quary(sql);
// this.closesql();
if(duibi==null)
{
try
{
while(resu.next())
{
duibi=resu.getString(1);
}
}
catch(SQLException ex)
{
System.out.println(ex);
}
}
if(duibi.equals(userpassword))
{
checkpoint=true;
}
else
{
checkpoint=false;
}
return checkpoint;
}
public void changepassword(String username,String password)
{
String sql="update UserInfo set PassWord='"+password+"' where LoginName='"+username+"'";
this.opensql();
this.quary(sql);
this.closesql();
}
}[/CODE]



----------------解决方案--------------------------------------------------------
恩,closesql这里我改了,可是后面!=你改为==我有点问题,如果为==,那我还要他判断么,直接返回false不久可以了吧,另外我这里运行的异常是,在数据库添加数据后,我点击修改,会提示我
newpassword=this.jt3.getText();
DBOperate object=new DBOperate();
test=object.check(username);
这行有错误,
resu=this.quary(sql);
这一行也会出现错误

----------------解决方案--------------------------------------------------------
其实楼上本版最负责的版主
楼主应该好好反省自己的错误
毕竟谁也没有帮助你的义务
遇到问题 还是自己解决后才能真正体会要领的。。。。。。。
----------------解决方案--------------------------------------------------------
以下是引用韩剧鼻祖在2007-2-5 23:56:49的发言:
恩,closesql这里我改了,可是后面!=你改为==我有点问题,如果为==,那我还要他判断么,直接返回false不久可以了吧,另外我这里运行的异常是,在数据库添加数据后,我点击修改,会提示我
newpassword=this.jt3.getText();
DBOperate object=new DBOperate();
test=object.check(username);
这行有错误,
resu=this.quary(sql);
这一行也会出现错误

其实你那个==根本就不是判断用户名和密码是否相等的,你那个语句根本就是一句多余的废话

还有,你要判断用户名和密码是否正确,为什么用取两次呢,一次性取出来不是很好吗?难道你的数据库速度很快?

学习JAVA不能好高骛远.我对每个初学JAVA的人都是这么说的,如果基础不好,就不要一开始就想写什么什么系统,什么什么大的东西,要从基础的写起,对于数据库操作,我觉得还是学好了其它的再说

我记得我初学JAVA的时候,前三个月没有写一个用户界面程序,天天在CMD下面写程序,调试程序,有什么错误和异常,只有自己去排除,并且我还没有什么高级的IDE,只有自己一行一行地去数,数到出错误的行,然后自己去想,为什么会出异常呢.

也就是因为这样,我才打下了坚实的JAVA基础,你要相信基础打好以后,学什么都很快的

上面的这些话,希望对你有所帮助,晚安


----------------解决方案--------------------------------------------------------

真是苦口婆心,这么有耐心佩服了


----------------解决方案--------------------------------------------------------
  相关解决方案