Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: NullPointerException

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Thanked 0 Times in 0 Posts

    Default NullPointerException

    Hello everybody,
    I have an application where i use TDA as Map and so I have a Map of Maps where I store keys and values as String and Double as you see from the code bellow. So a Map that stores other Maps where I store information of movies and ratings. Then in the Map that contains the other Maps i store as value the name of the person that have given this oppinion about movies . I have implemented a method that calculates the euclidean distance between two users and it functions but when I try to use the same method inside another method that calculates the same thin but between a user and all the others it gives me an NullPointerExeption despite it compiles well.
    Can somebody give me an advice?
    This is the code:
    import java.util.*;

    public class MRdati {

    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 , Double> ratesT = new HashMap <String , Double >();
    ratesT.put("Lady in the water", 3.0 );
    ratesT.put("Snakes on a plane", 4.5 );
    ratesT.put("Just my luck ", 3.0 );
    ratesT.put("Superman returns", 4.0);
    ratesT.put("The night Listener", 3.0);
    ratesT.put("You me and Dupree", 1.0);

    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);
    critics.put("Toby", ratesT);
    //System.out.println(critics.get("Lisa Rose"));

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

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

    // these are the methods

    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)

    return Math.sqrt(d);

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

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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,693 Times in 2,643 Posts

    Default Re: NullPointerException

    it gives me an NullPointerExeption
    Please post the full text of the error message. It gives the location of the error.
    A NPE is because the code tries to use a variable with a null value to reference an object. You need to find what variable has the null value and then backtrack in the code to see why that variable does not have a valid no-null value.

    Also please edit the post and wrap your code in code tags.
    Last edited by Norm; June 13th, 2012 at 03:18 PM. Reason: spelling
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. NullPointerException
    By deathmatex in forum Exceptions
    Replies: 4
    Last Post: March 27th, 2012, 03:54 AM
  2. [SOLVED] NullPointerException
    By macko in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 21st, 2011, 11:35 AM
  3. [SOLVED] Nullpointerexception
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 14th, 2010, 10:33 PM
  4. [SOLVED] NullPointerException
    By javapenguin in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 1st, 2010, 12:10 AM
  5. NullPointerException
    By bbr201 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2010, 07:06 PM

Tags for this Thread