刚接触JAVA,可能问的问题很白痴,请见谅
我的代码:
public int check(Insurant insurant) throws SQLException {
// TODO Auto-generated method stub

Insurant ins=idt.Find(insurant.getInsname());
//System.out.println(ins.getInsname());
String psd=insurant.getPswd();
String password=ins.getPswd();
int i=psd.compareTo(password);
return i;
    }
 登陆验证密码的方法。
问题是,从数据库返回的 实体对象ins肯定是对的,非空。
我要比较ins.getPswd()和原来传进来的密码。
int i=psd.compareTo(password);出错!我用bealean i=psd.equal(password);一样出错。
我觉得很简单的代码,就是不知道哪错了

解决方案 »

  1.   

    可能bealean 和equal我在发帖的时候写错了,不过在程序里我可以保证拼写是没错的,
    错误代码:警告: Unhandled exception
    java.lang.NullPointerException
    at java.lang.String.compareTo(String.java:997)
    at com.bestin.biz.InsurantManagerImpl.check(InsurantManagerImpl.java:18)
    at com.bestin.struts.LoginAction.execute(LoginAction.java:39)
      

  2.   


    String psd=insurant.getPswd();  // 应该是insurant中的pswd字段值为空
      

  3.   

    加打印语句查呗...String psd=insurant.getPswd();
    System.out.println(psd == null);
    String password=ins.getPswd();
    System.out.println(password == null);
    int i=psd.compareTo(password); 
      

  4.   

    6楼的方法,在控制台输出psd看看,看是否为空
      

  5.   

    打印一下看看是不是真的为空
    String psd=insurant.getPswd();
    System.out.println(psd);
    String password=ins.getPswd();
    System.out.println(password);
    int i=psd.compareTo(password);
      

  6.   

    psd.equal(password);应该是psd.equals(password);
      

  7.   

    用 s.equals(o)方法时注意两点:
    1 s为空时出错,空指针出错.
    建议你判断一下是否为空。
    2 如果 s,o,有一个为常量,(如o是肯定有值)建议用 o.equals(s),这样肯定不会出错。
      还有 s.equals(""),改成 "".equals(o),保证不出错。
      

  8.   

    boolean i = psd.equals(password);
      

  9.   

    bealean i=psd.equals(password);
      

  10.   

      想在程序人生的道路上,一直走下去,自己碰到问题就要自己找问题,
      自己解决问题,这样才可进度,发展。不要碰到怎么问题就急的求别人,
      就拿你楼主的问题来说,有很多的程序员都碰到这样问题。不管包含老的程序员还是新手程序员
      都会碰到,所以进行代码的写的时候要考虑几点:
      1.空的NULL异常。
      2.字段控制的长度。
      3.数据类型一致性。