Results 1 to 7 of 7
- 07-16-2009, 11:10 AM #1
Member
- Join Date
- Jul 2009
- Posts
- 3
- Rep Power
- 0
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
Java Code:// LinkedList class // // CONSTRUCTION: with no initializer // Access is via LinkedListIterator class // // ******************PUBLIC OPERATIONS********************* // boolean isEmpty( ) --> Return true if empty; else false // void makeEmpty( ) --> Remove all items // LinkedListIterator zeroth( ) // --> Return position to prior to first // LinkedListIterator first( ) // --> Return first position // void insert( x, p ) --> Insert x after current iterator position p // void remove( x ) --> Remove x // LinkedListIterator find( x ) // --> Return position that views x // LinkedListIterator findPrevious( x ) // --> Return position prior to x // ******************ERRORS******************************** // No special errors /** * Linked list implementation of the list * using a header node. * Access to the list is via LinkedListIterator. * @author Mark Allen Weiss * @see LinkedListIterator */ import java.io.*; class BeamNode { int x; int y; } public class LinkedList { /** * Construct the list */ public LinkedList( ) { header = new ListNode( null ); } /** * Test if the list is logically empty. * @return true if empty, false otherwise. */ public boolean isEmpty( ) { return header.next == null; } /** * Make the list logically empty. */ public void makeEmpty( ) { header.next = null; } /** * Return an iterator representing the header node. */ public LinkedListIterator zeroth( ) { return new LinkedListIterator( header ); } /** * Return an iterator representing the first node in the list. * This operation is valid for empty lists. */ public LinkedListIterator first( ) { return new LinkedListIterator( header.next ); } /** * Insert after p. * @param x the item to insert. * @param p the position prior to the newly inserted item. */ public void insert( Object x, LinkedListIterator p ) { if( p != null && p.current != null ) p.current.next = new ListNode( x, p.current.next ); } /** * Return iterator corresponding to the first node containing an item. * @param x the item to search for. * @return an iterator; iterator is not valid if item is not found. */ public LinkedListIterator find( Object x ) { ListNode itr = header.next; while( itr != null && !itr.element.equals( x ) ) itr = itr.next; return new LinkedListIterator( itr ); } /** * Return iterator prior to the first node containing an item. * @param x the item to search for. * @return appropriate iterator if the item is found. Otherwise, the * iterator corresponding to the last element in the list is returned. */ public LinkedListIterator findPrevious( Object x ) { ListNode itr = header; while( itr.next != null && !itr.next.element.equals( x ) ) itr = itr.next; return new LinkedListIterator( itr ); } /** * Remove the first occurrence of an item. * @param x the item to remove. */ public void remove( Object x ) { LinkedListIterator p = findPrevious( x ); if( p.current.next != null ) p.current.next = p.current.next.next; // Bypass deleted node } // Simple print method public static void printList( LinkedList theList ) { if( theList.isEmpty( ) ) System.out.print( "Empty list" ); else { LinkedListIterator itr = theList.first( ); for( ; itr.isValid( ); itr.advance( ) ) System.out.print( itr.retrieve( ) + " " ); } System.out.println( ); } private ListNode header; // In this routine, LinkedList and LinkedListIterator are the // classes written in Section 17.2. public static int listSize( LinkedList theList ) { LinkedListIterator itr; int size = 0; for( itr = theList.first(); itr.isValid(); itr.advance() ) size++; return size; } public static void main( String [ ] args ) { LinkedList theList = new LinkedList( ); LinkedListIterator theItr; BeamNode[] b = new BeamNode[3]; int i,x=0,y=0; String line1 = null,line2 = null; theItr = theList.zeroth( ); printList( theList ); for( i = 1; i < 4; i++ ) { try { BufferedReader is = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the value of x\n"); line1 = is.readLine(); [COLOR="Red"]b[i].x = Integer.parseInt(line1);[/COLOR] System.out.println("Enter the value of y\n"); line2 = is.readLine(); b[i].y = Integer.parseInt(line2); } catch (NumberFormatException ex) { System.err.println("Not a valid number: " + line1); } catch (IOException e) { System.err.println("Unexpected IO ERROR: " + e); } theList.insert( b[i], theItr ); // printList( theList ); theItr.advance( ); } System.out.println( "Size was: " + listSize( theList ) ); /* for( i = 0; i < 10; i += 2 ) theList.remove( new Integer( i ) ); for( i = 0; i < 10; i++ ) if( ( i % 2 == 0 ) == ( theList.find( new Integer( i ) ).isValid( ) ) ) System.out.println( "Find fails!" ); System.out.println( "Finished deletions" );*/ printList( theList ); } } // Basic node stored in a linked list // Note that this class is not accessible outside // of package weiss.nonstandard class ListNode { // Constructors public ListNode( Object theElement ) { this( theElement, null ); } public ListNode( Object theElement, ListNode n ) { element = theElement; next = n; } public Object element; public ListNode next; }Java Code:// LinkedListIterator class; maintains "current position" // // CONSTRUCTION: Package visible only, with a ListNode // // ******************PUBLIC OPERATIONS********************* // void advance( ) --> Advance // boolean isValid( ) --> True if at valid position in list // Object retrieve --> Return item in current position /** * Linked list implementation of the list iterator * using a header node. * @author Mark Allen Weiss * @see LinkedList */ public class LinkedListIterator { /** * Construct the list iterator * @param theNode any node in the linked list. */ LinkedListIterator( ListNode theNode ) { current = theNode; } /** * Test if the current position is a valid position in the list. * @return true if the current position is valid. */ public boolean isValid( ) { return current != null; } /** * Return the item stored in the current position. * @return the stored item or null if the current position * is not in the list. */ public Object retrieve( ) { return isValid( ) ? current.element : null; } /** * Advance the current position to the next node in the list. * If the current position is null, then do nothing. */ public void advance( ) { if( isValid( ) ) current = current.next; } ListNode current; // Current position }
- 07-16-2009, 11:30 AM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
It would be good, if you place your code inside code tags and let us know the line number where you are getting the exception.
- 07-16-2009, 01:53 PM #3
Member
- Join Date
- Jul 2009
- Posts
- 3
- Rep Power
- 0
getting error in line no 166 as Exception in thread "main" java.lang.NullPointerExce
[QUOTE=kavitha_0821;76152]
Java Code:// LinkedList class // // CONSTRUCTION: with no initializer // Access is via LinkedListIterator class // // ******************PUBLIC OPERATIONS********************* // boolean isEmpty( ) --> Return true if empty; else false // void makeEmpty( ) --> Remove all items // LinkedListIterator zeroth( ) // --> Return position to prior to first // LinkedListIterator first( ) // --> Return first position // void insert( x, p ) --> Insert x after current iterator position p // void remove( x ) --> Remove x // LinkedListIterator find( x ) // --> Return position that views x // LinkedListIterator findPrevious( x ) // --> Return position prior to x // ******************ERRORS******************************** // No special errors /** * Linked list implementation of the list * using a header node. * Access to the list is via LinkedListIterator. * @author Mark Allen Weiss * @see LinkedListIterator */ import java.io.*; class BeamNode { int x; int y; } public class LinkedList { /** * Construct the list */ public LinkedList( ) { header = new ListNode( null ); } /** * Test if the list is logically empty. * @return true if empty, false otherwise. */ public boolean isEmpty( ) { return header.next == null; } /** * Make the list logically empty. */ public void makeEmpty( ) { header.next = null; } /** * Return an iterator representing the header node. */ public LinkedListIterator zeroth( ) { return new LinkedListIterator( header ); } /** * Return an iterator representing the first node in the list. * This operation is valid for empty lists. */ public LinkedListIterator first( ) { return new LinkedListIterator( header.next ); } /** * Insert after p. * @param x the item to insert. * @param p the position prior to the newly inserted item. */ public void insert( Object x, LinkedListIterator p ) { if( p != null && p.current != null ) p.current.next = new ListNode( x, p.current.next ); } /** * Return iterator corresponding to the first node containing an item. * @param x the item to search for. * @return an iterator; iterator is not valid if item is not found. */ public LinkedListIterator find( Object x ) { ListNode itr = header.next; while( itr != null && !itr.element.equals( x ) ) itr = itr.next; return new LinkedListIterator( itr ); } /** * Return iterator prior to the first node containing an item. * @param x the item to search for. * @return appropriate iterator if the item is found. Otherwise, the * iterator corresponding to the last element in the list is returned. */ public LinkedListIterator findPrevious( Object x ) { ListNode itr = header; while( itr.next != null && !itr.next.element.equals( x ) ) itr = itr.next; return new LinkedListIterator( itr ); } /** * Remove the first occurrence of an item. * @param x the item to remove. */ public void remove( Object x ) { LinkedListIterator p = findPrevious( x ); if( p.current.next != null ) p.current.next = p.current.next.next; // Bypass deleted node } // Simple print method public static void printList( LinkedList theList ) { if( theList.isEmpty( ) ) System.out.print( "Empty list" ); else { LinkedListIterator itr = theList.first( ); for( ; itr.isValid( ); itr.advance( ) ) System.out.print( itr.retrieve( ) + " " ); } System.out.println( ); } private ListNode header; // In this routine, LinkedList and LinkedListIterator are the // classes written in Section 17.2. public static int listSize( LinkedList theList ) { LinkedListIterator itr; int size = 0; for( itr = theList.first(); itr.isValid(); itr.advance() ) size++; return size; } public static void main( String [ ] args ) { LinkedList theList = new LinkedList( ); LinkedListIterator theItr; BeamNode[] b = new BeamNode[3]; int i,x=0,y=0; String line1 = null,line2 = null; theItr = theList.zeroth( ); printList( theList ); for( i = 1; i < 4; i++ ) { try { BufferedReader is = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the value of x\n"); line1 = is.readLine(); [COLOR="Red"]b[i].x = Integer.parseInt(line1);[/COLOR] System.out.println("Enter the value of y\n"); line2 = is.readLine(); b[i].y = Integer.parseInt(line2); } catch (NumberFormatException ex) { System.err.println("Not a valid number: " + line1); } catch (IOException e) { System.err.println("Unexpected IO ERROR: " + e); } theList.insert( b[i], theItr ); // printList( theList ); theItr.advance( ); } System.out.println( "Size was: " + listSize( theList ) ); /* for( i = 0; i < 10; i += 2 ) theList.remove( new Integer( i ) ); for( i = 0; i < 10; i++ ) if( ( i % 2 == 0 ) == ( theList.find( new Integer( i ) ).isValid( ) ) ) System.out.println( "Find fails!" ); System.out.println( "Finished deletions" );*/ printList( theList ); } } // Basic node stored in a linked list // Note that this class is not accessible outside // of package weiss.nonstandard class ListNode { // Constructors public ListNode( Object theElement ) { this( theElement, null ); } public ListNode( Object theElement, ListNode n ) { element = theElement; next = n; } public Object element; public ListNode next; }Java Code:// LinkedListIterator class; maintains "current position" // // CONSTRUCTION: Package visible only, with a ListNode // // ******************PUBLIC OPERATIONS********************* // void advance( ) --> Advance // boolean isValid( ) --> True if at valid position in list // Object retrieve --> Return item in current position /** * Linked list implementation of the list iterator * using a header node. * @author Mark Allen Weiss * @see LinkedList */ public class LinkedListIterator { /** * Construct the list iterator * @param theNode any node in the linked list. */ LinkedListIterator( ListNode theNode ) { current = theNode; } /** * Test if the current position is a valid position in the list. * @return true if the current position is valid. */ public boolean isValid( ) { return current != null; } /** * Return the item stored in the current position. * @return the stored item or null if the current position * is not in the list. */ public Object retrieve( ) { return isValid( ) ? current.element : null; } /** * Advance the current position to the next node in the list. * If the current position is null, then do nothing. */ public void advance( ) { if( isValid( ) ) current = current.next; } ListNode current; // Current position }
- 07-16-2009, 02:11 PM #4
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
You have not used the code tags yet. However if you are talking about the red colored line, it has to raise an exception. The array what you have created is of size 3, means accessible elements are 0,1,2. However your for loop iterates from 1 to 4 and in the loop you are using the x variable to access the array. So the loop will try to access the array elements as b[1](no probs), b[2](no probs), b[3] (here is the problem) because accessible array elements are 0,1,2.
-
Your BeamNode array, b, goes from 0 to 2 (with new BeamNode[3], and yet you loop from 1 to 4:
a NullPointerException should be expected here.Java Code:for( i = 1; i < 4; i++ )
HTH.
- 07-16-2009, 03:28 PM #6
Hi,
As Fubarable said ur looping is wrong.
Added to this one more problem in ur code.U are having
BeamNode[] b = new BeamNode[3];
Here u are having array of references not object.But u tried to access like this b[0].x.Even this is a problem
-Regards
RamyaRamya:cool:
-
Similar Threads
-
Exception in thread "main" java.lang.NullPointerException
By Sudde-J in forum EclipseReplies: 7Last Post: 04-18-2011, 02:24 PM -
Exception in thread "main" java.lang,NullPointerException
By ljk8950 in forum AWT / SwingReplies: 15Last Post: 10-12-2010, 05:51 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 1Last Post: 07-16-2009, 10:35 AM -
Exception in thread "main" java.lang.NullPointerException
By farooqhussain786 in forum New To JavaReplies: 10Last Post: 04-03-2009, 06:31 AM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 06:32 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks