0

I'm a beginner at programming in Java. I'm aware of the number of topics on this particular message error but I haven't been able to fix this issue. The error is indicated to be in line 23 in the following code. I'm aware the problem seems to be linked to null references, but I'm failing to see how my method "Recommander" isn't assigning values to all the references.

public class Recommander{

// predetermined array of series 
private String[]  serie= {"Friends S1","Friends S2",
     "Friends S6","Friends S7", "Friends S8", "How I met your mother S1",
    "How I met your mother S2","How I met your mother S3 ","Castle S3", 
    "Santa Clarita Diet S1", "Modern Family S1","Modern Family S2",
    "Family Guy S6", "The Simpsons S5"}; 

// array of the purchases of one customer  
private int[] profil ; 

//number of profiles in the database
private int nbProfils = 10 ; 

// array of all the profiles from the database and the series available
private int[][] records ;

// creation of an instance of the object Recommander 
public Recommander() { 
    for (int k=0;k<=nbProfils-1; k++) {
        for (int i=0; i<=serie.length-1;i++) { 
            records[k][i]= (int) (Math.random())*2 ; 
        }
    } 
} 

// Display of the series available in store
public String affichageSerie() { 
    String Affichage = "" ; 
    for (int i = 0 ; i<=serie.length-1; i++) { 
        Affichage = Affichage + serie[i] +  "\n" ; 
    } 
    return Affichage  ;
} 

}

  • 1
    Please show the line so we're not all forced to count 23 lines down. Line 23 also appears to just have a brace on it, so doubly you need to show the line explicitly. – Carcigenicate May 04 '17 at 14:41
  • Your records variable is not initialized, thus it is a null reference, when you are trying to write to it in the for-loop, which I assume is line 23. You have to initialize it with something like `private int[][] records = new int[nbprofiles][serie.length];`. – Lysandros Nikolaou May 04 '17 at 14:44

1 Answers1

0

Initialize the array

 private int[][] records = new int[nbProfils][serie.length];
  • I'd bet this still gives an NPE... – Gyro Gearless May 04 '17 at 14:48
  • This will fix the null pointer... private int[][] records = new int[nbProfils][serie.length]; – Jayanand Raghuwanshi May 04 '17 at 14:54
  • Thank you! Can I just ask, why is it wrong to leave it "unspecified" in the initialisation of the arrays? Again, thank you so much for your super quick reply! – Disenchantment May 04 '17 at 15:13
  • In java we need to create object before using it. Arrays in java are the objects which needs to be created before you do any operations on them. new operator will create the array object dynamically and allocate memory as per the specified size.. I will suggest you to read something about Dynamic memory allocation.. array declaration and initialization – Jayanand Raghuwanshi May 04 '17 at 18:54