0

This is my code:

public class Main {

private int[][] AR;
private int[][] AG;
private int[][] Sink;
private int[][] RR;
private int[][] RP;

private int n;

//int n = 3;

public void initialization(int n) {
    initAR(n);
    initAG(n);
    initSink(n);
    initRR(n);
    initRP(n);
}

public void initAR(int n) {
    this.AR = new int[n][n];
}

public void initAG(int n) {
    this.AG = new int[n][n];
}

public void initSink(int n) {
    this.Sink = new int[n][n];
}

public void initRR(int n) {
    this.RR = new int[n][n];
}

public void initRP(int n) {
    this.RP = new int[n][n];
}

public int multi_unit_fun(int process, int resource) {

    boolean IsAllocated = false;
    for (int i = 0;i < n;i++) {
        if (AG[resource][i] == 1) {
            IsAllocated = true;
            break;
        }
    }


    if (!IsAllocated) {//resource is available
        AG[resource][process] = 1;
        Sink[resource][process] = 1;
        RP[resource][process] = 1;
        return 0;
    }
    else {//resource is not available.
        if (Sink[resource][process] == 1) {
            return 1;
        }
        else {
            blockProcess(process, resource, n);
            return 2;
        }
    }
}

public int[][] getSink() {
    return this.Sink;
}

public  void blockProcess(int process, int resource, int n) {
    AR[process][resource] = 1;
    int sum = 0;
    for (int i = 0;i < n;i++) {
        sum += AG[i][process];
    }
    if (sum > 0) {
        for (int k = 0; k < n;k++) {
            if (Sink[k][process] == 1) {
                for (int j = 0;j < n;j++) {
                    Sink[k][j] = Sink[resource][j];
                    RR[k][j] = RR[k][j] | RR[resource][j];
                    RP[k][j] = RP[k][j] | RP[resource][j];
                }
            }
        }
    }
}

}

When the code is executed at "AG[resource][process] = 1" there is an exception said "NullPointerException". It seems like there is something wrong when I initialize the matrix AR. But I cannot figure out what is the problem.

Suraj Rao
  • 28,186
  • 10
  • 88
  • 94

0 Answers0