PDA

View Full Version : [Java] Null Pointer Exception


pollo86
07-10-2008, 18:03
Ciao, mi sapete dire cosa c'č che non va in questo codice ?



import java.util.*;

public class Matrice {

/**
* @param args
*/
public static void main(String[] args) {

int[][] M = {
{ 0, 1, 1 },
{ 1, 0, 0 },
{ 1, 0, 0 }
};


int n = M.length; // n.ro righe
AdjacencyList[] aList = new AdjacencyList[n];
for(int i = 0; i < n ; i++)
for(int j = 0; j < n; j++)
if(M[i][j] == 1)
aList[i].add(new Integer(i));




}

class AdjacencyList {
private LinkedList<Integer> list = new LinkedList<Integer>();
public void add(Integer item) {
list.add(item);
}

}
}





All'esecuzione mi da errore java.lang.NullPointerException

vegeta83ssj
07-10-2008, 18:37
Prova cosė:


package javaapplication3;

import java.util.*;

public class Main {

/**
* @param args
*/
public static void main(String[] args) {

int[][] M = {
{ 0, 1, 1 },
{ 1, 0, 0 },
{ 1, 0, 0 }
};


int n = M.length; // n.ro righe
AdjacencyList[] aList = new AdjacencyList[n];

for(int i = 0; i < n ; i++){
aList[i] = new AdjacencyList();
for(int j = 0; j < n; j++)
if(M[i][j] == 1)
aList[i].add(new Integer(i));
}




}

static class AdjacencyList {
private LinkedList<Integer> list = new LinkedList<Integer>();



public void add(Integer item) {
list.add(item);
}

}
}

pollo86
07-10-2008, 20:05
ottimo, grazie mille :D