1

all I am interesting learning how to achieve a 2d String [][] array console printout using only native, pure java. The following code prints out a 2d matrix when I run my program, but one particular line was confusing while doing some test runs on converting my string to a 2d array:

rulesTable[i][j] = rulesMatrix[j];

Is there a way I could code this wherein rulesMatrix would be = to a 2d String array instead of the 1d String array I currently have? Any tips would be great. Thank you. The following snippet contains the loop-in-question:

protected void forgeGrammar()
{

    //this verifies that all 5 rules displayed in the console are valid
    grammarRules = new String(startingRule + secondRule + thirdRule + fourthRule + fifthRule);

    rulesArray = new String[grammarRules.length()];

    //remove the commas from the grammar  
    rulesArray = grammarRules.split("[,]");

    //convert the grammar to a 2D String array
    rulesTable = new String[rulesArray.length][5];
    for (int i = 0; i < rulesTable.length; i++)
    {
        String[] rulesMatrix = rulesArray[i].split(blank);

        for(int j = 0; j < rulesMatrix.length; j++)
        {
            rulesTable[i][j] = rulesMatrix[j];
        }
    }
1
  • Ffff, was long to read :) but I didn't get what is exactly your problem ... Commented Aug 21, 2012 at 8:33

1 Answer 1

1

In your code rulesTable Never gets initialized that's why NullPointer exception is thrown at below line

rulesTable[0][0].contains(rules_input.substring(0, 4));
Sign up to request clarification or add additional context in comments.

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.