My code compiles but it has a java null error.

import java.util.Arrays;

public class Example
{
    private int[] NumberArray;

    public Example()
    {
        int[] NumberArray = new int[4];
    }

    public void setNumberArray()
    {
        NumberArray[0] = 20;
        NumberArray[1] = 15;
        NumberArray[2] = 22;
    }

    public void printnumberArray()
    {
        for(int index=0; index < NumberArray.length; index++)
        {
           System.out.println(NumberArray[index]);
        }
    }
}

EXACTLY what code are you executring and EXACTLY what is thet COMPLETE error message?

ps Did you intend that line 9 will create a new "NumberArray" variable? The NumberArray that you initialise in setNumberArray isn't the one declared on line 5!

I'm trying to see how the array works and i'm trying to print the array using a for loop. Yes on the "NumberArray variable but don't you have intiazile the array? My error is

java.lang.NullPointerException
    at Example.printnumberArray(Example.java:28)

Yes.
YOu have TWO variables"NumberArray", declared on lines 5 and 9.
The one you initialise is declared on line 9, and never used anywhere else.
The one declared on line 5 is the one you (try to) use, but that's not initialised.

how would i be able to use the array i declared?

Your mistake is declaring the same variable name twice. On line 9, just use the one you already have, don't declare a new one.

thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.