2

I am parsing a grid coordinate in degree decimal format. I'd like to store pattern matches in a StringBuilder array for further manipulation. However, I am get a null pointer when calling append() in each while loop.

String coordinateExample = "N14585928W084144340";
this.parseCoordinate(coordinateExample);
...

public void parseCoordinate(String coordinateString) {

    int i = 0;

    //extract NSEW leading characters
    Pattern pL = Pattern.compile("[A-Z]");
    Matcher m = pL.matcher(coordinateString);        
    StringBuilder[] hemisphere = new StringBuilder[]{};

    while (m.find()) {
        hemisphere[i].append(m.group());
        //System.out.println("m.group(): " + m.group());
        i++;
    }        
    // reset i
    i = 0;

    //extract decimal degree digits
    Pattern pN = Pattern.compile("[0-9]++");
    Matcher n = pN.matcher(coordinateString);
    StringBuilder[] coordinate = new StringBuilder[]{};

    while (n.find()) {            
        coordinate[i].append(n.group());
        //System.out.println("q.group(): " + n.group());
        i++;
    }
}

4 Answers 4

6
StringBuilder[] coordinate = new StringBuilder[]{};

This is creating an empty array that can contain instances of type StringBuilder. What you most likely want to do is:

StringBuilder coordinate = new StringBuilder();

which creates an actual StringBuilder object that can be appended to.

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

4 Comments

I'll need two coordinate StringBuilder objects and two hemisphere objects. Do I have to create them separately? I'd rather loop through an array...
I thought that StringBuilder's mutability would allow me to create/initialize a StringBuilder array and then set the values later...
The problem is you are confusing an array with the actual StringBuilder Object. An array is an array and in no way encompasses or reflects the functionality of String Builder, it just happens to be able to contain instances of String Builder. If you want an array containing two different instances of string builder then you should do the following: StringBuilder[] coordinate = new StringBuilder[2]; coordinate[0] = new StringBuilder(); coordinate[1] = new StringBuilder(); You would then access each instance as coordinate[0].append("")
or with the literal syntax from you answer, StringBuilder [] s = new StringBuilder []{new StringBuilder(),new StringBuilder()};
3

You are creating array of string builders but your not instantiating it specify the length when you are using arrays

Solution // StringBuilder[] hemisphere = new StringBuilder[length];

for(int i=0;i<length;i++){
hemisphere [i]=new StringBuilder();
}

Then you wont get any null pointer exception I hope this helps

Comments

0

You are creating an array of StringBuilder without specifying the length i.e StringBuilder[] hemisphere = new StringBuilder[]{}; so you have an empty array that you are trying to populate hence the NullPointerException

Comments

0

I am really new to Java but why are you using the {} brackets after the instantiation of the builders? Shouldn't it simply be StringBuilder [] name = new StringBuilder []? Or perhaps StringBuilder [][] name = new StringBuilder [][]?

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.