所以我想首先告诉你我自己在学习Java,而你们是我对老师最近的事情。非常感谢你提出我简单明了的问题。我只是想学习。我再次得到一个错误,在我的生活中我无法弄明白。
这是错误:
Exception in thread "main" java.lang.NullPointerException
at Advisor_score.All_user.Score1(All_user.java:13)
at Advisor_score.All_user.main(All_user.java:28)
以下是我的评级类代码:
package Advisor_score;
public class Rating {
double [] Ratings;
double sum=0;
double raw_advisor;
double advisor_score;
public Rating (double [] x){
Ratings = x;
}
public double Score(){
for(int i=2;i<Ratings.length;i++){
sum+=Ratings[i];
}
raw_advisor=((sum-(3*(Ratings.length-2)))/4);
advisor_score= 2.5+(2.5*(1-Math.pow(Math.E, -.5*raw_advisor)));
return advisor_score;
}
这是我的另一个类的代码:
package Advisor_score;
public class All_user{
double [] ADVISOR_SCORE;
Rating [] All_users;
double score;
public All_user(Rating...args){
All_users=args;
}
public double [] Score1(){
for (int j = 0;j<All_users.length;j++){
score=All_users[j].Score();
ADVISOR_SCORE[j]=score;
}
return ADVISOR_SCORE;
}
public void print(){
for(int i = 0;i<ADVISOR_SCORE.length;i++){
System.out.println(ADVISOR_SCORE[i]);
}
}
public static void main(String[] args){
double p1_1[] = {101,1,5,5,5};
double p2_1[] = {101,1,1,2,3};
Rating d = new Rating(p1_1);
Rating e = new Rating(p2_1);
All_user all=new All_user(d, e);
all.Score1();
all.print();
}
}
再一次,我在StackOverflow上不能感谢你们。你的帮助非常宝贵!!
答案 0 :(得分:10)
您尚未初始化ADVISOR_SCORE和All_users数组,但您确实尝试分配值并使用它们。当你宣布
时double[] ADVISOR_SCORE; // this is null until assigned
在某些时候,需要指定
ADVISOR_SCORE = new double[size];
答案 1 :(得分:1)
这个变量:
double [] ADVISOR_SCORE;
尚未初始化...因此它是null
。
答案 2 :(得分:1)
ADVISOR_SCORE尚未初始化
答案 3 :(得分:1)
Jeff Storey提供了最好的解释,这是我在学习Java时必须学习的两个半相关技巧:
1)初始化该数组
ADVISOR_SCORE = new double[size];
除非重新初始化,否则无法修改数组的长度。学生经常会尝试将另一个值添加到数组的末尾,否则会以某种方式“增长”它。如果这是您需要的,请结帐Vector和ArrayList。
2)Java编码约定是大写类名...
public class Rating {
...但保留方法名称的第一个字母为小写。
public double [] getFirstScore() {
当其他人开始处理您的代码时,这将有助于提高可读性。
快乐的编码!