2D数组空指针异常

时间:2017-03-08 04:29:11

标签: java arrays

好的,首先,我可以保证已经提出这个问题,但我还没有找到我的具体问题,而且我没有让代表对现有帖子发表评论。基本上我已经将2D数组实例化为类对象,因为我需要以多种方法访问数组,并且更改必须保持永久性。但是,我不知道创建矩阵所需的实际所需大小,并且当我尝试稍后更改大小时,我会得到空指针异常。

基本上:

public static void sort(ArrayList<Character> line, int row)
{
    System.out.println("line size " + (line.size()-1));
    System.out.println("maze row check " + (maze[row] == null));
    maze[row] = new int[line.size()-1];
    for (int x = 0; x <line.size(); x++)
    {
        maze[row][x] = line.get(x);
        System.out.println("final sopt " + maze[row][x]);
    }
}

有没有办法成功地做到这一点?或者我忽略了一些非常简单的事情? 感谢您抽出宝贵时间阅读/回答此问题, 标记

在这之后必须进行10次编辑的是完整的代码(完整的代码来自测试和其他代码,我刚评论出来,因为我不需要它或写出更好的东西:

package maze;


import java.io.*;
import java.util.ArrayList;

import static java.lang.System.*;

public class Maze
{
private static int[][] maze;

public Maze(int size, String line)
{



}

public boolean hasExitPath(int r, int c)
{
    return false;
}

public String toString()
{
    String output="";
    return output;
}

public static void doMaze(int x, int y)
{

}

public static void readMaze() throws IOException
{
    System.out.println(0);
    BufferedReader read = new BufferedReader(new FileReader("maze.dat"));
    String numString;
    int numLine;
    String mazeLine;
    int row = 0;
    int x = 0;
    int lastChar = 0;

    while ((numString = read.readLine()) !=null && (mazeLine = read.readLine()) !=null)
    {
        System.out.println(1);
        numLine = Integer.parseInt(numString.replaceAll("\\s+", ""));
        System.out.println("numString" + numString + "numLine" + numLine); //both set to 5...
        ArrayList<Character> curLine = new ArrayList<Character>();
        for(int d = 0; d < numLine; d++) curLine.add('a');
        System.out.println("curLine " + curLine.size());

        for (int z = 0; z < mazeLine.length(); z++)
        {
            System.out.println(2);
            // get first row of numbers >> need to check lastchar
            // sort and send to get added
            // if remainder of line is more than numLine
                // go to step one, but start from lastchar
            if (lastChar < curLine.size())
            {
                System.out.println(3);
                for (x = 0; x < numLine; x++);
                {
                    System.out.println(4);
                    curLine.add(mazeLine.charAt(x + lastChar));
                }
                lastChar = x + lastChar;
                System.out.println("row " + row);
                sort(curLine, row);
                row++;

            }
            else{System.out.println("error"); System.out.println(lastChar); System.out.println(curLine.size());}

        }
        /*
        numLine = Integer.parseInt(numString.replaceAll("\\s+", ""));
        ArrayList<Character> curLine = new ArrayList<Character>(numLine);
        for (int x = 0; x < numLine; x++)
        {
            //System.out.println(mazeLine.charAt(x));
            System.out.println(maze[0][0]);
            curLine.add(mazeLine.charAt(x));
        }
        y++; */ 
    }
    read.close();
}

public static void sort(ArrayList<Character> line, int row)
{
    System.out.println("line size " + (line.size()-1));
    System.out.println("maze row check " + (maze[row] == null));
    maze[row] = new int[line.size()-1];
    for (int x = 0; x <line.size(); x++)
    {
        maze[row][x] = Integer.parseInt(String.valueOf(line.get(x)));
        System.out.println("final sopt " + maze[row][x]);
    }
}

}

0 个答案:

没有答案