tags:

views:

168

answers:

4

So I would like to start out by telling you that I am learning Java on my own and you guys are the nearest thing I have to teachers. So thank you so much for putting up with my simple and obvious question. I am just trying to learn. Once again I am getting an error that for the life of me I cannot figure out.

Here is the error:

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)

Here is my code for the ratings class:

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;
}

Here is my code for the other class:

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();
        }

    }

Again, I cannot thank you guys enough at StackOverflow. Your help has been invaluable!!

+1  A: 

this variable:

double [] ADVISOR_SCORE;

hasn't been initialized... and therefore it's null.

chakrit
+10  A: 

You have not initialized the ADVISOR_SCORE and All_users arrays, but you do try to assign values and use them. When you declare

double[] ADVISOR_SCORE; // this is null until assigned

At some point, it needs to be assigned

ADVISOR_SCORE = new double[size];
Jeff Storey
+1  A: 

ADVISOR_SCORE has not been initialised

Robert
+1  A: 

Jeff Storey provided the best explanation, here are two semi-related tips I had to learn when learning Java:

1) Once you initialize that array

ADVISOR_SCORE = new double[size];

You cannot modify the array's length unless you re-initialize it. Students often will try to add another value onto the end of an array or otherwise "grow" it somehow. If this is something you need, checkout Vector and ArrayList.

2) Java coding conventions are to capitalize class names...

public class Rating {

...but leave the first letter of method names in lower case.

public double [] getFirstScore() {

It'll help readability when others start working on your code.

Happy coding!

gmoore