• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Auto boxing in enhanced for loop

 
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this following code:

[Added code tags - see UseCodeTags for details]
Now, I get NullPointerException on executing above program. I do get it, its something to do with autoboxing in enhanced for loop, as if I try the same program with traditional for loop it will print null 2 times. Now

1> What happens in enhanced for loop, that it throws NPE?
2> If I change the code in Line 1 to a=al.toArray(a); than, code is fine printing 1,2,3 why is autoboxing problem does not arise here? as per my understanding of toArray() method which has been called on collection of Integer and the array passed to it is also an integer, will return an Integer.
 
Rancher
Posts: 4801
50
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The toArray method of ArrayList only populates the supplied array if it is large enough to hold all the elements, otherwise it creates a new one of the correct size and returns that.
So your supplied [] is never populated, so all the entries are null.

See the API.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:The toArray method of ArrayList only populates the supplied array if it is large enough to hold all the elements, otherwise it creates a new one of the correct size and returns that.
So your supplied [] is never populated, so all the entries are null.


@Prathima - And on a more general note: it's rarely a good idea to ignore what a method returns. There are (rare) cases where it can be done; but this isn't one of them.

FYI: The quickest way to create a properly typed array from a collection is (using your names):
Integer[] a = a1.toArray(new Integer[a1.size()]);

Winston
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My first question was not based on returned value of toArray() method, I know that in my program a is never been altered. It will be Integer a={null,null} through out the program.

I will explain traditional loop of my understanding, I expecting similar explanation for enhanced for loop.



in the above code:
1> i iterates through 0 to 2 i.e i=0,1.
2> As a.length returns int value there is no issue with unboxing.
3> a[i] which in my case is null, prints 2 times once when i=0 and 2nd time when i=1.

Now my query, can any one explain me what's happening in enhanced for loop such that it is throwing NPE? I hope my second query is based on me getting clear picture of my first query.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, if your question didn't have anything to do with converting the ArrayList to an array, then why did you add that? You could have made it simpler without the distracting ArrayList:


Why this throws NullPointerException: The compiler auto-unboxes the Integer objects, and when you auto-unbox an Integer object that is null, you get a NullPointerException. The compiler automatically translates your loop to this:

You see here that if an element of the array is null, Java will try to call a method (intValue()) on a reference that is null, which will cause a NullPointerException.

Note that primitive types such as int cannot be null.


 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jasper, It was very useful. Some times even though we know the basic things a big messy code make us get things confused, and we end up with too many queries or doubt about the things which we think we know. This happened with me, in this case. I have been to JLS of enhanced for loop several times, but dint realise that its element.intValue() i.e causing the NEP, instead of I started to think, a=toArray(a) might be doing unboxing from Integer to int, and than initializing it to int i:a will work fine. Any ways, Sorry for getting confused and messing up.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prathima gaitonde wrote:Any ways, Sorry for getting confused and messing up.


No probs. I hope you also realise that what Dave said was quite correct. If you had converted your ArrayList correctly, you wouldn't have had the problem, because the array would have contained actual Integers, not nulls.

Winston
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dave Tolls, thank you for the link. It didn't show up properly because the forum software mistook the ] in T[] for a closing ]. I sorted that out by replacing the ] by ] and I think it is working properly now.
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dave for your kind help.
 
reply
    Bookmark Topic Watch Topic
  • New Topic