将2d数组传递给构造函数时出现NullPointerException

时间:2015-09-05 06:11:35

标签: java arrays arraylist nullpointerexception

当我尝试将2d数组(myString)传递给构造函数时,Eclipse在以下行中显示NullPointerException

DenseBoard<String> temp1 = new DenseBoard<String>(myString, "a");

请问smb请解释一下我做错了什么?提前谢谢!

public class Tester {

    public static void main(String[] args){

        String[][] myString = {{"A B C"}, {"D E F"}, {"G H I"}};
        DenseBoard<String> temp1 = new DenseBoard<String>(myString, "a");
        System.out.println(temp1);
    }
}

Class DenseBoard

import java.util.*;
public class DenseBoard <T> {

 private ArrayList<ArrayList<T>> myBoard;

 public DenseBoard(T[][] x, T fillElem){

      for(int i = 0; i < x.length; i++){
         ArrayList<T> values = new ArrayList<T>();
          for(int j = 0; j < x[0].length; j++){
              values.add(x[i][j]);
          }
          myBoard.add(values);
      }
  }

 public String toString(){
      String result = "";
      for(int i = 0; i < myBoard.size(); i++){
          for(int j = 0; j < myBoard.get(i).size(); j++){
              result += myBoard.get(i).get(j);
          }
          System.out.println();
      }
      return result;
  }

}

4 个答案:

答案 0 :(得分:2)

您尚未初始化变量myBoard并且在for循环中您必须使用x[i].length而不是x[0].length

试试这个: -

package p1;
import java.util.ArrayList;
public class A1{
    public static void main(String[] args) {
        String[][] myString = {{"A B C"}, {"D E F"}, {"G H I"}};
        DenseBoard<String> temp1 = new DenseBoard<String>(myString, "a");
        System.out.println(temp1);
    }
}

class DenseBoard <T> {
    private ArrayList<ArrayList<T>> myBoard;
    public DenseBoard(T[][] x, T fillElem){
        myBoard = new ArrayList<>();
        for(int i = 0; i < x.length; i++){
            ArrayList<T> values = new ArrayList<T>();
            for(int j = 0; j < x[i].length; j++){
                values.add(x[i][j]);
            }
            System.out.println(i);
            System.out.println(values);
            myBoard.add(values);
        }
    }
    public String toString(){
        String result = "";
        for(int i = 0; i < myBoard.size(); i++){
            for(int j = 0; j < myBoard.get(i).size(); j++){
                result += myBoard.get(i).get(j);
            }
            System.out.println();
        }
        return result;
    }
}

答案 1 :(得分:1)

您的someone@somewhere:~/***/***% gulp [15:17:15] Using gulpfile ~/***/***/gulpfile.js [15:17:15] Starting 'default'... [15:17:15] Starting 'clean'... [15:17:15] Finished 'clean' after 4.28 ms [15:17:15] Starting 'build'... [15:17:15] Finished 'build' after 12 ms [15:17:15] Starting 'exec'... module.js:338 throw err; ^ Error: Cannot find module '/***/***/dist/app.js' at Function.Module._resolveFilename (module.js:336:15) at Function.Module._load (module.js:278:25) at Function.Module.runMain (module.js:501:10) at startup (node.js:129:16) at node.js:814:3 [15:17:15] 'exec' errored after 119 ms [15:17:15] Error: Command failed: /bin/sh -c node dist/app.js module.js:338 throw err; ^ Error: Cannot find module '/***/***/dist/app.js' at Function.Module._resolveFilename (module.js:336:15) at Function.Module._load (module.js:278:25) at Function.Module.runMain (module.js:501:10) at startup (node.js:129:16) at node.js:814:3 at ChildProcess.exithandler (child_process.js:751:12) at ChildProcess.emit (events.js:110:17) at maybeClose (child_process.js:1015:16) at Socket.<anonymous> (child_process.js:1183:11) at Socket.emit (events.js:107:17) at Pipe.close (net.js:485:12) [15:17:15] 'default' errored after 142 ms [15:17:15] Error: Command failed: /bin/sh -c node dist/app.js module.js:338 throw err; ^ Error: Cannot find module '/***/***/dist/app.js' at Function.Module._resolveFilename (module.js:336:15) at Function.Module._load (module.js:278:25) at Function.Module.runMain (module.js:501:10) at startup (node.js:129:16) at node.js:814:3 at ChildProcess.exithandler (child_process.js:751:12) at ChildProcess.emit (events.js:110:17) at maybeClose (child_process.js:1015:16) at Socket.<anonymous> (child_process.js:1183:11) at Socket.emit (events.js:107:17) at Pipe.close (net.js:485:12) 未在java中初始化。因此,在使用对象之前,应首先初始化对象。 试试这个

myBoard

答案 2 :(得分:0)

您的myBoard为空。

您应该使用

初始化它
private ArrayList<ArrayList<T>> myBoard = new ArrayList<>();

答案 3 :(得分:0)

看起来myBoard永远不会被实例化。