gpt4 book ai didi

java - 为什么这段代码会生成 NullPointerException?

转载 作者:行者123 更新时间:2023-12-01 07:40:15 25 4
gpt4 key购买 nike

我想在二维数组中加载两个机器人的初始位置,它们由字母N,S,O,E表示。

以下代码不起作用。为什么?

static Point[] robotInitialPositions(char [][]inputMatrix){

Point [] helperArray = new Point[2];

int aux=0;

for (int i=0; i<(inputMatrix[0].length-1); i++)
for (int j=0; j<(inputMatrix[0].length-1); j++)
{
if((inputMatrix[i][j]=='N')||(inputMatrix[i][j]=='S')||(inputMatrix[i][j]=='O')||(inputMatrix[i][j]=='E'))
{
helperArray[aux++]= new Point(i,j);
}

}

System.out.println("helper array 1: i,j " + helperArray[1].i + ", " + helperArray[1].j);
//NullPointerException here
return helperArray;

}

完整代码:

package bfs_robots;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

class Point {

int i;
int j;

Point(int i, int j){
this.i=i;
this.j=j;
}

}

public class Main {




static char turnCounter (char orientation){

if(orientation=='N')
return 'O';
if(orientation=='O')
return 'S';
if (orientation=='S')
return 'E';
else
return 'N';

}

static char turnClock(char orientation){

if(orientation=='N')
return 'E';
if(orientation=='E')
return 'S';
if (orientation=='S')
return 'O';
else
return 'N';

}

static Point[] robotInitialPositions(char [][]inputMatrix){

Point [] helperArray = new Point[2];

int aux=0;

for (int i=0; i<(inputMatrix[0].length-1); i++)
for (int j=0; j<(inputMatrix[0].length-1); j++)
{
if((inputMatrix[i][j]=='N')||(inputMatrix[i][j]=='S')||(inputMatrix[i][j]=='O')||(inputMatrix[i][j]=='E'))
{
helperArray[aux++]= new Point(i,j);
}

}

System.out.println("helper array 1: i,j " + helperArray[1].i + ", " + helperArray[1].j);

return helperArray;

}



static void bfs_find_solution (char[][] inputMatrix){


int countOfMovements=0;
// each turn and displacement adds one

// when moved N,S,D and O must be replaced with .
// * indicates wall, invalid movement

Point robotInitial[] = robotInitialPositions(inputMatrix);





}









public static void main(String[] args) throws IOException {


BufferedReader br = new BufferedReader(new FileReader(new File("input.txt")));

char [][] inputMatrix;

String line;
char [] lineAsCharArray;
int matrixSize;

while(true){

line = br.readLine();
matrixSize=Integer.parseInt(line);

inputMatrix = new char [matrixSize][matrixSize];

if (matrixSize==0){ // end outer looping
break;
}

else { //begin inner looping

for (int i=0; i<matrixSize; i++){

line = br.readLine();
inputMatrix[i] =line.toCharArray();

}

bfs_find_solution(inputMatrix);
}


}

}

}

input.txt(0表示文件结束)

5
D....
N...S
.....
*...*
....D
5
.....
S..S.
.....
.....
D..D.
3
SN.
***
.DD
0

最佳答案

     for (int i=0; i<(inputMatrix[0].length-1); i++)         
for (int j=0; j<(inputMatrix[0].length-1); j++)

似乎是错误的。第一行应该是 inputMatrix.length-1

我认为“<”应该是“<=”。或者保留“<”并且没有“length-1”,而只是“length”

关于java - 为什么这段代码会生成 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5851208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com