0
public class Tabel {
    private static int dimension;

    private ArrayList<ArrayList<Character>> tabel;


    public Tabel(int dimension) {

        Tabel.dimension = dimension;

        for (int i=0;i<Tabel.dimension*Tabel.dimension;i++) {
           tabel.add(new ArrayList<Character>());
        }

     }
}

When I try to debug (eclipse ide) I get a lot of weird "errors" or at the very least I encounter something I consider unexpected.

The private static int does not appear in the "variables" section of debug.

I get a NullPointerException on tabel.add(...) but when I watch the debug, it enters the for once, does not add anything in the table because when I hit "next" instead of jumping to the closing braces it jumps out of the function.

If I comment the .add it works so that's the problem (I think). Is my syntax wrong ? or should I post more code ?

2
  • 4
    Just a note: Having a static variable that gets initialized by the constructor seems like a recipe for disaster. Commented May 21, 2013 at 17:20
  • @Keppil This is a "thin version" I have a boolean that makes sure it only happens once, I know it's not the best way but I have no other idea. Commented May 21, 2013 at 19:20

3 Answers 3

5

tabel is not initialized, so it is null.

Change

private ArrayList<ArrayList<Character>> tabel;

to

private ArrayList<ArrayList<Character>> tabel = new ArrayList<ArrayList<Character>>();

Or better:

private List<ArrayList<Character>> tabel = new ArrayList<ArrayList<Character>>();

since this does not tie tabel to ArrayList.

Sign up to request clarification or add additional context in comments.

Comments

1

You have not initialized the private List. Do the following:

private List<ArrayList<Character>> tabel = new ArrayList<ArrayList<Character>>();

Comments

0

I'd have trouble understanding that level of nesting too.

It's better to refer to List rather than ArrayList. Unless you need a method in the concrete class, it makes your program more flexible to refer to the interface and the methods in the interface.

Create a class (1) that has a field defined as a List of Character. Set the field to a new ArrayList in the constructor.

Create another class (2) that has a field defined as a List of class (1). Set the field to a new ArrayList in the constructor.

Create another class (3) that has a field defined as a List of class (2). Set the field to a new ArrayList in the constructor.

Since you understand what you're doing, you can give these 3 classes more meaningful names.

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.