So I am just having some fun when I realize that the color is returning null. The odd thing though, is the Color is made during the implementation. The code I have is this:
package org.legend.game;
import java.awt.Color;
public class Type {
final static Type GROUND;
final static Type AIR;
static{
AIR = new Type(0);
GROUND = new Type(1);
}
private Color c;
Type(int type) {
Color c = colorFromType(type);
System.out.println(c);
this.c = c;
}
public Color getColor() {
return c;
}
private Color colorFromType(int num) {
switch (num) {
case 0:
return new Color(0, 0, 0, 0);
default:
return new Color(255, 255, 255, 255);
}
}
}
I tried using enums before, but that did not work either. Technically speaking, this should work, but for some reason, the colors always return null.
Does anyone know why this is happening? I am running this through an Applet FYI.
Example print:
java.awt.Color[r=0,g=0,b=0]
java.awt.Color[r=255,g=255,b=255]
//Classic NullPointerException linking to the Type#getColor() method.
Thanks,
Legend.
If you get a NullPointerException
when you do
type.getColor()
then it's type
that is null
, not the return value of the method call.
Once you sorted out the error, I suggest you go back to an enum. That's a much better approach for these types of objects.