我有一个方法 setColor
,返回 float[]。它总是返回大小为 3 的 float 组。
当我这样使用它时:
float[] color=setColor(0);
content.setColor(Color.getHSBColor(color[0], color[1], color[2]));
有一个NullPointerException
。
当我调试我的程序时,color[0]、color[1]、color[2] 都定义了,但它说有一个 NullPointerException。
我该如何解决这个问题?
这里是setColor的代码
private float[] setColor (int colorID){
float[]hsbValues=new float[3];
if(colorID == 1){
hsbValues = Color.RGBtoHSB(0,255,255,hsbValues);
}
else if(colorID == 2){
hsbValues = Color.RGBtoHSB(255,0,255,hsbValues);
}
else if(colorID == 3){
hsbValues = Color.RGBtoHSB(0,255,0,hsbValues);
}
else if(colorID == 4){
hsbValues = Color.RGBtoHSB(255,255,0,hsbValues);
}
else if(colorID == 5){
hsbValues = Color.RGBtoHSB(255,0,0,hsbValues);
}
else if(colorID == 6){
hsbValues = Color.RGBtoHSB(255,255,255,hsbValues);
}
else{
hsbValues = Color.RGBtoHSB(0, 0, 0,hsbValues);
}
return hsbValues;
}
这是类的构造函数。
DrawOutput (MinDistances requiredMinDistances, MainMatrix matrix){
super();
getRequiredMedoidsArray(requiredMinDistances);
paint(getGraphics(), requiredMinDistances, matrix);
}
getGraphics 为空,有什么建议吗?
最佳答案
这一行可以抛出 NPE 如果
color
为空内容
为空
如果 color
不为空,则检查您的 content
。
关于java - 具有已定义数组的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16252942/