I need to make a singly-linked list for my array of integers, but, I don't know what is currently wrong with my code right now.
This is the code to create the nodes. (the data)
package sllset;
class SLLNode {
int value;
SLLNode next;
public SLLNode(int i, SLLNode n){
value = i;
next = n
}
}
My other class which has my methods and constructors look like this.
package sllset;
public class SLLSet {
private int setSize;
private SLLNode head;
public SLLSet(){
head = null;
setSize = 0;
}
public SLLSet(int[] sortedArray){ //second constructor
setSize = sortedArray.length;
int i;
head=null;
for(i=0;i<setSize;i++){
head.next = head;
head = new SLLNode(sortedArray[i],head.next);
}
}
public String toString(){
SLLNode p;
String result = new String();
for(p=head ; p!=null ; p=p.next)
result += p.value;
return result;
}
public static void main(String[] args) {
int[] A = {2,3,6,8,9};
SLLSet a = new SLLSet(A);
System.out.println(a.toString());
}
}
My problem is that my second constructor doesn't work and I dont really have a clue as to why. I have been following a guide on how to make most of these functions so my knowledge of the code I guess isn't great enough to decipher the problem.
EDIT: So someone told me to specify the problem which I get a NULLPointerException at line 19; where I code head.next = head; . However, when I remove that part to test, line 20 gets the error message