2

Please help me explaning this. I can't seem to figure out why this produce a null pointer exception

/* Try to find customer in customer list, if not in list add to list with a 
new plan; otherwise add additional plan to customer*/


for(int a = 0; a < dataset.length; a++){

       if(customer_name_list.contains(dataset[a][CLIENT_NAME])){
            int temp_index = 0;

//NULLPOINTEREXCEPTION OCCURRED ON THE FOLLOWING LINE 
            while(!customer.get(temp_index).name.equals(dataset[a][CLIENT_NAME])){
                temp_index++;
            }
            customer.get(temp_index).add_plan(dataset[a][PLAN], dataset[a][DETAIL]);
       }

       else{
                Customer temp_customer = new Customer(dataset[a][CLIENT_NAME], dataset[a][PLAN], dataset[a][DETAIL]);

       customer.add(temp_customer);
       customer_name_list.add(dataset[a][CLIENT_NAME]);
       }
   }

Thank you for your help

6
  • Looks like you should be using a Map, not a List. Commented Jan 31, 2011 at 0:57
  • Can you post the stack trace as well please? Commented Jan 31, 2011 at 1:01
  • Found client :QBE Holdings Inc. at wisconsinrx.mainuserinterface.populate_helper(mainuserinterface.java:498) at wisconsinrx.mainuserinterface.v_button_opendataActionPerformed(mainuserinterface.java:546) at wisconsinrx.mainuserinterface.access$000(mainuserinterface.java:32) at wisconsinrx.mainuserinterface$1.actionPerformed(mainuserinterface.java:172) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) Commented Jan 31, 2011 at 1:07
  • at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) at java.awt.Component.processMouseEvent(Component.java:6267) at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) at java.awt.Component.processEvent(Component.java:6032) at java.awt.Container.processEvent(Container.java:2041) Commented Jan 31, 2011 at 1:08
  • I don't think the stack trace helped much. Commented Jan 31, 2011 at 1:09

4 Answers 4

2

Taking this as the line the exception occurs on:

while(!customer.get(temp_index).name.equals(dataset[a][CLIENT_NAME])){

There's a number of things that could be calling the issue. First off, customer could be null. Second, the results of customer.get(temp_index) could be null. Finally, customer.get(temp_index).name could also be null.

Since we're not working with the full code set here, I suggest you step through and print out the values to each of the above things to work out what one's null (or use a debugger.) That way you'll be able to see exactly what's causing the exception. My guess is that a customer's name may well be set to null which would cause the issue, but it could just as easily be any of the other things I mentioned.

Sign up to request clarification or add additional context in comments.

2 Comments

CLIENT_NAME appears to be an int and it isn't null (if it's an unboxed Integer) because it is used a few lines earlier. :) But other than that you are correct.
I was going with the fact it could be an Integer, however I missed the previous use. Thanks for pointing that out, I'll edit appropriately.
1

If it isn't found you don't exit the loop, instead you keep going. Try adding a counter or clause so you don't go over.

Comments

1

Either your customer collection, or one of its elements, or the name field of one of its elements is null. I can't really say anything more without seeing what goes in the customer collection.

But a good idea is to print out temp_index in the loop in some kind of log file so you can see how many iterations does it complete before falling over.

Another thing you might find useful is to try to avoid multiple dereferencing operators (.) on the same line, especially if you know little about the data that is being accessed.

You could create a helper method for example:

 private String getCustomerName( int index ) {
   Customer c = customer.get( index );
   return c.name;
 }

And use it in your while loop.

Although unrelated, but as others have pointed out, your loop will end in an IndexOutOfBoundsException if no matching elements are found, so you'll have to fix that too.

Comments

1
for (dataA : dataset) {
 if (customer_name_list.contains(dataA[CLIENT_NAME])) {
  for (c : customer) if (dataA[CLIENT_NAME].equals(c.name)) {
   c.add_plan(dataA[PLAN], dataA[DETAIL]);
   break;
  }
 } else {
  customer.add(new Customer(dataA[CLIENT_NAME], dataA[PLAN], dataA[DETAIL]));
  customer_name_list.add(dataA[CLIENT_NAME]);
 }
}

That's what you're doing, only cleaner. I suggest you might want to switch to either a hashing data structure (e.g., a HashMap) or a sorted data structure to improve your lookup and search calls. Then the above would look like

for (data : dataset) {
 if ((Customer c = customer_map.get(data[CLIENT_NAME]))!=null) {
  c.add_plan(data[PLAN], data[DETAIL]);
 } else {
  customer_map.put(data[CLIENT_NAME], new Customer(data[CLIENT_NAME], data[PLAN], data[DETAIL]));
 }
}

which, in addition to be easier on the brain, is nicer performance-wise. If you need other stuff, like insert-order based iteration, you can use the appropriate map flavor.

As to the NPE, that means an item you've fetched isn't properly initialized - either the item at temp_index is null (and getting name throws the NPE), or name is null and trying to call equals on it throws the NPE. If the index weren't there, you'd be getting an out-of-bounds style exception.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.