1

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);
    }
}
Ali
  • 56,466
  • 29
  • 168
  • 265
luojiebin
  • 103
  • 2
  • 3
  • 14

3 Answers3

6

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

Harmlezz
  • 7,972
  • 27
  • 35
4

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;
        }

    }
}
nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • 2 errors found: File: /home/luojiebin/introcs/hello/PercolationStats.java [line: 9] Error: cannot find symbol symbol: variable i location: class PercolationStats File: /home/luojiebin/introcs/hello/PercolationStats.java [line: 9] Error: incompatible types required: double found: double[] – luojiebin Mar 11 '14 at 10:33
  • I add this line sample_threshold = new double[3*T];and the problem solved.Thank you. – luojiebin Mar 11 '14 at 10:47
0

here at 3rd line where you've written double[] sample_threshold; instead just write double[] sample_threshold= new double[5000]; meaning just initalize the array. Then when you use it in for loop java will only consider the arrays for the times your for loop loops.

Parth
  • 568
  • 2
  • 6
  • 23