0
public static int[][] toRGB(float[][] gray) {
    int [][]imageRGB;
    for(int i=0; i<gray.length;i++) {
        for(int j=0; j< gray[0].length; j++) {
            imageRGB[i][j]=getRGB(gray[i][j]);
        }
    }
    return imageRGB;
}

This method won't work. Java tells me that it's because the variable imageRGB hasn't been initialised. Any idea how to fix this?

Alexis C.
  • 91,686
  • 21
  • 171
  • 177

1 Answers1

0

Unlike member variables, local variables are not initialized by default in Java. In Java member variables (Objects) are initialized to null by default. Thus compiler will not complain but your code will throw NullPointerException at runtime. To avoid Exception, you must create instance of it (using new) before using it.

In your case, you need to create a new instance of the array before using it. Try this:

int [][]imageRGB = new int[length][length];

If you are not sure about the length of the imageRGB, try using List or Set Collection in Java.

List<List<Integer>> = new ArrayList<>();
Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
  • I can raise many questions about your first paragraph, could you make it better so I can vote you up? Just one point, array is object cuz it used new keyword and it is not even member variables ? I do not understand what do you mean by Java member variables ? – Kick Buttowski Oct 20 '15 at 22:04