2017-05-04 100 views
0

我是Java编程的初学者。 我知道这个特定邮件错误的主题数量,但我无法解决此问题。在下面的代码中,错误被指示在第23行。 我知道这个问题似乎与空引用有关,但我无法看到我的方法“Recommander”是不是将值分配给所有引用。如何解决线程“main”中的异常java.lang.NullPointerException?

公共类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

请出示线,所以我们是不是所有的强制倒计时23线。第23行似乎只是有一个大括号,所以加倍你需要明确显示该行。 – Carcigenicate

+0

您的记录变量未初始化,因此它是空引用,当您尝试在for循环中写入它时,我认为它是第23行。您必须使用类似'private int [] [ ] records = new int [nbprofiles] [serie.length];'。 –

回答

0

初始化数组

private int[][] records = new int[nbProfils][serie.length]; 
+0

我敢打赌,这仍然给NPE ... –

+0

这将修复空指针...私人诠释[] [] []记录=新诠释[nbProfils] [serie.length]; –

+0

谢谢!我可以问一下,为什么在数组初始化时将其“未指定”置为错误?再一次,非常感谢你的超级快速回复! –

相关问题