2

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!!

2
  • I'd also like to give you some advice - Use List instead of arrays; Follow java naming conventions; embrace the for each loop construct. Commented Sep 14, 2010 at 16:49
  • Followup to Robert's comment, here is a link to the standard Java conventions, which will help your Java code look more like Java. :) oracle.com/technetwork/java/codeconv-138413.html Commented Sep 14, 2010 at 17:04

4 Answers 4

10

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];
Sign up to request clarification or add additional context in comments.

Comments

1

this variable:

double [] ADVISOR_SCORE;

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

Comments

1

ADVISOR_SCORE has not been initialised

Comments

1

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.