java.lang.NullPointerException
 at com.UserMan.userExist(UserMan.java:32)
空指针,看一下UserMan.java是什么?

解决方案 »

  1.   

    UserMan.java的32行,
    哎,帖子不能修改
      

  2.   

    chubbchubb(长街)你好:
    你能否说的详细点,我改如何该。谢谢了!
      

  3.   

    你看一下UserMan.java的32行的代码,有个变量在运行的时候是null
    你把UserMan.java的代码放上来
      

  4.   

    chubbchubb(长街) 
    UserMan.java如下:package com;
    import javax.naming.*;
    import javax.sql.*;
    import java.sql.*;
    import java.util.*;
    public class UserMan
    {
      Context ctx=null;
      DataSource ds=null;
    //构造函数
      public UserMan()
      {
        //从连接池中获取数据库连接
        try{
          ctx = new InitialContext();
          ds = (DataSource)ctx.lookup("document");
        }
        catch(NamingException e){
          e.printStackTrace();
        }  }
      //检查指定的用户是否存在
      //userId为要检查的用户ID
      public boolean userExist(String userId){
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs=null;
        boolean occupied=true;
        try{
         String sqlquery="select * from users where id=?";
         con=ds.getConnection();
         ps=con.prepareStatement(sqlquery);
         ps.setString(1,userId);
         rs=ps.executeQuery();
         if(!rs.next())
             occupied=false;
         }
         catch(SQLException e){
          e.printStackTrace();
          }
          finally{
              if (rs != null)   try {rs.close();}
                   catch (SQLException ignore) {}
                if (ps != null)  try {ps.close();}
                   catch (SQLException ignore) {}
                if (con != null)   try {con.close();}
                   catch (SQLException ignore) {}
        }
        return occupied;
      }  //验证用户是否为合法用户
      //user为用户ID,pwd为登陆用户提交的密码
      public boolean isValidUser(String user,String pwd)
      {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs=null;
        boolean isValid=false;
        try{
         String sqlquery="select * from users where id=? and password=?";
         con=ds.getConnection();
         ps=con.prepareStatement(sqlquery);
         ps.setString(1,user);
         ps.setString(2,pwd);
         rs=ps.executeQuery();
         if(rs.next())
             isValid=true;
         }
         catch(SQLException e){
          e.printStackTrace();
          }
          finally{
              if (rs != null)   try {rs.close();}
                   catch (SQLException ignore) {}
                if (ps != null)  try {ps.close();}
                   catch (SQLException ignore) {}
                if (con != null)   try {con.close();}
                   catch (SQLException ignore) {}
        }
        return isValid;
      }
    }
      

  5.   

    你的ds为Null
    也就是没有找到数据源
      

  6.   

    UserMan.java的32行是:
    con=ds.getConnection()么?
      

  7.   

    chubbchubb(长街):
    32行是: con=ds.getConnection();
    我该如何做?
      

  8.   

    那就明白了,你的ds是空的,没有取到啊。
    ds = (DataSource)ctx.lookup("document");
    这里没有异常么?
    你看一下你的datasource的设置,SQLServer我没有用过,不知道怎么设
      

  9.   

    chubbchubb(长街):谢谢,问题一解决是:数据源出了问题。