1

When I try to pass the 2d array (myString) to constructor, Eclipse shows me NullPointerException in the following line:

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

Could smb please explain me what I did wrong? Thanks in advance!

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;
  }

}
1
  • 1
    In addition to the answers, you should fix this: x.length, x is a 2d array you need something like x[0].length. Commented Sep 5, 2015 at 6:20

4 Answers 4

2

You havn't Initialized your variable myBoard and in for loop you have to use x[i].length instead of x[0].length

Try this:-

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your myBoard is not initialized in java. Therefore, you should first intialize the object before you use it. Try this

private ArrayList<ArrayList<T>> myBoard = new ArrayList<>();

Comments

0

Your myBoard is null.

You should initialize it with,

private ArrayList<ArrayList<T>> myBoard = new ArrayList<>();

Comments

0

It looks like myBoard is never instantiated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.