mercredi 25 février 2015

NullPointerException from tring to get seperate RGB components


I am trying to get the seperate RGB components from each element in a 2D color array,but it keeps throwing a NullPointerException and I am not sure why. It might be something small that I missed, so some help would be really appreciated.


CODE:



Color[][] grid = new Color[MaxColors][MaxColors];

int R1 = 0;


for(int x = 0; x < N; x++){
for(int y = 0; y < N; y++){


R1 = grid[x][y].getRed();
/* do something with R1*/



}
}


I also tried to first add the colors to a list and and then convert it to a one-dimensional array. But I still get the same error.



List<Color> colorList = new ArrayList<Color>();

for(int x = 0; x < N; x++){
for(int y = 0; y < N; y++){


colorList.add(grid[x][y]);

}
}

Color[] SortColors = colorList.toArray(new Color[colorList.size()]);

for(int x = 0; x < SortColors.length; x++){

R1 = SortColors[x].getRed();
System.out.print(" " + SortColors[x]);
System.out.print(" " + R1);

}


It prints the following:


java.awt.Color[r=98,g=85,b=217] 98 java.awt.Color[r=254,g=110,b=177] 254Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at DrawGrid.ColourSorting.sortColours(ColourSorting.java:43)


Some help would be great. thank you!





Aucun commentaire:

Enregistrer un commentaire