| Author |
NullpointerException in ArrayList,Vector,LinkedList
|
rex tony
Ranch Hand
Joined: Aug 29, 2007
Posts: 159
|
|
In ArrayList,Vector,LinkedList,In which ,Why we cant store the null value but we can store in a "SPACE"?.How the compiler treaded as a "SPACE"? If i add the null,at the runtime throws NullpointerException?
|
 |
Thirugnanam Saravanan
Ranch Hand
Joined: Dec 13, 2007
Posts: 81
|
|
SPACE ( " " ) , which you have used in your Code is treated as a "Empty String" which is still a valid Java Object for the Compiler. But null is not a valid Java Object which can not be stored in ArrayList and Vector.
|
Saravanan
SCJP 5.0(98%), SCWCD 5.0 (100%), OCA
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
As Thirugnanam says, a space is something entirely different than a null reference.
Originally posted by Thirugnanam Saravanan: But null is not a valid Java Object which can not be stored in ArrayList and Vector.
That is not correct. You can store null in a Vector or ArrayList without problems. The API documentation of ArrayList says:
Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null.
The problem is not in the fact that you try to store null in an ArrayList or Vector. You are calling Collections.sort(...) on the ArrayList, and that method cannot handle null elements. Look at the stack trace of the exception message that you get when you run your code. You will see that the exception happens in the line with Collections.sort(AL).
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: NullpointerException in ArrayList,Vector,LinkedList
|
|
|