Results 1 to 8 of 8
- 04-10-2009, 03:50 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 5
- Rep Power
- 0
another NullPointerException in LinkedList class
Good morning to you all.
I've pulled an all nighter on this program and I'm getting close to being done except for this damn java.lang.NullPointerException! I've been trying like crazy to figure out what the hell is up, but I just have no idea.
Here's what it says;
Exception in thread "main" java.lang.NullPointerException
at LinkedList.add(LinkedList.java:145)
at LinkedList.main(LinkedList.java:236)
Here are my programs;
Java Code:// // LinkedList.java // // package csci1902.lab3; import csci1902.lab3.*; public class LinkedList { private boolean permitsDuplicates; private int sortOrder; private Node header; //Zeroth node, contains no data private Node listPTR; //The Node-pointer for pointNext and pointPrevious private int pointInt = 0; //I use this for pointNext and pointPrevious to see how long //listPTR has been at the end of the list public LinkedList(){ this.permitsDuplicates = true; this.sortOrder = 0; } public LinkedList(boolean permitsDuplicates, int sortOrder){ this.permitsDuplicates = permitsDuplicates; this.sortOrder = sortOrder; } public Object first(){ return header.getNext().getData(); } public Object last(){ Node ptr = header; while (ptr != null){ if(ptr.getNext() == null) return ptr.getData(); ptr = ptr.getNext(); } return null; } public int length(){ int Length = 0; Node ptr = header.getNext(); while (ptr != null){ Length++; ptr = ptr.getNext(); } return Length; } public boolean lookup(Object obj){ Node ptr = header.getNext(); while(ptr != null){ if(ptr.getData().equals(obj.toString())) return true; ptr = ptr.getNext(); } return false; } public Node get(int n){ Node ptr = header; int i = 0; if (n < 0) return null; while(ptr != null){ if (i == n) return ptr; else{ i++; ptr = ptr.getNext(); } } return null; } public Object getObj(int n){ return this.get(n).getData(); } public int find(Object obj){ Node ptr = header; int i = 0; while(ptr != null){ if (obj.equals(ptr.getData())) return i; else{ i++; ptr = ptr.getNext(); } } return 0; } public void delete(Object obj){ Node front = header.getNext(); Node behind = header; while(front != null){ if(obj.equals(front.getData())){ behind.setNext(front.getNext()); front.setNext(null); } else{ front = front.getNext(); behind = behind.getNext(); } } return; } public void delete(int n){ Node front = header.getNext(); Node behind = header; int i = 0; if (n < 0) return; while(front != null){ if (i == n){ behind.setNext(front.getNext()); front.setNext(null); return; } else{ i++; front = front.getNext(); behind = behind.getNext(); } } return; } public void add(Node newItem){ if(header.getNext() == null) // AROUND HERE EITHER THIS LINE or the one below header.setNext(newItem); Node front = header.getNext(); Node behind = header; while(front != null){ if(permitsDuplicates != true){ if (newItem.equals(front.getData())){ return;} } else if(sortOrder == 0){ newItem.setNext(behind.getNext()); behind.setNext(newItem); return; } else if(sortOrder == -1){ if(newItem.getData().toString().compareTo(front.getData().toString()) >= 0){ newItem.setNext(behind.getNext()); behind.setNext(newItem); return; } } else if(sortOrder == 1){ if(newItem.getData().toString().compareTo(front.getData().toString()) <= 0){ newItem.setNext(behind.getNext()); behind.setNext(newItem); return; } } else{ front = front.getNext(); behind = behind.getNext(); } } newItem.setNext(behind.getNext()); behind.setNext(newItem); return; } public Object pointNext(){ if(this.length() == 0){ //Empty LL return null; } else if(this.length() == 1){ //First Item listPTR.setNext(header.getNext()); return listPTR.getNext(); } else if(listPTR.getNext().getNext() == null){ //On last item, next item null pointInt++; return null; } else{ listPTR.setNext(listPTR.getNext().getNext()); return listPTR.getNext(); } } public Object pointPrevious(){ Node ptr = header.getNext(); Node test = listPTR.getNext(); int listPTRIndex = 0; //This is how many Nodes away from the end that listPTR is int previousIndex = 0; if(this.length() == 0){ return null; } else if(listPTR.getNext().getNext() == null && pointInt > 0){ pointInt = 0; return listPTR.getNext(); } else{ while(test != null){ listPTRIndex++; test = test.getNext(); } previousIndex = this.length() - listPTRIndex; //This figures out the index of the space before it listPTR.setNext(this.get(previousIndex)); //and subtracts it from the length, finding the index of the return listPTR.getNext(); //previous one, I then use .get to get that Node and set it } } public void reset(){ listPTR.setNext(null); } public static void main(String[] args) { LinkedList l = new LinkedList(false, 1); String x1 = "jack"; Node s1 = new Node(x1, null); l.add(s1); //AND HERE is the exception in main String x2 = "hello world"; Node s2 = new Node(x2, null); l.add(s2); String x3 = "tomato"; Node s3 = new Node(x3, null); l.add(s3); Object o; o = l.pointNext(); // Value of o is "hello world" System.out.println(o.toString()); o = l.pointNext(); // Value of o is "jack" System.out.println(o.toString()); o = l.pointNext(); // Value of o is "tomato" System.out.println(o.toString()); o = l.pointNext(); // Value of o is null //System.out.println(o.toString()); l.reset(); // o = l.pointNext(); // Value of o is "hello world" System.out.println(o.toString()); //o = l.pointPrevious(); // Value of o is null System.out.println(o.toString()); o = l.pointNext(); // Value of o is "hello world" System.out.println(o.toString()); o = l.pointNext(); // Value of o is "jack" System.out.println(o.toString()); o = l.pointPrevious(); // Value of o is "hello world" System.out.println(o.toString()); o = l.pointPrevious(); // Value of o is null //System.out.println(o.toString()); o = l.pointPrevious(); // Value of o is null //System.out.println(o.toString()); o = l.pointNext(); // Value of o is "hello world" System.out.println(o.toString()); o = l.pointNext(); // Value of o is "jack" System.out.println(o.toString()); o = l.pointNext(); // Value of o is "tomato" System.out.println(o.toString()); o = l.pointNext(); // Value of o is null //System.out.println(o.toString()); o = l.pointPrevious(); // Value of o is "tomato" System.out.println(o.toString()); o = l.pointPrevious(); // Value of o is "jack" System.out.println(o.toString()); o = l.pointPrevious(); // Value of o is "hello world" System.out.println(o.toString()); o = l.pointPrevious(); // Value of o is null //System.out.println(o.toString()); } }
and this program;
Thanks!Java Code:// // Node.java // //package csci1902.lab3; //import csci1902.lab3.*; public class Node { private Object data = null; private Node next = null; public Node(Object initData, Node initLink) { data = initData; next = initLink; } public void setData(Object newData) { data = newData; } public void setNext(Node newLink) { next = newLink; } public Object getData() { return data; } public Node getNext() { return next; } }Last edited by muffstuff; 04-10-2009 at 04:08 PM.
- 04-10-2009, 04:06 PM #2
Member
- Join Date
- Apr 2009
- Posts
- 5
- Rep Power
- 0
I forgot to mention that you only have to really look at the (main) and (add) functions in LinkedList.
I have to make a LinkedList of Nodes (Single Link) for a basic database program.
-
The line that the NPE occurs should tell you what object is null. So which one do you think it is? (hint, there's only one that it could be, and it's because you're using it without ever setting it = to anything).
- 04-10-2009, 05:37 PM #4
Member
- Join Date
- Apr 2009
- Posts
- 5
- Rep Power
- 0
i knew it had to be that, but when i tried to set header to be
private Node header = new Node();
the compiler gives me an error, saying
LinkedList.java:18: cannot find symbol
symbol : constructor Node()
location: class Node
private Node header = new Node(); //Zeroth node, contains no data
^ (this is supposed to be under new)
LinkedLists make me miss arrays so much...Last edited by muffstuff; 04-10-2009 at 05:46 PM.
- 04-10-2009, 06:02 PM #5
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
the compiler can't find "Node()" because there is no such constructor...
- 04-10-2009, 06:17 PM #6
Member
- Join Date
- Apr 2009
- Posts
- 5
- Rep Power
- 0
Ha, thanks, yeah I just realized that too, so I initialized it as Node(null, null), but of course it gave me the same error.
I think I have to just find a new way of making my length method work or my add method.
How would I check if the LinkedList is empty before it's empty?
You know?
My main issue is that I have to have something to give header to point to (or something) because having the empty list gives me all sorts of Null exceptions.
I won't be making my deadline, but I think I'm going to sleep for 4 hours or so before I have to work...
Any more help would be appreciated!
Thanks!
- 04-10-2009, 06:31 PM #7
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
the problem with your add method was already hinted at by fubarable. you seem to know the line that's causing the problem already, so if you use his info, it should click.
- 04-10-2009, 10:51 PM #8
Member
- Join Date
- Apr 2009
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
LinkedList problem
By Mika in forum New To JavaReplies: 7Last Post: 02-18-2009, 02:10 PM -
Please help me with LinkedList in Java
By lenny in forum New To JavaReplies: 2Last Post: 07-31-2007, 02:24 PM -
how to use LinkedList
By fred in forum Advanced JavaReplies: 1Last Post: 07-24-2007, 01:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks