Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I am just beginning java programming. I have got a Null Pointer exception problem in the array List My code is below

ArrayList<Arpaymentitem> arpaymentitemsList= jb.getArpaymentitems();
arpaymentitemsList.removeAll(Collections.singleton(null));

            try {
                for(Arpaymentitem arpaymentitem:arpaymentitemsList)
                {

                    if (arpaymentitem.getInvoicekey()!=null) {
                        statement2.setString(1,arpaymentitem.getInvoicekey());
                    }
                    if(arpaymentitem.getInvoicekey() != null)
                        {
                        statement2.setString(2,arpaymentitem.getAmount());
                        }
                        }
                    statement2.addBatch();
                }
             catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

I am getting null pointer exception error in the FOR statement. I am sure there are some null values in the list. To ignore the null values I have introduced a step

ArrayList<Arpaymentitem> arpaymentitemsList= jb.getArpaymentitems();
arpaymentitemsList.removeAll(Collections.singleton(null)); --> remove nulls

The NPE I got is java.lang.NullPointerException at payment.Intacct_Payment.main(Intacct_Payment.java:169) and the line is for(Arpaymentitem arpaymentitem:arpaymentitemsList)

The problem still persists. What am I doing wrong here.

share|improve this question
    
Are you sure this : arpaymentitemsList is not null? – Stultuske Feb 10 at 12:07
    
I have introduced this arpaymentitemsList.removeAll(Collections.singleton(null)); to remove null values. Is that not right? – Karthi Feb 10 at 12:08
    
not if the list itself is not instantiated, then you just throw a NPE on a different location. – Stultuske Feb 10 at 12:09
2  
Where is the stack trace of the exception. What line does it refer to? – JB Nizet Feb 10 at 12:10
2  
You're not telling us the truth. If the exception was thown at the for() statement, the only possible reason would be that arpaymentitemsList is null. And it can't be null, otherwise an NPE would have been thrown the line before. Indent your code, recompile it, rerun it, and post the exact and complete stack tracxe. – JB Nizet Feb 10 at 12:23

1 Answer 1

up vote 1 down vote accepted

Can you try this follwing steps and give a feedback

ArrayList<Arpaymentitem> arpaymentitemsList = new ArrayList<>();
if (jb.getArpaymentitems().size()> 0)
{
        for (Arpaymentitem arpaymentitem : jb.getArpaymentitems()) 
        {
              System.out.println("Description: ...");
              arpaymentitemsList.add(arpaymentitem)
        }
}
//The Rest of the code
share|improve this answer
    
I am getting NPE at if (jb.getArpaymentitems().size()> 0) – Karthi Feb 10 at 12:35
    
That means the problem is in the input jb.getArpaymentitems(). Can you print the values of jb.getArpaymentitems() using for-each condition or check for null? – Saqib Rezwan Feb 10 at 14:26
    
Yes. I am printing the data using a foreach loop. After getting some 180 records I am getting null value exception. How can I skip checking for null values. or I can stop the loop if the null value is reached? – Karthi Feb 10 at 15:49
    
Use try-catch(NullPointerException) to skip or end the loop. Put this try-catch inside the the loop, not outside. EXAMPLE: for (Arpaymentitem arpaymentitem : jb.getArpaymentitems()) { try { ..... } catch(NullpointerException e) { return; } } – Saqib Rezwan Feb 10 at 17:26
    
This is right! @Saqib Rezwan. Got this working. – Karthi Feb 26 at 13:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.