两个相关java类中的java.lang.NullPointerException

时间:2014-03-11 10:21:31

标签: java nullpointerexception

我实现了两个java类来解决渗透问题,但它抛出了以下异常

java.lang.NullPointerException
    at PercolationStats.<init>(PercolationStats.java:24)
    at PercolationStats.main(PercolationStats.java:54)

程序:

public class PercolationStats {
    int t=0;
    double[] sample_threshold;
    Percolation B;
    int N1;
    public PercolationStats(int N, int T) {
        t=T;
        N1 = N;
        int number_of_open=0;
        for(int i=0;i<T;i++) {
            B=new Percolation(N1);
            while(!B.percolates()) {
                double r1 = Math.random();
                int open_i = (int)(r1*N1);
                double r2 = Math.random();
                int open_j = (int)(r2*N1);
                B.open(open_i,open_j);
            }
            for(int k=0;k<N1;k++) {
            for(int j=0;j<N1;j++) {
                if(B.isOpen(k, j))
                    number_of_open++;
            }
            sample_threshold[i] = (number_of_open*1.0)/N1;
            }

        }
    }
    public double mean() {
        double sum = 0.0;
        for(int i=0;i<N1;i++) {
            sum += sample_threshold[i];
        }
        return sum/t;
    }
    public double stddev() {
        double sum = 0.0;
        double u = mean();
        for(int i=0;i<N1;i++) {
            sum += (sample_threshold[i]-u)*(sample_threshold[i]-u);
        }
        return sum/(t-1);
    }
    public double confidenceLo() {
        return mean()-((1.96*Math.sqrt(stddev()))/(Math.sqrt(t)));
    }
    public double confidenceHi() {
        return mean()+((1.96*Math.sqrt(stddev()))/(Math.sqrt(t)));
    }

    public static void main(String[] args) {
        int N = Integer.parseInt(args[0]);
        int T = Integer.parseInt(args[1]);
        PercolationStats C=new PercolationStats(N,T);
        double mean=C.mean();
        double stddev = C.stddev();
        double confidenceLo = C.confidenceLo();
        double confidenceHi = C.confidenceHi();
        System.out.println("mean                       = "+mean);
        System.out.println("stddev                     = "+stddev);
        System.out.println("95% confidence interval    = "+confidenceLo+", "+confidenceHi);
    }
}

3 个答案:

答案 0 :(得分:6)

您从未初始化double[] sample_threshold;。因此它是空的。

答案 1 :(得分:4)

一旦初始化为已知大小,Java确实会将double[]填入0.0 。您必须首先初始化数组:

public PercolationStats(int N, int T) {
    t=T;
    N1 = N;
    sample_threshold[i] = new double[T]; // add this line
    int number_of_open=0;
    for(int i=0;i<T;i++) {
        B=new Percolation(N1);
        while(!B.percolates()) {
            double r1 = Math.random();
            int open_i = (int)(r1*N1);
            double r2 = Math.random();
            int open_j = (int)(r2*N1);
            B.open(open_i,open_j);
        }
        for(int k=0;k<N1;k++) {
        for(int j=0;j<N1;j++) {
            if(B.isOpen(k, j))
                number_of_open++;
        }
        sample_threshold[i] = (number_of_open*1.0)/N1;
        }

    }
}

答案 2 :(得分:0)

这里是你写过double [] sample_threshold的第3行; 而只是写double [] sample_threshold = new double [5000]; 意思是只是初始化阵列。然后当你在for循环中使用它时,java将只考虑你的for循环循环的数组。