1

I initially want to say thank you for taking the time to look at my post. Basically I have attempted to create a multidimensional array with random integers using Math.random. The code compiles and keeps returning the null pointer exception error message. I don't know what I did wrong in creating my object. Can anyone tell me what's wrong with the code?

public Table(int r, int c)
    {
        rows = r;
        columns = c;

        for (int i = 0; i < r; i++)
            for (int j = 0; j < c; j++)
                {
                    /*
                    * Here is where the error keeps returning, blueJ keeps pointing
                    * me to this line of code and it has to be the variables I am using
                    * in the array that are causing the issue. The only issue is I                       * don't know what to insert for that.
                    */
                    theTable[i][j] = (int)(100*Math.random());
                }
    }
1
  • 2
    I guess you haven't initialized theTable[i][j] with the size? Commented Dec 6, 2012 at 3:56

3 Answers 3

1

Where in your code are you initializing theTable? That can be the only thing on that line that is null. Ensure that where you declare theTable that you define it as well:

private int[][] theTable = new int[r][c]
Sign up to request clarification or add additional context in comments.

Comments

1

Add:

int[][] theTable = new int[r][c];

right before the for loops, if you want it to be local to the method. If you want it to be a member of the class, add

private int[][] theTable = new int[r][c];

at the top of your class.

Comments

0

You are neither declaring nor initializing theTable, so to Java, it doesn't exist. When you try to use a nonexistent object in Java, you will get a Null Pointer Exception. There are already correct answers giving solutions to your problem. I suggest that you use their code. durron597's is especially clear/good.

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.