1

I'm having trouble initializing an array. When I try to draw the Array I'm getting a NullPointerException.

I need to access the class where I've declared the array from another class, that's why it's static.

Here's my code:

static int[][] DayOfTheMonth = new int[3][10];


public static void ArrayValue() {
    for (int column = 0; DayOfTheMonth.length < 4; column++) {
        for (int row = 10; DayOfTheMonth[column].length < 10; row++) {
            if (DaysofTheMonth <= Tag.MaximumDaysOfAMonth()) {

                DayOfTheMonth.[column][row] = Date.getDate() + DaysofTheMonth;
                DaysofTheMonth++;

            } else if (DaysofTheMonth > Tag.MaxDay()) {
                DaysofTheMonth = 1;

                if (Month != 12)
                    Month++;
                else {
                    Month = 0;
                    Year++;

                }
            }
        }
    }
}

Another problem is that, when I try to access the method trough my main class, it says:

Exception in thread "Main" java.lang.ArrayIndexOutOfBoundsException: 3
0

2 Answers 2

2

ArrayIndexOutOfBoundsException states you are trying to access an Element from and Index that does not exists so,

In this line:

for (int column = 0;  DayOfTheMonth.length < 4; column++)

You have specified to go For Loop to go infinite because the length will always be less than 4 so you need to have column in condition like

for (int column = 0;  column < DayOfTheMonth.length; column++)

So Make it loop until 3, as it will start from 0 and go up to 3.

And 1 more thing for your clarity the 1st Thing is Row and Second is column, so you have 3 rows and 10 columns although its just related to naming-problem but you should be clear about it.

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

Comments

2

This is 2 questions. I can't answer the first because you don't say where the exception happens, and I don't know what you mean by "draw" the array.

For the second, your problem is here (and in similar places):

for (int column = 0; DayOfTheMonth.length < 4; column++) 

DayOfTheMonth.length will always evaluate to 3, so column will keep increasing. What you probably want is

for (int column = 0; column < DayOfTheMonth.length; column++) 

I make no claims as to whether this is the only problem.

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.