• Post Reply Bookmark Topic Watch Topic
  • New Topic

plz help me in using a ListIterator

 
Amarender Reddy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is the code I used to implement the ListIterator interface.
=========================================================================
import java.io.*;
import java.util.*;

public class ListIteratorExample
{
public static void main(String rags[])
{
ListIterator li=null;
try
{
for(int i=1;i<10;i++)
{
Integer in=new Integer(i);
li.add(in);
}
}catch(Exception e){e.printStackTrace();}

System.out.println("PRINTING LISTITERATOR ELEMENTS");
while(li.hasNext())
System.out.println(li.next());

}

}
==================================================================

It throws a NullPointerException at line num. 20 and 25 printing the line "PRINTING LISTITERATOR ELEMENTS".Plz help me rectify the error.
Thanking you.
 
Henry Wong
author
Sheriff
Posts: 22644
111
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
li = null, so, li.add(in);, throws a null pointer exception.

Henry
 
Amarender Reddy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry!
But if not for "li=null"...it gives an error saying ListIterator should be initialised. Whats up then!
 
Ragu Ram
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope this helps
 
Amarender Reddy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Ram!

its not "li.next()" in the last line but "listIter.next()" as ArrayList do

not support any next() method. Anyhave thank you once again ....its working.

cheers
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!