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)