I am creating a shadow of createTriangle() in the method createPathingTriangle(). createTriangle worked just fine, but when I created the identically-dimensioned createPathingTriangle that used String instead of int, the new triangle started throwing NPE's.
The line that's throwing it is-
pathingTriangle[y][x] = new String("00" + String.valueOf(x));
i.e. the first line that populates it. I looked it up and have liberally sprinkled "new" around the code in createPathingTriangle, but it didn't seem to solve the problem.. I am assuming that it has something to do with the fact that int is a primitive but String is not, but hours of fiddling and nothing gives.
private int[][] createTriangle() {
triangle = new int[triangleSize][];
for (int y = 0; y < triangle.length; y++){
int [] xAxis = new int[triangle.length - y];
for (int x = 0; x < xAxis.length; x++){
xAxis[x] = (int) (Math.random() * 100);
}
triangle[y] = xAxis;
}
printTriangle(triangle);
return triangle;
}
private String[][] createPathingTriangle() {
pathingTriangle = new String[triangleSize][];
for (int y = 0; y < pathingTriangle.length; y++){
for (int x = 0; x < pathingTriangle.length - y; x++){
if (x < 10){
pathingTriangle[y][x] = new String("00" + String.valueOf(x));
}
else if (x < 100){
pathingTriangle[y][x] = new String("0" + String.valueOf(x));
}
else{
pathingTriangle[y][x] = new String(String.valueOf(x));
}
}
}
return pathingTriangle;
}
new String();as you create newStringobjects unnecessarily. Second can you provide some stack trace and more informationStringandint, in theintcode, you are correctly creatingintarrays (int [] xAxis = new int[triangle.length - y];), and using them, but not in theStringcode.