简体   繁体   中英

NullPointer Exception in Java

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!

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM