我一直试图让这个Sudoku Solver工作,调试器给我这个错误。查看错误,我的Sudoku类中有一个NullPointerException
。我不明白我的方法究竟出了什么问题。
更新:
01-06 16:56:25.124: E/AndroidRuntime(12733): FATAL EXCEPTION: main
01-06 16:56:25.124: E/AndroidRuntime(12733): Process: com.nom.sudokuhint, PID: 12733
01-06 16:56:25.124: E/AndroidRuntime(12733): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.nom.sudokuhint/com.nom.sudokuhint.MainActivity}: java.lang.NullPointerException
01-06 16:56:25.124: E/AndroidRuntime(12733): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
01-06 16:56:25.124: E/AndroidRuntime(12733): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-06 16:56:25.124: E/AndroidRuntime(12733): at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-06 16:56:25.124: E/AndroidRuntime(12733): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-06 16:56:25.124: E/AndroidRuntime(12733): at android.os.Handler.dispatchMessage(Handler.java:102)
01-06 16:56:25.124: E/AndroidRuntime(12733): at android.os.Looper.loop(Looper.java:136)
01-06 16:56:25.124: E/AndroidRuntime(12733): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-06 16:56:25.124: E/AndroidRuntime(12733): at java.lang.reflect.Method.invokeNative(Native Method)
01-06 16:56:25.124: E/AndroidRuntime(12733): at java.lang.reflect.Method.invoke(Method.java:515)
01-06 16:56:25.124: E/AndroidRuntime(12733): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-06 16:56:25.124: E/AndroidRuntime(12733): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-06 16:56:25.124: E/AndroidRuntime(12733): at dalvik.system.NativeStart.main(Native Method)
01-06 16:56:25.124: E/AndroidRuntime(12733): Caused by: java.lang.NullPointerException
01-06 16:56:25.124: E/AndroidRuntime(12733): at com.nom.sudokuhint.Sudoku.stringify(Sudoku.java:44)
01-06 16:56:25.124: E/AndroidRuntime(12733): at com.nom.sudokuhint.Sudoku.<init>(Sudoku.java:21)
01-06 16:56:25.124: E/AndroidRuntime(12733): at com.nom.sudokuhint.MainActivity.<init>(MainActivity.java:17)
01-06 16:56:25.124: E/AndroidRuntime(12733): at java.lang.Class.newInstanceImpl(Native Method)
01-06 16:56:25.124: E/AndroidRuntime(12733): at java.lang.Class.newInstance(Class.java:1208)
01-06 16:56:25.124: E/AndroidRuntime(12733): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
01-06 16:56:25.124: E/AndroidRuntime(12733): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
01-06 16:56:25.124: E/AndroidRuntime(12733): ... 11 more
Sudoku.java
package com.nom.sudokuhint;
public class Sudoku {
int matrix[][];
String solveable;
String start[][];
String end[][];
String attempt[][];
String editList[];
public Sudoku() {
matrix = new int[][] {
{ 0, 0, 0, 0, 0, 0, 0, 6, 0 }, // evil level just to start
{ 8, 0, 2, 0, 0, 3, 0, 0, 0 }, { 6, 0, 0, 8, 9, 0, 0, 3, 0 },
{ 0, 1, 0, 9, 0, 0, 7, 0, 4 }, { 0, 0, 0, 0, 8, 0, 0, 0, 0 },
{ 7, 0, 5, 0, 0, 2, 0, 9, 0 }, { 0, 2, 0, 0, 3, 8, 0, 0, 5 },
{ 0, 0, 0, 4, 0, 0, 1, 0, 2 }, { 0, 4, 0, 0, 0, 0, 0, 0, 0 } };
stringify(matrix, start);
stringify(matrix, attempt);
solveMatrix();
stringify(attempt, editList);
}
static void stringify(int[][] cells, String[][] data) {
data = new String[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (cells[i][j] == 0) {
data[i][j] = " ";
} else {
data[i][j] = String.valueOf(cells[i][j]);
}
}
}
}
public void stringify(String[][] Old, String[] data) {
data = new String[81];
int c = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
data[c] = Old[i][j];
c++;
}
}
}
public boolean solveMatrix() {
if (solve(0, 0, matrix)) {
stringify(matrix, end);
return true;
} else {
return false;
}
}
public String checkMatrix() {
int count = 0;
String temp = "";
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (attempt[i][j] != end[i][j] && attempt[i][j] != "Hint?"
&& attempt[i][j] != " ") {
count++;
}
}
}
if (count != 0) {
temp = "So far you have " + String.valueOf(count) + " mistakes";
} else {
temp = "Nice, no mistakes so far";
}
return temp;
}
public String hintMatrix(int x) {
int i = (x / 9);
int j = (x % 9);
attempt[i][j] = end[i][j];
return attempt[i][j];
}
public void hintMatrix() {
}
public void setMatrix(int i, int j, String data) {
attempt[i][j] = data;
}
static boolean solve(int i, int j, int[][] cells) {
if (i == 9) {
i = 0;
j++;
if (j == 9) {
return true;
}
}
if (cells[i][j] != 0) {
return solve(i + 1, j, cells);
}
for (int val = 0; val < 9; val++) {
if (legal(i, j, val, cells)) {
cells[i][j] = (val + 1);
if (solve(i + 1, j, cells)) {
return true;
}
}
}
cells[i][j] = 0;
return false;
}
static boolean legal(int i, int j, int val, int[][] cells) {
for (int k = 0; k < 9; k++) {
if ((val + 1) == cells[k][j]) {
return false;
}
}
for (int k = 0; k < 9; k++) {
if ((val + 1) == cells[i][k]) {
return false;
}
}
int boxRowOffset = (i / 3) * 3;
int boxColOffset = (j / 3) * 3;
for (int k = 0; k < 3; k++) {
for (int m = 0; m < 3; m++) {
if ((val + 1) == cells[boxRowOffset + k][boxColOffset + m]) {
return false;
}
}
}
return true;
}
}
答案 0 :(得分:0)
您需要在夸大布局后检索您的UI元素,否则findViewById
将返回null
,因此行NPE
上的table.addView(currentRow);
会返回。
将其更改为:
TableLayout table;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //inflate the layout
table = (TableLayout) findViewById(R.id.sudokuPuzzle); //it's ok now
答案 1 :(得分:0)
这是你的问题:
TableLayout table = (TableLayout) findViewById(R.id.sudokuPuzzle);
在您致电table = (TableLayout) findViewById(R.id.sudokuPuzzle);
后,将onCreate()
移至setContentView()