-3

I have a condition like this :

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();
}

but when i dont fill any variable on that parameters, why it getting error like this :

10:07:35,238 INFO [STDOUT] : : : ERROR QUERYING HISTORY TO DATABASE
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(Native Method)
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 ERROR [STDERR] at 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 ERROR [STDERR] at 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)

apaul
  • 16,092
  • 8
  • 47
  • 82
Diaz Pradiananto
  • 447
  • 11
  • 21
  • 1
    please print all `history.` and see what you get... Might that will come to know why you are getting `Null Pointer Exception` – Fahim Parkar Feb 06 '12 at 03:23
  • May I ask what the reason is, to compare an empty String in case insensitve way: `.equalsIgnoreCase ("")`. Btw.: For a long time I haven't seen such an ugly code sample. – user unknown Feb 06 '12 at 03:23

3 Answers3

5

I'd suggest rewriting your if statement like this:

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) {

There's no point in using equalsIgnoreCase when you're comparing to the empty string, and there's no point in using equals when comparing to null.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
2

Never use .equals to check if something null, always use ==

If you want to check if a string is empty or equal to null you should do it like so:

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

.equals if used for comparing the value of two objects, so in the case of checking for null you want to use == to check what is being referenced by that variable

Istinra
  • 343
  • 4
  • 8
1

I''m guessing that one or more of your get... methods are returning null. You need to check for null as well as empty strings.

When comparing for null you're doing a reference comparison so

somevariable == null

is correct.

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116