I'm getting a Null pointer access: The variable 'numbers' can only be null at this location error from intellisense with the following code. (Errors marked)

public static int isOne(int incoming){
    String original = Integer.toString(incoming);
    int length = original.length();
    int i;
    int numbers[] = null;

    for(i = 0; i < length; i++){
        String worker = Character.toString(original.charAt(i));
        int workInt = Integer.parseInt(worker);
/* HERE */  numbers[i] = workInt;
        System.out.print(i + "=" + workInt + ","); /* this line just tests it */
    }

    int z;
    int sum = 0;
    int thisNumber = 0;

    for(z = 0; z < length; z++){
/* HERE */  thisNumber = numbers[z]; 
        thisNumber = thisNumber * thisNumber;
        sum = sum + thisNumber;
    }

    return 0;
}

When running the code, the console gives an exception on the first error. LogCat gives nothing.

Intellisense give the suggestion of an @suppress for the two errors.

I really don't have a clue, but I think it might be at the point where I initialise 'numbers'.

Thanks for any help.

share|improve this question

1  
Well, you don't really initialise numbers. You set it to null, which unsurprisingly causes a null pointer exception later on. – biziclop Mar 30 at 18:21
feedback

8 Answers

up vote 6 down vote accepted

You assign numbers with null instead of assigning it with an array.

Change:

int numbers[] = null;

into

int[] numbers = new int[length];

Note that in java, declaring int[] myVar only allocates a reference to the array, and does not actually create the array object. In order to allocate the array itself, you use new int[size], and assign the new object to the variable you want.

share|improve this answer
dang... always seconds too slow. – digitaljoel Mar 30 at 18:22
feedback

this code is really strange, inefficient and buggy.

And useless while always returning 0

Try the following

public static int isOne(int incoming){
   return 0;
}

;-) or with the same calculations and a real, sorry int, return value

public static int isOne(int incoming){
   int sum = 0;
   int pos = 0;
   while (true) {
     int digit = incoming % 10;
     System.out.print(pos + "=" + digit + ","); /* reverse order!, this line just tests it */
     incoming /= 10;
     if (incoming == 0) break;
     sum += digit * digit;
     pos ++;
   }
   return sum;
}
share|improve this answer
the returning 0 is just so it returns something (I had it returning a boolean at first, and that made me have a return statement). Returning some is what it will do when I finish typing this comment ;) – ACarter Mar 30 at 18:42
feedback

...or more exactly, where you don't initialise numbers.

Arrays in Java must be declared (that part you have done) and initialised before any usage (that part you haven't). Instead of the null line, you should type:

int[] numbers = new int[length];

which creates a new and empty array of integers and length as its length.

By the way, it's more common to write int[] numbers that int numbers[], because from the former, it's more obvious it is an array.

share|improve this answer
feedback

Because you are setting the value to null. I think what you meant to do is

int numbers[] = new int[length];
share|improve this answer
feedback

You need to initialize the size of your array like this:

int number = new int[length];

Another suggestion is to use the Java ArrayList which will allow more flexibility in creating your array. You won't need to initialize to a size and you can add to this array in your loop:

ArrayList<Integer> numbers = new ArrayList<Integer>();
share|improve this answer
feedback

Change:

int numbers[] = null;

to

int[] numbers = new int[length];
share|improve this answer
feedback

You are right. The problem is that you are not initializing numbers

replace

int numbers[] = null;

with

int numbers[] = {1,2,3,4,5}

share|improve this answer
feedback

You never initialize numbers. You set it to null, and then you try to assign to numbers[i]. You need to set numbers to a new array

int[] numbers = new int[length];
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.