This question already has answers here:
What is a NullPointerException, and how do I fix it?

(12个答案)


14天前关闭。





我从LU分解方法中得到2个矩阵(上下两个矩阵),我正在尝试获取变量。
Here are my Lower matrix and Upper matrix

public void getZ() {
    for(int i = 0; i < z.length; i++) { // iteration
        if(i != nodeApplied) { // first we initialize the z with 0
            // unless we reach the node that the force was applied
            z[i] = 0;
        } else {
            z[i] = force;
        }
    }
}

public void getNewZ() {
    // lower
    int[] newZ = new int[n]; // we create a new int list to save the values of the variables
    for (int i = 0; i < n; i++) {
        int decrement = 0; // all positions in the row before the diagonal subtracted
        for (int j = 0; j < n; j++) {
            if (j == i) { // if it is a diagonal
                newZ[i] = (z[i] - decrement) / lu.lower[i][i];
            } else { // if is not in the diagonal
                decrement += lu.lower[i][j] * newZ[j];
            }
        }
    }
    this.z = newZ;
}

public void getVariables() {
    getNewZ();
    for(int i = 0; i < n; i++) {
        System.out.println(z[i]);
    }
}




Exception in thread "main" java.lang.NullPointerException
    at Matrix.getNewZ(Matrix.java:92)
    at Matrix.<init>(Matrix.java:68)
    at Matrix.main(Matrix.java:118)

最佳答案

我认为变量'lu'尚未实例化,目前指向null。

10-08 02:14