views:

91

answers:

2

I am attempting to calculate Z_Scores in Java. However, I am new to programming and Java and cannot seem to figure out what is wrong. I keep getting a null pointer exception. Can anyone enlighten me as to what I am doing wrong. I am getting the exception at line 80 in bold. For whatever reason it doesn't like the object I am using. Here is my source code:

    public class Z_score {
    double Mean []=new double[10];
    double SD []=new double[10];
    Z_Expert [] Z_All;

    public Z_Expert[] getZ_All() {
        return Z_All;
    }

    public void setZ_All(Z_Expert[] z_All) {
        Z_All = z_All;
    }

    public double[] getMean() {
        return Mean;
    }

    public double[] getSD() {
        return SD;
    }

    public void setSD(double[] sD) {
        SD = sD;
    }

    public void setMean(double[] mean) {
        Mean = mean;
    }


    public void Z_Calc(Expert_Score...args){
        Z_score r = new Z_score();
        Expert_Score[] All_users=args;
        double sum = 0;
        double v = 0;
        double t []= new double[10];
        double h []= new double[10];
        double a []= new double[10];
        double [] w=new double[10];
        double [] s=new double[10];
        Z_Expert[] All_user = new Z_Expert[All_users.length];
        for(int j=0;j<10;j++){
            for(int i=0;i<All_users.length;i++){
                Expert_Score x=All_users[i];
                double [] y=x.getExpert_Scores();
                sum=sum+y[j];   
            }
            t[j]=(sum/All_users.length);
            sum=sum-t[j]*All_users.length;
        }   
        r.setMean(t);
        for (int k=0;k<10 ;k++){
            for(int l=0;l<All_users.length;l++){
                Expert_Score z=All_users[l];
                double [] q=z.getExpert_Scores();
                v=v+(q[k]-t[k])*(q[k]-t[k]);
            }
            a[k]=(v/All_users.length);
            v=v-a[k]*All_users.length;
        }
        r.setSD(a);
        s=r.getMean();
        w=r.getSD();
        for (int m=0;m<10;m++){
            for(int n=0;n<All_users.length;n++){
                Expert_Score z=All_users[n];
                int a2=z.getID();
                double [] q=z.getExpert_Scores();
                h[m]=(q[m]-s[m])/w[m];
                Z_Expert a1 = new Z_Expert(a2, h);
                All_user[n]=a1;
            }
            r.setZ_All(All_user);
        }

    }   

    public void print(Z_score x) {
        Z_Expert [] g=x.getZ_All();
        **for(int p=0;p<g.length;p++){**
        Z_Expert e=g[p];
        int sum=1;
        double [] f=e.getExpert_Scores();
        System.out.println("ID: "+e.getID());
            for(int o = 0;o<10;o++){
                System.out.print("Domain "+sum+": "+f[o]+ " ");
                sum++;
            }
        System.out.println();
        System.out.println();

        }
    }


public static void main(String[] args){ 
        double p1[] = {1,2,5,7,5,6,6,8,9,10};
        double p2[] = {4,3,4,3,4,1,2,3,1,5};
        double p3[] = {10,2,6,4,5,6,7,8,9,10};
        Expert_Score x = new Expert_Score(1, p1);
        Expert_Score y = new Expert_Score(2, p2);
        Expert_Score z = new Expert_Score(3, p3);
        Z_score scrCalc = new Z_score();
        scrCalc.Z_Calc(x,y,z);
        scrCalc.print(scrCalc);
}
}

Thanks in advance!

+3  A: 
Z_Expert [] g=x.getZ_All();                // returns null and assigns null to g
**for(int p=0;p<g.length;p++){**           // references g.length, but g is null
crowne
To extend this excellent answer, you are missing a constructor for the class Z_score which means that Z_score.Z_All will be null until you call Z_score.setZ_All(). (or add an equivalent constructor)
RD
+2  A: 

Ok, you've got some weird things going on here.

Ultimately you need to call setZ_All on any instance of Z_score before you try to print it. This will initialize the Z_All variable such that it won't be null anymore.

Looks to me like the root of the problem is in the Z_Calc(Expert_Score...args) method. That method is an instance method of Z_score, but you're instantiating a new Z_score on the first line of that method (32):

Z_score r = new Z_score();

You then use this r variable throughout the method, but because r is a separate instance of the Z_score class nothing you do to it really matters within the context of your main method. So even though setZ_All gets called on the r instance, it doesn't actually affect the instance created on line 103 in your main method.

Long story short, try replacing all references to 'r' with 'this' in your Z_Calc method and see if that gets you any further. I have a feeling there are some more errors, but that should help with this one.

Mike Deck
I did this and it made things a lot better. However, as you suspected I still have problems. For whatever reason, it only calculates for the last instance in my Z_Expert []
Spencer
@Spencer, first clean your code up a bit, get rid of the underscores, format class names like ClassName, variables like variableName, and use full words or even phrases for all your variable names instead of just letters. Then add some printlns at key points in the code to help you see what's happening. If you still have problems try to isolate the issue to one method and then ask another question with only the well-formatted code for that method and a good description of exactly what went wrong.
Mike Deck
@Mike I messed around with it and finally got it working! Thanks so much Mike!
Spencer