Results 1 to 6 of 6

Thread: Null pointer

  1. #1
    WSUVanc is offline Member
    Join Date
    Mar 2013
    Posts
    14
    Rep Power
    0

    Default Null pointer

    The most recent code, issue has nothing to due with null pointers anymore, wish i could change posting title.

    Java Code:
    public class LinkedList {
    
    	private Node head;
    	private Node tail;
    	
    	public LinkedList(Node firstelement) {
    		head = firstelement;
    		tail = firstelement;
    	}
    	
    	public Node first() {
    		return head;
    	}
    	
    	public void appendToFront(Node another) {
    		another.next = head;
    		head = another;
    		
    	}
    	public void append(Node another) {
    		tail.next = another;
    		tail = tail.next;
    	}
    	
    	public static class Node {
    
    		public String data;
    		private Node next;
    		
    		public Node(String s) {
    			data = s;
    		}
    	}
    	
    	public static class LinkedListIterator {
    
    		private Node current;
    		private Node reset;
    		private Node last;
    		
    		
    		
    		public LinkedListIterator(LinkedList l) {
    			current = l.head;
    			reset = l.head;
    			last=l.tail;
    		}
    		
    		public boolean hasNext() {
    			// return true iff there is another node in the list
    			if (current != null) return true;
    			return false;
    			
    		}
    		public Node next() {
    			// return the 'next' node and advance
    			Node thisone = current;
    			current=current.next;
    			return thisone;
    			
    			
    		}
    		
    		
    		public String dequeue(){
    			if(hasNext()){
    			String popper=next().data;
    			return popper;
    			}
    			current=reset;
    			return "Empty";
    		}
    		public void enqueue(String input){
    			Node queue=new Node(input);
    			last.next = queue;
    			last = last.next;	
    		}
    	}
    	
    
    	public static void main(String []args) {
    		
    		LinkedList l = new LinkedList(new Node("Hello"));
    		LinkedListIterator i = new LinkedListIterator(l);
    		i.enqueue("bought a car");
    		i.enqueue("its a junker");
    		i.enqueue("I bought a boat");
    		
    		
    		while(i.hasNext()){
    		System.out.println(i.dequeue());
    		
    		}
    		System.out.println(i.dequeue());
    		
    		i.enqueue("Second List");
    		i.enqueue("Starts here");
    		
    		System.out.println(i.dequeue());
    		
    		while(i.hasNext()){
    			System.out.println(i.dequeue());
    		}
    	}
    }
    Last edited by WSUVanc; 03-15-2013 at 04:43 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,949
    Rep Power
    4

    Default Re: Null pointer

    queue is supposed to be an instance of Node but I don't see where you initialized it.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    WSUVanc is offline Member
    Join Date
    Mar 2013
    Posts
    14
    Rep Power
    0

    Default Re: Null pointer

    i didn't on the top one, and i think that was the initial problem with that one, but i did on line 74 on the revised code. Thanks for the comment though, it would have saved me an hour or so if someone would have said that earlier. The problem i am having now is that after i dequeue everything in my queue, Im not sure how to get rid of it. I put reset in there, but the original elements pop up in my next print out.

    This is the print out.

    Hello
    bought a car
    its a junker
    I bought a boat
    Empty
    Hello
    bought a car
    its a junker
    I bought a boat
    Second List
    Starts here

    After i hit empty i suppose to clear and reset the queue then fill it again. Or after ever dequeue delete the element that was dequeued. Not sure which approach would be the best.
    Last edited by WSUVanc; 03-15-2013 at 02:04 AM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,949
    Rep Power
    4

    Default Re: Null pointer

    Nope. Line 40 in the original post just declares it. However, it looks like you added it to the method enqueue().

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    WSUVanc is offline Member
    Join Date
    Mar 2013
    Posts
    14
    Rep Power
    0

    Default Re: Null pointer

    I saw that when replying and deleted that post, apparently after you started replying though lol.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,949
    Rep Power
    4

    Default Re: Null pointer

    Yep! Ships in the night. Happens a lot. LOL

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Null pointer exception
    By Domo230 in forum New To Java
    Replies: 4
    Last Post: 02-28-2011, 10:21 AM
  2. Null pointer exception
    By jessie in forum New To Java
    Replies: 5
    Last Post: 02-08-2011, 02:58 PM
  3. Null Pointer
    By theen3my in forum AWT / Swing
    Replies: 3
    Last Post: 10-03-2009, 02:10 PM
  4. null pointer exception
    By jyothi.priyanka in forum New To Java
    Replies: 12
    Last Post: 03-11-2009, 05:04 PM
  5. null pointer help
    By mayhewj7 in forum New To Java
    Replies: 5
    Last Post: 02-17-2009, 11:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •