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 null check in Iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "null check in Iterator" Watch "null check in Iterator" New topic
Author

null check in Iterator

Vatsa dude
Greenhorn

Joined: Apr 29, 2009
Posts: 22
I am puzzled why I am getting a NullPointer exception in the blow code - Error on line no 12. If I check for something like "abc", it works fine.

Calling code:


Saurabh Deshpande
Ranch Hand

Joined: Aug 09, 2007
Posts: 33
On line 12 you are trying to compare s (null object) with another string ("null" is a string)
You have to check if s is not null before you do that.
You can use if (s==null) instead.


Saurabh Deshpande.
SCJP, SCWCD, SCEA Part 1
Doing Part 2 now....
Vatsa dude
Greenhorn

Joined: Apr 29, 2009
Posts: 22
Same exception with:

. I think its some strange behavior with the Iterator, but I cannot figure out the exact reason.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Sri Vatsa wrote:Same exception with:

. I think its some strange behavior with the Iterator, but I cannot figure out the exact reason.


I think you should check that again or post your code. No NPE will occurs if you replace

with


Although the code still doesn't do what you want it to do.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Vatsa dude
Greenhorn

Joined: Apr 29, 2009
Posts: 22
Sorry, my bad.

does the trick..
Method is supposed to return Iterator<String> with null values removed. Can you please suggest a better solution?

Thanks
Sri
Mohammed Yousuff
Ranch Hand

Joined: Oct 17, 2007
Posts: 198
Sri Vatsa can you please tell us what exactly your method has to do?? so that i can help on that


My Thoughts : http://passion4java.blogspot.com
Try not to become a man of success but rather to become a man of value.
Vivek Singh
Ranch Hand

Joined: Oct 27, 2009
Posts: 92
Sri Vatsa wrote:


You are trying to apply equals method to a NULL . So it throws Null pointer Exception..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

Note that the Iterator returned by this method is useless. It is already at the end, and cannot get to the start anymore. As such, every call to hasNext() will return false and you cannot get anything from it anymore.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
If you really need the equals method, you can turn it round

. . . if ("null".equals(s)) . . .
Vatsa dude
Greenhorn

Joined: Apr 29, 2009
Posts: 22
null error is resolved..But,I see most of you commenting that the Iterator is not coded perfectly. Here's the requirement (an extra step to the initial requirement). Not sure how to proceed in the most efficient way..Can you please help?

Calling code- client



Actual method implementation

Vatsa dude
Greenhorn

Joined: Apr 29, 2009
Posts: 22
Can some one please suggest a solution. I am blank having tried this for a long time..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
You will have to go through the API specifications for Iterator. Can you re-interpret that as meaning "if you have a List which contains null values this Iterator misses them out"? You will have to decide for yourself, but I would think yes, and the interpretation is like this:
  • You have created a subclass of a List class, maybe called ArrayListSkippingNull<E>
  • You write the documentation comments saying this class accepts null insertions, but its Iterator omits nulls when iterating.
  • You create a private inner class which implements the ListIterator<E> interface.
  • It has an index field which represents how many elements have been passed already (start = 0, end = n - 1).
  • Its hasNext() method counts forward, increaasing the index while the next reference is null, and returns true if you find a non-null reference before the end of the List.
  • Its hasPrevious method does the same in the other direction.
  • Its next method goes forward to find the next non-null reference (or throws a NoSuchElementException) incrementing the index if nulls are passed.
  • Its previous method does the same in the other direction.
  • Its remove method takes an element out of the List and moves all the other elements to fill in the gap and reduces the size of the List.
  • Your iterator and listIterator methods return an instance of that class.
  • I have never tried that, so I don't know whether it will work in your situation.
    Arjun Abhishek
    Ranch Hand

    Joined: Jul 08, 2008
    Posts: 57
    Hi

    Please check if this code piece helps you.




    cheers
    K

     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: null check in Iterator