Java 插入二维字符串数组时获取空指针异常

Java 插入二维字符串数组时获取空指针异常,java,arrays,string,nullpointerexception,Java,Arrays,String,Nullpointerexception,我有一个名为input1.txt的文件,内容如下: a、b、c、d b-d c d d e 我想读它,把它们放在二维字符串数组中。我已经为它编写了代码。但它显示空指针异常。错误在哪里?下面是我的代码: 我在graphNodes[I][j]=s行中得到异常 BufferedReader br = null; BufferedReader cr = null; int lines = 0; try { br = new BufferedReader(new F

我有一个名为input1.txt的文件,内容如下:

a、b、c、d b-d c d d e

我想读它,把它们放在二维字符串数组中。我已经为它编写了代码。但它显示空指针异常。错误在哪里?下面是我的代码:

我在
graphNodes[I][j]=s行中得到异常

BufferedReader br = null;
    BufferedReader cr = null;
    int lines = 0;
    try {
        br = new BufferedReader(new FileReader(filename));
        try {
            while (br.readLine() != null) {
                lines++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    }
    List<String> nodes = new ArrayList<String>();
    String[][] graphNodes = new String[lines][];
    String[] line = new String[lines];
    int i = 0;
    int j = 0, x = 0;
    try {
        cr = new BufferedReader(new FileReader(filename));
        while (cr.readLine() != null) {
            line[x] = cr.readLine();
            System.out.println("Line is: " + line[x]);
            String[] letters = line[x].split(" ");
            for (String s : letters) {
                 System.out.println("Letter is " + s);
                graphNodes[i][j] = s;
                j++;
            }
            i++;
            x++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
BufferedReader br=null;
BufferedReader cr=null;
int行=0;
试一试{
br=新的BufferedReader(新的文件读取器(文件名));
试一试{
while(br.readLine()!=null){
行++;
}
}捕获(IOE异常){
e、 printStackTrace();
}
}捕获(FileNotFoundException e1){
e1.printStackTrace();
}
列表节点=新的ArrayList();
字符串[][]图形节点=新字符串[行][];
字符串[]行=新字符串[行];
int i=0;
int j=0,x=0;
试一试{
cr=新的BufferedReader(新的文件读取器(文件名));
while(cr.readLine()!=null){
第[x]行=cr.readLine();
System.out.println(“行是:“+Line[x]);
字符串[]个字母=行[x]。拆分(“”);
for(字符串s:字母){
System.out.println(“字母为”+s);
图形节点[i][j]=s;
j++;
}
i++;
x++;
}
}捕获(IOE异常){
e、 printStackTrace();
}

在访问其
j
索引之前,您需要实例化
图形节点[i]

在访问其
j
索引之前,您需要实例化
图形节点[i]

图形节点缺少列长度

String[][] graphNodes = new String[lines][];
在您的问题中,一旦获得
字母
,就可以初始化2d数组的列

String[] letters = line[x].split(" ");
graphNodes[i] = new String[letters.length];

图形节点
缺少列长度

String[][] graphNodes = new String[lines][];
在您的问题中,一旦获得
字母
,就可以初始化2d数组的列

String[] letters = line[x].split(" ");
graphNodes[i] = new String[letters.length];

首先,您不需要读取文件两次。一次获取行数,另一次获取实际数据。
您需要阅读这些行并将它们直接放入列表中。然后,您可以使用该列表执行任何需要的操作。

首先,您不需要读取两次文件。一次获取行数,另一次获取实际数据。
您需要阅读这些行并将它们直接放入列表中。然后,您可以对该列表执行任何需要的操作。

我相信您在以下代码方面遇到了问题:

try {
    cr = new BufferedReader(new FileReader(filename));
    while (cr.readLine() != null) {
        line[x] = cr.readLine();
        System.out.println("Line is: " + line[x]);
        String[] letters = line[x].split(" ");
        for (String s : letters) {
             System.out.println("Letter is " + s);
            graphNodes[i][j] = s;
            j++;
        }
        i++;
        x++;
    }
}
while语句表示“while cr.readLine()!=null”,此时它读取了文件的第一行,将其与null进行比较,它不是null,因此进入循环。然后,您告诉它将第[x]行设置为cr.readLine(),然后它读取文件的下一行,并将其设置为第[x]行。因此,跳过第一行代码,除了使用它来检查while循环条件之外,没有做任何其他事情

我想在while循环中你想要的是这样的东西

try {
        cr = new BufferedReader(new FileReader(filename));
        for(String lineValue = cr.readLine(); lineValue != null; x++, lineValue = cr.readLine()) {
            line[x] = lineValue;
            System.out.println("Line is: " + line[x]);
            String[] letters = line[x].split(" ");
            for (String s : letters) {
                 System.out.println("Letter is " + s);
                graphNodes[i][j] = s;
                j++;
            }
            i++;
        }
    } 

但是正如前面提到的,您需要声明二维数组的大小。为了实现此循环,我刚刚将其设置为
String[lines][100]
,但您需要调整它以满足您的需要(无论您预计最长的字母行有多长)。

我相信您在以下代码方面遇到了问题:

try {
    cr = new BufferedReader(new FileReader(filename));
    while (cr.readLine() != null) {
        line[x] = cr.readLine();
        System.out.println("Line is: " + line[x]);
        String[] letters = line[x].split(" ");
        for (String s : letters) {
             System.out.println("Letter is " + s);
            graphNodes[i][j] = s;
            j++;
        }
        i++;
        x++;
    }
}
while语句表示“while cr.readLine()!=null”,此时它读取文件的第一行,将其与null进行比较,它不是null,因此进入循环。然后,您告诉它将[x]行设置为cr.readLine(),然后它读取文件的下一行,并将其设置为[x]行。因此,跳过第一行代码,除了使用它检查while循环条件外,对它不做任何操作

我想在while循环中你想要的是这样的东西

try {
        cr = new BufferedReader(new FileReader(filename));
        for(String lineValue = cr.readLine(); lineValue != null; x++, lineValue = cr.readLine()) {
            line[x] = lineValue;
            System.out.println("Line is: " + line[x]);
            String[] letters = line[x].split(" ");
            for (String s : letters) {
                 System.out.println("Letter is " + s);
                graphNodes[i][j] = s;
                j++;
            }
            i++;
        }
    } 

但是正如前面有人提到的,您需要声明二维数组的大小。为了这个循环,我刚刚做了
String[lines][100]
,但是您需要调整它以满足您的需要(无论您预计最长的字母行有多长。

首先,您没有为第二个维度指定值:
String[][]图形节点=新字符串[lines][]
)。 这意味着您基本上是试图将
s
设置为一个不存在的值。 您可以尝试先定义
String[]letters
,然后执行类似
String[]graphnodes=newstring[lines][letters.getLength()];

而且,
while(cr.readLine()!=null)
应该是
while(cr.hasNextLine())

如果您执行以下操作,您的代码看起来也会更干净:

    for (int i = 0, j = 0, x = 0; cr.hasNextLine(); i++, x++) {
        line[x] = cr.readLine();
        System.out.println("Line is: " + line[x]);
        String[] letters = line[x].split(" ");
        for (String s : letters) {
             System.out.println("Letter is " + s);
            graphNodes[i][j] = s;
            j++;
        }
    }

首先,您没有为第二个维度指定值:
String[][]graphnodes=newstring[lines][]
。 这意味着您基本上是试图将
s
设置为一个不存在的值。 您可以尝试先定义
String[]letters
,然后执行类似
String[]graphnodes=newstring[lines][letters.getLength()];

而且,
while(cr.readLine()!=null)
应该是
while(cr.hasNextLine())

如果您执行以下操作,您的代码看起来也会更干净:

    for (int i = 0, j = 0, x = 0; cr.hasNextLine(); i++, x++) {
        line[x] = cr.readLine();
        System.out.println("Line is: " + line[x]);
        String[] letters = line[x].split(" ");
        for (String s : letters) {
             System.out.println("Letter is " + s);
            graphNodes[i][j] = s;
            j++;
        }
    }

要实例化的值是多少?取决于数组长度。如果不知道长度,可以使用ArrayList。要实例化的值是多少?取决于数组长度。如果不知道长度,可以使用ArrayList。