0

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;
}
4
  • Can you post a stacktrace? Commented Aug 29, 2014 at 8:42
  • Please mark a line where NPE occur Commented Aug 29, 2014 at 8:42
  • 1
    First of all don't use new String(); as you create new String objects unnecessarily. Second can you provide some stack trace and more information Commented Aug 29, 2014 at 8:42
  • 1
    The code is not the same for String and int, in the int code, you are correctly creating int arrays (int [] xAxis = new int[triangle.length - y];), and using them, but not in the String code. Commented Aug 29, 2014 at 8:42

1 Answer 1

3

You never initialize the second dimension of the array pathingTriangle = new String[triangleSize][];

which is done in the first funnction here int [] xAxis = new int[triangle.length - y];

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

1 Comment

Thanks for your help. I reverted the code back to that, and it all worked out somehow. What's weird is that I had originally just copied and pasted the code, and replaced int with String and it didn't just work from the get-go.

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.