【问题标题】:Getting null pointer exception while inserting into two dimensional array of Strings插入二维字符串数组时出现空指针异常
【发布时间】:2013-08-18 07:54:45
【问题描述】:

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

a b c d

b d

c d

d e

我想阅读它并将它们放入二维字符串数组中。我已经为它编写了代码。但它显示 NULL POINTER EXCEPTION。错误可能在哪里?以下是我的代码:

我在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();
    }

【问题讨论】:

    标签: java arrays string nullpointerexception


    【解决方案1】:

    graphNodes 缺少列长度

    String[][] graphNodes = new String[lines][];
    

    在你的问题中,一旦你得到letters,你就可以初始化二维数组的列

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

    【讨论】:

      【解决方案2】:

      您需要先实例化graphNodes[i],然后才能访问其j 索引。

      【讨论】:

      • 我需要实例化到什么值?
      • 这取决于数组长度。如果您不知道长度,可以使用 ArrayList 代替。
      【解决方案3】:

      我相信您在使用以下代码时遇到了问题:

      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,所以它进入了循环。然后你告诉它设置 line[x] 等于 cr.readLine() 然后读取文件的下一行,并将其设置为 line[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],但您需要对其进行调整以满足您的需求(无论您预计最长的字母行有多长。

      【讨论】:

        【解决方案4】:

        首先,您没有为第二个维度指定值:String[][] graphnodes = new String[lines][]。 这意味着您基本上是在尝试将 s 设置为一个不存在的值。 你可以尝试先定义String[] letters,然后再做类似String[][] graphnodes = new String[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++;
                }
            }
        

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-07-28
            • 2014-12-02
            • 2012-02-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-02-07
            • 1970-01-01
            相关资源
            最近更新 更多