I would like to print my values in my array. I am filling my values with a string tokenizer that I leave up to the user to fill.

When I use this method I get extra values since its initialized to 10.

int[] anArray = new int[10];
for (int i = 0; i < anArray.length; i++)
            {
                System.out.println("The values in the array " + anArray[i]);
            }

When I use this method it gives me a null error. If I don't initialize it complains that I haven't intialized it.

int[] array = null;
for (int i = 0; i < anArray.length; i++)
            {
                System.out.println("The values in the array " + anArray[i]);
            }

What can I do to print the values filled values in array? 

remark 1: don't use StringTokenizer, it has been legacy for quite a while, and has been replaced by the much easier to work with 'split' method in the String class.
remark 2: you are not using the array variable, so why it gives you any problems. I have no idea. best thing is to copy paste the actual code you are trying to run. if you meant:

int[] anArray = null;
for (int i = 0; i < anArray.length; i++)
            {
                System.out.println("The values in the array " + anArray[i]);
            }

then the problem is clear. your array doesn't have a length, nor does it have elements. provide elements before you try to access them.

... another reason to use split() is that it returns an array of exactly the right length, ie every value in the array is filled.

Ok the answer is check to see if the cell in the Array is null before trying to access if.

int[] anArray = new int[10];

for(int i=0; i < anArray.length; i++)
{
    if(anArray[i] != null)
    {
        System.out.println("Value: " + anArray[i]);
    }
}

ObSys: just to point out a few very basic, yet fatal flaws in the code you've provided, and to remind that it is better to provide a sollution in concept then to hand out custom made code (especially if the code is wrong)

what's the use of that test in your code? it is impossible for the array to be null there.
not to mention that the elements of the array (which you are checking for) are primitives, which means they can't be null.

it's nice that you added a test, but you should have noticed that you added the test on the wrong place. if you want to work like this, you'll need to check whether the array is null before you try to iterate over it.

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.