File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Arrays.asList() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Arrays.asList()" Watch "Arrays.asList()" New topic
Author

Arrays.asList()

Dhana Sen
Ranch Hand

Joined: Mar 05, 2006
Posts: 33
Hi,

I have a doubt in Java Arrays.asList() method. I try to pass Store = null, It's returning [null] (It's considering as null a value and size as 1), then I tried with Store = "" (it's considering [] (empty as a value) and size will be 1) Is there any flaw in asList Method.


Sita Marivada
Greenhorn

Joined: Apr 01, 2010
Posts: 14
Arrays class has some bug in it. Sun has to fix that.
Be sure not to pass null values to Arrays.asList() while coding itself.


Sita Putta
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
This doesn't seem like a bug at all to me - the method is doing what you asked it to do. What did you think the result would be?

I can think of three possible responses to Arrays.asList(null):

1. Return a list with a null in it, length 1.

2. Return an empty list, length zero.

3. Return a null (calling length() woudl throw a NullPointerException)

It's not clear to me which of these might be what you want; all are potentially reasonable responses. But the first one seems the closest match to what your code actually says.
Sita Marivada
Greenhorn

Joined: Apr 01, 2010
Posts: 14
I would agree with the third result.

Return a null (calling length() woudl throw a NullPointerException)

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18113
    
  39


Plus... it is implement as a var-arg of generics. So, option 2 isn't really an option that would make sense.

Between option 1 and option 3, option 1 is definitely the better choice.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Oh, I left out:

4. Throw an exception (NullPointerException or IllegalArgumentException) - this is what I am usually inclined to do in my own code, if it's not a library used by others. I often try to write my code such that null is not considered a valid option.

Hentry Wong wrote:Plus... it is implement as a var-arg of generics. So, option 2 isn't really an option that would make sense.

I'm not sure why the var-arg or generics would be a problem here. Can you elaborate? Thanks.

Sita: so, what if you called

and store1 = "Apple", store2 = null, store3 = "Android". What should the result be?

1. return a List of ("Apple", null, "Android")

2a. return a List of ("Apple", "Android")

2b. return a List of (null)

3. return a null

4. throw a NullPointerException
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18113
    
  39

Mike Simmons wrote:
Hentry Wong wrote:Plus... it is implement as a var-arg of generics. So, option 2 isn't really an option that would make sense.

I'm not sure why the var-arg or generics would be a problem here. Can you elaborate? Thanks.


Bad wording on my part. Sorry.

Basically, I meant, with var-args and generics, that the method will handle an array of Objects. And with processing the var-arg, the compiler's two choices are ... (1) treat the null as an array of objects, or (2) create an array of objects using the parameters. And option 2 can't happen with either option.

Henry
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Ah, I see. I was thinking of option 2 as creating a List of whatever reference(s) are given, but silently removing the nulls. This is seen in 2a later. I don't think it's a good idea (unless maybe you rename the method to asListWithNullsRemoved()), but it would certainly be possible to implement.

Note that as far as the compiler is concerned, the compiler doesn't "know" that the variable store is null. It doesn't remember that as it compiles. It just knows that store is of type String, and has been definitely assigned before the call to Arrays.asList(). So the compiler will necessarily construct a call as if there's a single String parameter - inserted into an array since it's a vararg.
Saurav Sen
Greenhorn

Joined: Jul 12, 2010
Posts: 2
Interestingly, above code will through NPE if I do it as follows --
Arrays.asList(null);
But if I do it as --
String store = null;
Arrays.asList(store);
then it doesn't throw NPE instead creates an list with one string in it.

Definitely it is a bug with var-argument generic implementation in Java & certainly not a problem with Arrays.asList(T... a) implementation.
Explanation below --
1. var-arg generic is implemented at the level of compiler just like other features of generics. i.e. JVM has no concept of variable argument method & its existence.
2. the way it is implemented at the level of compiler is as follows --
Code [
String str1 = null;
String str2 = null;
String str3 = null;
Arrays.asList(str1, str2, str3):
]

Now if you open the byte code (i.e. decompiling) you will find that it is implemented in the following way --
bytecode [
String str1 = null;
String str2 = null;
String str3 = null;
Arrays.asList(new String[]{str1, str2, str3});
]

So, if you examine it carefully you can see that even though str1, str2, str3 are all NULL but at the time of execution Arrays.asList(....) doesn't fire NPE because it gets a new String[]{str1, str2, str3} array the element of which are NULL.
So, that is exactly is the reason why ---

Arrays.asList(null); throws NPE

but,
String store = null;
Arrays.asList(store);
doesn't thorw NPE.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19542
    
  16

Saurav Sen wrote:Arrays.asList(null); throws NPE

but,
String store = null;
Arrays.asList(store);
doesn't thorw NPE.

That's caused by the varargs. Arrays.asList(null) sees the null as the entire T[], not just one T element.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
The behavior you observe has nothing to do with generics, and everything to do with the implementation of varargs and that "million dollar mistake" null.



The moral of this story is, do your best to avoid passing/returning nulls .


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
 
Don't get me started about those stupid light bulbs.
 
subject: Arrays.asList()
 
Similar Threads
asList method not for primitives?
Exception while using arrays.aslist() method
Problem ArrayList
Arrays.asList odd behaviour
Collections :List.add throws runtime Exception(Unsupported Exceptions)