当前位置: 代码迷 >> Java Web开发 >>   "main "   java.lang.NullPointException
  详细解决方案

  "main "   java.lang.NullPointException

热度:188   发布时间:2016-04-17 14:55:51.0
初学者的问题..关于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 <test.java:50>
at   test.test.checklogin <test.java:77>
----------------------------------------该怎么改啊,,困了两天了----


------解决方案--------------------
at test.test.checklogin <test.java: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);
  相关解决方案