0

I wrote this code to merge two Lists (NodoLista is a single element of the list), but I get the NullPointerExcpetion at line 34 of Main. Can you help me?

public static void main(String[] args) {
    NodoLista a= new NodoLista(4,new NodoLista(5,new NodoLista(6,null)));
    NodoLista b= new NodoLista(7,new NodoLista(8, new NodoLista(9,null)));
    a=fusioneOrdinata(a,b);
    while(a!=null){
        System.out.println(a.info);
        a=a.next;
    }
}

static NodoLista fusioneOrdinata(NodoLista x, NodoLista y){
NodoLista pp=null;
NodoLista a=x;
NodoLista b=y;
while(b!=null){
    while(a!=null || b.info>a.info){
        pp=a;
        a=a.next;
    }
    if(b.info<=a.info){
        pp.next=b;
        b.next=a;
    }
    b=b.next;
}
return a;}

public class NodoLista {
int info; //OBJECT= Classe da cui derivano tutte le altre, contenitore di oggetti (non di tipi primitivi)
NodoLista next; // RICORSIVA

NodoLista(int info, NodoLista next){
    this.info=info;
    this.next=next;
}}
Cosimo Sguanci
  • 1,251
  • 2
  • 14
  • 25
  • I know that but I can't find the reasons here because I can't find any null reference... – Cosimo Sguanci May 15 '16 at 16:07
  • Well, what line is 34 ? Did you debug ? What did you do ? Where/What is the problem ? This is not a code writing service. – UDKOX May 15 '16 at 16:10
  • I have already written the code. It's up there. And all you ask is in my first question. Line 34 is: while(a!=null || b.info>a.info) I can't find the null reference. Can you help me? I don't think I have asked the code at all. – Cosimo Sguanci May 15 '16 at 16:12
  • You gave 0 information of the problem, copy/pasted the code and expected us to solve it for you. This is not how StackOverflow works, you need to be precise. Now that I know what line it is, I noticed that you change the value of `b` but don't check if it's `null`. Try writing 1 `while` statement instead of 2, like this: `while(b!=null && a!=null || b.info > a.info)`. – UDKOX May 15 '16 at 16:18

0 Answers0