Q:

java.lang.NullPointerException in two related java class

I implemented two java classes to solve the percolation problem but it throws the following exception
java.lang.NullPointerException
    at PercolationStats.<init>(PercolationStats.java:24)
    at PercolationStats.main(PercolationStats.java:54)

Program:
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);
    }
}

A:

You never initialized double[] sample_threshold;. Hence it is null.

A:

Java will indeed fill a double[] with 0.0 once it is initialized to a known size. You must initialize the array first:
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;
        }

    }
}

