-2

I keep getting a NullPointerException at (see below). Everything works fine in C#, but in android it breaks?

arrDBNumbers is full and code is supposed to run through and count the amount of #1, #2, #3 and so on to #49 adding 1 to arrFreq[i][1] to fill arrFreq with the total count of the numbers.

It runs through the if statement till k hits 6 where arrDBNumbers[0][6] is 1, then jumps inside if statement and then breaks? I'm not sure whats going on here any advice thanks in advance T

Integer[][] arrDBNumbers = new Integer[100][8];
Integer[][] arrFreq = new Integer[49][2];


for (int i = 0; i < 49; i++){
    for (int j = 0; j < 49; j++){
        for (int k = 1; k < 7; k++){
            if (arrDBNumbers[j][k] == (i + 1)){
                arrFreq[i][1]++;     //  < here is where I get Exception?
            }
        }
    }
}
5
  • what is i when the exception is thrown? Commented May 1, 2013 at 17:23
  • While the question's been answered.. I think that's also susceptible to an IOBE.. k > 2.. also i+1 if i is 48..? Commented May 1, 2013 at 17:23
  • Why do you use Integers rather than ints? Commented May 1, 2013 at 17:24
  • It is a java question not android. It is better to change the title Commented May 1, 2013 at 17:24
  • No line number for nullpointer. Commented May 1, 2013 at 18:03

2 Answers 2

5

Because just writing

Integer[][] arrFreq = new Integer[49][2];

means you have initialized the array with all null elements because it is an array of Integer Objects and Object's default value will be null reference. Hence,

arrFreq[i][1]++;  // trying null++;

gives NullPointerException.

This wouldn't have been the case if you had used an array of primitives, which will default to an array of 0s.

int[][] arrFreq = new int[49][2];
Sign up to request clarification or add additional context in comments.

2 Comments

Hi thanks for quick reply, so Integer[][] arrFreq; and leave it at that yea.
not sure why that is but it works : Please check Java tutorials to know what are the default values of primitives and references.
0

Integer is a container for int, in your case you have lots of null objects. if you use int instead it will work as you expect.

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.