我在Android Studio中做了一个项目,并且当我绝对不应该得到一个空指针异常。这是问题代码:
public class PlayActivity extends AppCompatActivity{
boolean[][] cellClicked = new boolean[3][3];
boolean player1 = false;
boolean player2 = true;
boolean currentPlayer;
ImageView[][] cells = new ImageView[3][3];
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) throws NullPointerException{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
currentPlayer = player1;
cells[0][0] = (ImageView) findViewById(R.id.cell_0);
cells[0][1] = (ImageView) findViewById(R.id.cell_1);
cells[0][2] = (ImageView) findViewById(R.id.cell_2);
cells[1][0] = (ImageView) findViewById(R.id.cell_3);
cells[1][1] = (ImageView) findViewById(R.id.cell_4);
cells[1][2] = (ImageView) findViewById(R.id.cell_5);
cells[2][0] = (ImageView) findViewById(R.id.cell_6);
cells[2][1] = (ImageView) findViewById(R.id.cell_7);
cells[2][2] = (ImageView) findViewById(R.id.cell_8);
try {
for(int i = 0; i < 3; i++) {
for(int j = 0; i < 3; j++) {
setListeners(i, j);
}
}
} catch(NullPointerException e){
throw new NullPointerException("cant access element");
}
}
private void setListeners(int i, int j){
final int row = i;
final int col = j;
cells[row][col].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!cellClicked[row][col]) {
makeMove(cells[row][col], row, col);
}
}
});
}
当我运行应用程序时,只要我导航到PlayActivity,应用程序就会崩溃。当我查看日志错误时,它会显示:
FATAL EXCEPTION: main
Process: net.drmcb.tictactoe, PID: 29345
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.drmcb.tictactoe/net.drmcb.tictactoe.PlayActivity}: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
at net.drmcb.tictactoe.PlayActivity.setListeners(PlayActivity.java:55)
at net.drmcb.tictactoe.PlayActivity.onCreate(PlayActivity.java:44)
at android.app.Activity.performCreate(Activity.java:6942)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
错误所指的行是44:setListeners(i, j);
和55:
cells[row][col].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!cellClicked[row][col]) {
makeMove(cells[row][col], row, col);
}
}
});
我在makeMoves()
中注释掉了所有代码,因此不是问题。
在我的for循环中包含对setListeners()的调用,i < 3
部分突出显示,当我将鼠标悬停在它上面时,它告诉我&#34;条件&#39; i&lt; 3&#39;永远是真的&#34; ...
在过去几个小时尝试调试时,我有以下问题:
为什么for循环超过它i < 3
条件?
为什么对cells.length
或cells[0].length
的引用会破坏代码?
答案 0 :(得分:3)
它不是Null Exception
,而是ArrayIndexOutOfBoundsException
因为你的内心for loop
for(int j = 0; i < 3; j++) {
请注意 i&lt; 3 应为 j&lt; 3
for(int j = 0; j < 3; j++) {
答案 1 :(得分:1)
基本上你的 j 能够增加到3,因为你的i&lt; 3 in
for(int j = 0; i < 3; j++) {
永远是真的因为我是0。
正如Yazan所说,只要纠正你的i&lt; 3至j&lt; 3,没关系。
这也告诉我们正确命名我们的变量以避免这类问题。