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

标签 java nullpointerexception

我想在二维数组中加载两个机器人的初始位置,它们由字母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/

相关文章:

java - 如何在Project POM中继承External Maven POM

java - 编辑: How do I set variables in a Project pom from a Maven Plugin?

java - 用户选择时使用 switch case 和 while 循环进行无限循环

java - 调用无效方法

java - Android:使用属性时出现空指针异常

java - 无法访问 struts 操作类中的 JSON 请求对象

android - Cache.Entry 没有获取 json 数据

java - 如何删除整个列表但只留下最后一个索引

java - 在 IBM WebSphere 上运行的意外异常 java.math.BigDecimal

设置 CursorAdapter 时 Android 应用强制关闭