0

I'm trying to initialize an array (of a class object I created) of 2 dimensions, but I keep on having the same runtime error:

Exception in thread "main" java.lang.NullPoointerException
    at ........

I've managed to do it with primitive types, but not types extending object and I would like to know if it's possible (and if it's the case how).

Here's an exemple of my code:

MyCustomObject[][] matrix = new MyCustomObject[10][10];

for (int i = 0; i < 10; i += 1)
    matrix[i][0] = new MyCustomObject("some arguments ...");

The error is marked at the line were I try to give a value to the matrix: matrix[i][0] = ....

From what I understand after my researches, Java gave the value null to every member of the array, which is okay for me. But why would it mark me an error when I'm trying to replace the null value with an existing one. I'm not calling a method on null.

EDIT

Full code:

int sourceLength = source.length(); // Length of a CharSequence
int targetLength = target.length(); // Length of a CharSequence
Matrix distanceMatrix[][] = new Matrix[sourceLength][targetLength];

for (int row = 1; row < sourceLength; row += 1) {
    distanceMatrix[row][0] = new Matrix(        // The error is marked at this line.
        distanceMatrix[row - 1][0].cost + option.getDeletionCost(),
        row - 1,
        0
    );
}

for (int column = 1; column < targetLength; column += 1) {
    distanceMatrix[0][column] = new Matrix(
        distanceMatrix[0][column - 1].cost + option.getInsertionCost(),
        0,
        column - 1
    );
}

for (int row = 1; row < sourceLength; row += 1) {
    for (int column = 1; column < targetLength; column += 1) {

        // do more stuff.

    }
}

Matrix class (which is inside the main class):

public final static class Matrix {

    public int cost;
    public int row;
    public int column;

    public Matrix(int cost, int row, int column) {
        this.cost = cost;
        this.row = row;
        this.column = column;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Matrix)
            return (cost == ((Matrix)obj).cost
                && row == ((Matrix)obj).row
                && column == ((Matrix)obj).column);
        return (super.equals(obj));
    }

}

Stack traces:

Exception in thread "main" java.lang.NullPointerException
    at net.azzerial.gt.core.Fuzzy.distance(Fuzzy.java:54)
    at net.azzerial.gt.core.Fuzzy.levenshteinDistance(Fuzzy.java:24)
    at net.azzerial.gt.Test.main(Test.java:15)
2
  • Can you post a longer stacktrace please and the arguments you give to the new MyCustomObject. Commented Nov 8, 2018 at 12:06
  • I'll send the full class + stack traces once back in front of my computer. Commented Nov 8, 2018 at 12:33

3 Answers 3

2

I tried out the same thing and it works. Here's my code:

 public class StackOverFlow {

    public static void main(String[] args) {
        Foo[][] foos = new Foo[10][10];
        for(int i=0;i<10;i++){
            foos[i][0]= new Foo();
        }
    }

    public static class Foo{
    }
}

Can you tell us which version of java you are using and your pom.xml if you are using maven? Also, just to be sure, you can also post the code snippet of MyCustomObject.

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

1 Comment

I'm using Java 8 and gradle. I'll retry the code to see if it works. If not, I'll send the complete class in a new message. Thanks for the help!
0
Matrix distanceMatrix[][] = new Matrix[sourceLength][targetLength];

for (int row = 1; row < sourceLength; row += 1) {
    distanceMatrix[row][0] = new Matrix(        // The error is marked at this line.
        distanceMatrix[row - 1][0].cost + option.getDeletionCost(), //actually it occurs here
        row - 1,
        0
    );
}

The problem is when you try to call distanceMatrix[row - 1][0].cost and row==1. You never created distanceMatrix[0][0], it's null, and you try to access it's cost field. I assume the option object isn't null (worth checking as well).

If a single method call is few lines long, the stack trace will point the line where the call starts. E.g. new Matrix() call starts at line 54 and ends at 58, error occurs at line 55 but the stack trace points toward 54.

1 Comment

Thanks for the help, I totally forgot that I was calling an empty object element in the constructor's parameters... And thanks as well for teaching me that the stack trace will always point the 1st line of a method which is few lines long!
0

Your code seams to be correct. There's just a logical error at matrix initialization. And what about your constructor? There is some code inside that may be causing the NPE?

If the NPE occurs inside constructor, the compiler may point the error at the line matrix[i][0]... when it's happening inside the constructor. You may be looking just the upper of the stack trace when the real problem is bellow. Just the full stack trace can say.

1 Comment

I'd say that no, there's no way to get a NPE beside calling a method with the null value. I've edited the post so that there's more info.

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.