File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullpointerException in ArrayList,Vector,LinkedList

 
rex tony
Ranch Hand
Posts: 159
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic