使用equals和==进行比较

时间:2012-02-06 03:16:46

标签: java operators

我有这样的条件:

try {
        History history = requestHelper.getHistory();
       if (!history.getInvoiceNo().equalsIgnoreCase("") || //String
                !history.getCcEmail().equalsIgnoreCase("") || //String
                !history.getHostAuthCode().equalsIgnoreCase("") || //String
                !history.getMerchantCategory().equalsIgnoreCase(null) || //String
                !history.getCcName().equalsIgnoreCase("") || //String
                !history.getCcPhone().equalsIgnoreCase("") || //String
                history.getBatchNo() != 0 || //int
                !history.getIpAddress().equalsIgnoreCase("") || //String
                !history.getTransactionStatus().equals(null)) { //char
            System.out.println(": : : FAILED GET HISTORY, NO PARAMETER SET!");
            requestHelper.getResultHelper().setLstHistoryTransaction(null);
       else {
            System.out.println(": : : SUCCESS GET HISTORY!");
            /* some command */
       }
} catch (Exception ex) {
        System.out.println(": : : ERROR QUERYING HISTORY TO DATABASE");
        ex.printStackTrace();
}

但是当我不填充该参数的任何变量时,为什么会出现这样的错误:

  

10:07:35,238 INFO [STDOUT] :::错误查询数据库的历史
  10:07:35,238 ERROR [STDERR] java.lang.NullPointerException
  10:07:35,238 ERROR [STDERR] at   doku.edp.executors.ManageTransactionExecutorBean.findHistoryTransaction(ManageTransactionExecutorBean.java:411)
  10:07:35,238 ERROR [STDERR] at   doku.edp.executors.ManageTransactionExecutorBean.execute(ManageTransactionExecutorBean.java:78)
  10:07:35,239 ERROR [STDERR] at   sun.reflect.NativeMethodAccessorImpl.invoke0(原生方法)
  10:07:35,239 ERROR [STDERR] at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  10:07:35,239 ERROR [STDERR] at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  10:07:35,239 ERROR [STDERR] at   java.lang.reflect.Method.invoke(Method.java:597)
  10:07:35,239错误   [STDERR]在   org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:122)
  10:07:35,239 ERROR [STDERR] at   org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:111)
  10:07:35,239 ERROR [STDERR] at   org.jboss.ejb3.EJBContainerInvocationWrapper.invokeNext(EJBContainerInvocationWrapper.java:69)
  10:07:35,239 ERROR [STDERR] at   org.jboss.ejb3.interceptors.aop.InterceptorSequencer.invoke(InterceptorSequencer.java:73)
  10:07:35,239 ERROR [STDERR] at   org.jboss.ejb3.interceptors.aop.InterceptorSequencer.aroundInvoke(InterceptorSequencer.java:59)   10:07:35,239 ERROR [STDERR] at   sun.reflect.GeneratedMethodAccessor402.invoke(Unknown Source)
  10:07:35,239 ERROR [STDERR] at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  10:07:35,239 ERROR [STDERR] at   java.lang.reflect.Method.invoke(Method.java:597)
  10:07:35,239错误   [STDERR]在   org.jboss.aop.advice.PerJoinpointAdvice.invoke(PerJoinpointAdvice.java:174)
  10:07:35,239 ERROR [STDERR] at   org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
  10:07:35,239 ERROR [STDERR] at   org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.fillMethod(InvocationContextInterceptor.java:72)
  10:07:35,239 ERROR [STDERR] at   org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_fillMethod_613521570.invoke(InvocationContextInterceptor_z_fillMethod_613521570.java)
  10:07:35,239 ERROR [STDERR] at   org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
  10:07:35,239 ERROR [STDERR] at   org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.setup(InvocationContextInterceptor.java:88)
  10:07:35,239 ERROR [STDERR] at   org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_setup_613521570.invoke(InvocationContextInterceptor_z_setup_613521570.java)
  10:07:35,239 ERROR [STDERR] at   org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
  10:07:35,239 ERROR [STDERR] at   org.jboss.ejb3.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:62)
  10:07:35,239 ERROR [STDERR] at   org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
  10:07:35,239 ERROR [STDERR] at   org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:56)

3 个答案:

答案 0 :(得分:5)

我建议改写你的if语句:

if (!"".equals(history.getInvoiceNo()) || //String
    !"".equals(history.getCcEmail()) || //String
    !"".equals(history.getHostAuthCode()) || //String
    history.getMerchantCategory() != null || //String
    !"".equals(history.getCcName()) || //String
    !"".equals(history.getCcPhone()) || //String
    history.getBatchNo() != 0 || //int
    !"".equals(history.getIpAddress()) || //String
    history.getTransactionStatus() != null) {

当您与空字符串进行比较时,使用equalsIgnoreCase毫无意义,与equals进行比较时使用null毫无意义。

答案 1 :(得分:2)

永远不要使用.equals检查是否为null,总是使用==

如果你想检查一个字符串是空还是等于null,你应该这样做:

//Make sure a string isn't null
if(string != null && string.isEmpty())

.equals如果用于比较两个对象的值,那么在检查null的情况下,你想使用==检查该变量引用的内容

答案 2 :(得分:1)

我猜你的一个或多个get...方法正在返回null。您需要检查null以及空字符串。

比较null时,你正在进行参考比较,所以

somevariable == null

是对的。