NullPointerExceptiondo

di il
1 risposte

NullPointerExceptiondo

Salve a tutti,
sto cercando di implementare un metodo che mi calcola la distanza euclidea tra i valori di una Map
.Compila bene ma mi da un NullPointerException e non capisco dove sta il problema. Qualcuno mi può dare un suggerimento?
ecco il codice:
import java.util.*;

public class MRdati {

public double D(String u1, String u2, Map <String,Map<String, Double>> critics){

Map <String, Double> u1c = critics.get(u1);
Map<String, Double> u2c = critics.get(u2);
double d=0;


for (String film: u1c.keySet()){
double v1 = u1c.get(film); // per ogni stringa film trova il suo valore
double v2 = u2c.get(film);
if(v2 == 0)
continue;
d+=(v2-v1)*(v2-v1);
}

return Math.sqrt(d);
}

public double[] ListaR( Map <String,Map<String, Double>> critics, String w1){
double []d1= new double [critics.size()-1];
int i=0;
for (String critics_i: critics.keySet()){
d1=D(w1, critics_i, critics);
i++;
}
return d1;
}

public double [][] listaRlistaR(Map <String,Map<String, Double>>critics){
int i=0;
double [][]d2 = new double[critics.size()][critics.size()-1];
for(String s : critics.keySet()){
d2 = ListaR(critics, s);
i++;
}
return d2;
}

public static void main (String[]args){


Map <String , Double> ratesLR = new HashMap<String , Double >();

ratesLR.put("Lady in the water", 2.5);
ratesLR.put("Snakes on a plane", 3.5);
ratesLR.put("Just my luck", 3.0);
ratesLR.put("Superman returns", 3.5);
ratesLR.put("The night Listener", 3.0);
ratesLR.put("You me and Dupree", 2.5);



Map <String , Double> ratesGS = new HashMap <String , Double >();

ratesGS.put("Lady in the water", 3.0);
ratesGS.put("Snakes on a plane", 3.5);
ratesGS.put("Just my luck", 1.5);
ratesGS.put("Superman returns", 5.0);
ratesGS.put("The night Listener", 3.0);
ratesGS.put("You me and Dupree", 3.5);

Map <String , Double> ratesMP = new HashMap <String , Double >();

ratesMP.put("Lady in the water", 2.5);
ratesMP.put("Snakes on a plane", 3.5);
ratesMP.put("Just my luck", 1.5);
ratesMP.put("Superman returns", 3.5);
ratesMP.put("The night Listener", 4.0);
ratesGS.put("You me and Dupree", 3.5);

Map <String , Double> ratesCP = new HashMap <String , Double >();

ratesCP.put("Lady in the water", 2.5);
ratesCP.put("Snakes on a plane",3.5 );
ratesCP.put("Just my luck ",3.0 );
ratesCP.put("Superman returns", 4.0);
ratesCP.put("The night Listener", 4.5);
ratesCP.put("You me and Dupree", 2.5);

Map <String , Double> ratesML = new HashMap <String , Double >();

ratesML.put("Lady in the water", 3.0 );
ratesML.put("Snakes on a plane", 4.0 );
ratesML.put("Just my luck ", 3.0 );
ratesML.put("Superman returns", 3.0);
ratesML.put("The night Listener", 3.0);
ratesML.put("You me and Dupree", 2.0);

Map <String , Double> ratesJM = new HashMap <String , Double >();

ratesJM.put("Lady in the water", 3.0 );
ratesJM.put("Snakes on a plane", 4.0 );
ratesJM.put("Just my luck ", 3.0 );
ratesJM.put("Superman returns", 5.0);
ratesJM.put("The night Listener", 3.0);
ratesJM.put("You me and Dupree", 3.5);


Map<String, Map<String, Double>> critics = new HashMap<String, Map<String, Double>> ();

critics.put("Lisa Rose", ratesLR);
critics.put("Gene Seymour", ratesGS);
critics.put("Michael Phillips", ratesMP);
critics.put("Claudia Puig", ratesCP);
critics.put("Mick Lasale", ratesML);
critics.put("John Matthews", ratesJM);

MRdati mrd = new MRdati();
System.out.println("dist euclid" + mrd.D("Lisa Rose", "Gene Seymour", critics));


MRdati mrd1 = new MRdati();
System.out.println("risultato "+ mrd1.ListaR(critics, "Lisa Rose"));


}
}

1 Risposte

  • Re: NullPointerExceptiondo

    Ciao!! Hai sbagliato area, dovevi mettere questo argomento nella sezione Java.
    In ogni caso potresti incollare il contenuto dell'eccezione, per capire dove si verifica il problema?
Devi accedere o registrarti per scrivere nel forum
1 risposte