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
  • 51,545
  • 25
  • 157
  • 246
luojiebin
  • 103
  • 2
  • 3
  • 14

3 Answers3

6

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

Harmlezz
  • 7,524
  • 23
  • 34
  • Doesn't java automated to initialized it to all 0.0? – luojiebin Mar 11 '14 at 10:25
  • 2
    Java doesn't know how big the array is. – Hayden Mar 11 '14 at 10:26
  • @luojiebin Refer to my answer, it shows the declaration (I can only assume the size is T) – nanofarad Mar 11 '14 at 10:27
  • Then how to fix it please?I am just a novice – luojiebin Mar 11 '14 at 10:27
  • @luojiebin If you have a question, please state it more precisely. What part of initializing the array do you need help with? My answer shows the line that is to be added, if you have a specific question address it there – nanofarad Mar 11 '14 at 10:29
  • I am sorry but you said you implemented that code and you need help with initializing array ? Both the answers clearly mentions that you need to initialize the array – Mr Spark Mar 11 '14 at 10:31
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
  • 36,174
  • 4
  • 79
  • 104
  • 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
  • 558
  • 2
  • 6
  • 22