场景:
java.lang.NullPointException异常
点击右边红色标题查看本文完整版:java.lang.NullPointException异常
package test;
import java.sql.*;
import testbean.beantest;
public class test
{
private int pwdcheck(String name,String Pwd)
{
int flag=0;
String pass= " ";
ResultSet rs=null;
beantest checkPwd=new beantest();
String strsql= "select UserPwd from login where UserId= ' "+name+ " ' or UserName= ' "+name+ " ' ";
rs=checkPwd.GetRs(strsql);
try
{
while (rs.next())
{
pass=rs.getString( "UserPwd ");
}
rs.close();
if (pass.equals(Pwd))
flag=2;//密码正确
else
flag=3;//密码错误
}
catch (SQLException e)
{
flag=0;
}
return flag;
}
public int checklogin(String name,String Pwd){
ResultSet rs=null;
int count=0;
int flag=0;
beantest namecheck=new beantest();
String strsql= "select count(*) as recount from login where UserId= ' "+name+ " ' or UserName= ' "+name+ " ' ";
rs=namecheck.GetRs(strsql);
try{
while(rs.next())
{
count=rs.getInt( "recount ");
}
rs.close();
if(count==0)
flag=1;//用户存在
else{
if(pwdcheck(name,Pwd)==2)
flag=2;//密码正确
else
flag=3;//密码不正确
}
}
catch(SQLException e){return 0;}
return flag;
}
public static void main(String args[]){
test dd=new test();
System.out.println(dd.checklogin( "ss ", "ss "));
}
}
----------------------运行结果-----------
Exception in thread "main " java.lang.NullPointException
at test.test.checklogin <50>50>
at test.test.checklogin <77>77>
----------------------------------------该怎么改啊----
------解决方法--------------------
at test.test.checklogin <50>50>
数啊,test.java文件的第50行,你有对象没有new过
------解决方法--------------------
==========Beantest类都有问题
代码帮你整理了下,前面那个测试类,自己弄吧
感觉好乱..
package testbean;
import java.sql.*;
public class beantest
{
String driver= "com.microsoft.jdbc.sqlserver.SQLSercerDriver ";
String url= "jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=test ";
String user= "sa ";
String pass= " ";
Connection Conn=null;
Statement stmt=null;
ResultSet rs=null;
public beantest()
{
DbConn();
}
private void DbConn()
{
try{
Class.forName(driver);
Conn=DriverManager.getConnection(url,user,pass);
}
catch(Exception e){
}
}
public String GetRs(String strsql)
{
try
{
stmt=Conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(strsql);
return rs;
}
catch (SQLException e)
{
return null;
}
}
------解决方法--------------------
select count(*) 以这个开头的永远会返回一行结果集
哪怕没有满足你后面where条件的一个数据库行
另外把as recount 这个去掉在看看