nullpointerexception - Multiple Linked Lists in Java -
i trying create multiple linked list in java, @ 1 of lines getting nullpointerexception , unable find out reason why such error arising.
import java.util.*; class node{ int data; node link; public node(){ data = 0; link = null; } } public class ll{ static node add(node head[], int x){ node temp = new node(); system.out.println("enter value"); temp.data = new scanner(system.in).nextint(); temp.link = head[x].link; head[x].link = temp; return head[x]; } public static void main(string []args){ int m =0; int x = 0; int flag = 0; system.out.println("enter size of index"); m = new scanner(system.in).nextint(); node []head = new node[m]; while(x<head.length){ head[x].data = 0; //error arises here head[x].link = null; x++; } system.out.println(head[0].data); //error arises here. } }
thats because head[x]/head[0] contains null reference.
Comments
Post a Comment