繁体   English   中英

线程“ main”中的异常java.lang.NullPointerException,但在main方法中解析了int

Exception in thread “main” java.lang.NullPointerException but int is parsed in main method

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

运行代码时得到以下信息,但是我在main方法中调用了solve(4)。 因此,我不确定为什么存在null? 最初我有NQueensProblem q = new NQueensProblem(4); 但这在编译时引发了错误。 任何指导将不胜感激。

NQueensProblem.solve(NQueensProblem.java:at在NQueensProblem.can(NQueensProblem.java:32)在NQueensProblem.solve(NQueensProblem.java:at在NQueensProblem。 Java:76)

public class NQueensProblem {
public int[][] solution;
public NQueensBT(int N) {
    solution = new int[N][N];
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            solution[i][j] = 0;
        }
    }
}
public void solve(int N) {
    if (placeQueens(0, N)) {
        //print the result
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                System.out.print(" " + solution[i][j]);
            }
            System.out.println();
        }
    } else {
        System.out.println("NO SOLUTION EXISTS");
    }
}
public boolean placeQueens(int queen, int N) {
    // will place the Queens one at a time, for column wise
    if (queen == N) {
        //if we are here that means we have solved the problem
        return true;
    }
    for (int row = 0; row < N; row++) {
        // check if queen can be placed row,col
        if (canPlace(solution, row, queen)) {
            // place the queen
            solution[row][queen] = 1;
            // solve for next queen
            if (placeQueens(queen + 1, N)) {
                return true;
            }
            //if we are here that means above placement didn't work
            //BACKTRACK
            solution[row][queen] = 0;
        }
    }
    //if we are here that means we haven't found solution
    return false;
}
// check if queen can be placed at matrix[row][column]
public boolean canPlace(int[][] matrix, int row, int column) {
    // since we are filling one column at a time,
    // we will check if no queen is placed in that particular row
    for (int i = 0; i < column; i++) {
        if (matrix[row][i] == 1) {
            return false;
        }
    }
    // we are filling one column at a time,so we need to check the upper and
    // diagonal as well
    // check upper diagonal
    for (int i = row, j = column; i >= 0 && j >= 0; i--, j--) {
        if (matrix[i][j] == 1) {
            return false;
        }
    }
    // check lower diagonal
    for (int i = row, j = column; i < matrix.length && j >= 0; i++, j--) {
        if (matrix[i][j] == 1) {
            return false;
        }
    }
    // if we are here that means we are safe to place Queen at row,column
    return true;
}
public static void main(String[] args) {
    int N = 4;
    NQueensProblem q = new NQueensProblem(N);
    q.solve(N);
}

}

1 个回复

如果我了解代码以及此处的问题,请尝试以下操作。

public class NQueensProblem {
public int[][] solution;
public NQueensProblem(int N) {
    solution = new int[N][N];
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            solution[i][j] = 0;
        }
    }
}
public void solve(int N) {
    if (placeQueens(0, N)) {
        //print the result
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                System.out.print(" " + solution[i][j]);
            }
            System.out.println();
        }
    } else {
        System.out.println("NO SOLUTION EXISTS");
    }
}
public boolean placeQueens(int queen, int N) {
    // will place the Queens one at a time, for column wise
    if (queen == N) {
        //if we are here that means we have solved the problem
        return true;
    }
    for (int row = 0; row < N; row++) {
        // check if queen can be placed row,col
        if (canPlace(solution, row, queen)) {
            // place the queen
            solution[row][queen] = 1;
            // solve for next queen
            if (placeQueens(queen + 1, N)) {
                return true;
            }
            //if we are here that means above placement didn't work
            //BACKTRACK
            solution[row][queen] = 0;
        }
    }
    //if we are here that means we haven't found solution
    return false;
}
// check if queen can be placed at matrix[row][column]
public boolean canPlace(int[][] matrix, int row, int column) {
    // since we are filling one column at a time,
    // we will check if no queen is placed in that particular row
    for (int i = 0; i < column; i++) {
        if (matrix[row][i] == 1) {
            return false;
        }
    }
    // we are filling one column at a time,so we need to check the upper and
    // diagonal as well
    // check upper diagonal
    for (int i = row, j = column; i >= 0 && j >= 0; i--, j--) {
        if (matrix[i][j] == 1) {
            return false;
        }
    }
    // check lower diagonal
    for (int i = row, j = column; i < matrix.length && j >= 0; i++, j--) {
        if (matrix[i][j] == 1) {
            return false;
        }
    }
    // if we are here that means we are safe to place Queen at row,column
    return true;
}
public static void main(String[] args) {
    int N = 4;
    NQueensProblem q = new NQueensProblem(N);
    q.solve(N);
}
}
7 线程“main”中的异常java.lang.NullPointerException - HBase索引数据

我正在解析pdf并在变量中存储标题,作者等,我需要索引hbase中的值。 所以我从我在项目中创建的变量中获取hbase表的数据。 当我在hbase表中使用变量进行索引时,程序显示NullPointerException错误。 我尝试了两种不同的类型,但都没有。 和 这 ...

2016-03-08 00:34:03 1 346   java
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2022 STACKOOM.COM