0

The function is:

public Integer[] details;
private void putDetails(Integer l){
    if (l != null){
        int n = new Integer(0);
        n = details[l];
        details[l]=n+1;
    }
}

The error message say:

java.lang.NullPointerException
    at operacional.an_lex.putDetails(an_lex.java:30)

where the line 30 is : n = details[l];

Can you help me?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Chris Sum
  • 123
  • 1
  • 2
  • 11

1 Answers1

1

You need to initialize the details array:

//some number is the size of the array
int[] details = new int[some number]; 

or you can initialize like so if you want to fill the elements with 0's:

int[] details = new int[]{0,0,0,0}; 

What is happening is that you are getting an element from the details array, but that element doesn't have value. I'd check to make sure you have an assigned value in that array's element.

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
  • I tried that but is the same error ... – Chris Sum May 19 '15 at 19:27
  • 1
    @TekolinSum I'd suggest you post the rest of your code then. Otherwise, we can't help much more. – BlackHatSamurai May 19 '15 at 19:27
  • @TekolinSum Even when you initialize `details` with `Integer[] details = new Integer[10];` this array will be filled with `nulls`, and since `n` is `int` it can't store `null` returned by `details[l];`. You will need to fill your array with proper Integers first. – Pshemo May 19 '15 at 19:44
  • You opened my mind. That´s the error. I write now : `public Integer details[] = new Integer[4];` but how can I fill the 4 positions with zero without writing 4 lines using `details[0]=0; details[1]=0;` ? – Chris Sum May 19 '15 at 20:01
  • @TekolinSum Simplest way would be... not using `Integer` but `int` array which is by default filled with zeroes. – Pshemo May 19 '15 at 20:23
  • @BlackHatSamurai `int` array is by default filled with `0` so `{0,0,0,0}` part in case of `new int[]` is redundant. This array initialization part can be useful if type of array is `Integer` or if you want to set array content to something different then default values. – Pshemo May 19 '15 at 20:25
  • Thank you Masters !!! Now everything is ok ! – Chris Sum May 20 '15 at 00:58