1

I have a method setColor, returning float[]. It always returns float array with size=3. When I use it like this:

 float[] color=setColor(0);
 content.setColor(Color.getHSBColor(color[0], color[1], color[2]));

there is a NullPointerException.

When I debug my program color[0], color[1], color[2] are defined, but it says that there is a NullPointerException.

How can I fix this?

Here is code of 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;
}

Here is constructor of class.

DrawOutput (MinDistances requiredMinDistances, MainMatrix matrix){
    super();
    getRequiredMedoidsArray(requiredMinDistances);
    paint(getGraphics(), requiredMinDistances, matrix);
}

getGraphics is null, Any suggestions?

9
  • 1
    The variable content may be null. Have you checked this? Commented Apr 27, 2013 at 13:58
  • 1
    Can you provide more code? Commented Apr 27, 2013 at 14:00
  • with two code lines it's pretty difficult to get a correct answer for a NullPointer. Commented Apr 27, 2013 at 14:01
  • 1
    Post your stack trace (your exception). Commented Apr 27, 2013 at 14:02
  • Yeah, sory added more code. Commented Apr 27, 2013 at 14:04

3 Answers 3

2

This line can throw NPE if

  1. color is null
  2. content is null

If color is not null therefore check your content.

Sign up to request clarification or add additional context in comments.

3 Comments

If the people who are downvoting because of context instead of content then this downvotes should be casted to upvotes.. +1 from me for correct answer with a small typo.
@Luiggi Mendoza, I'd like to know PID of those people. Sometimes I'd enjoy to send them kill -9 :)
I think the downvotes are origined by the "simplicity" of what he's asking and the vague way the OP has made the question. Just saying (didn't downvote).
2

The content variable is null.

4 Comments

The is not an answer. It's a question.
This is an answer grammatically composed as question.
You are right, it's a question... The answer is "check the variable content"
Please change the form of your question to be an answer (if I do it, that will be messing with your answer).
0

Your NPE is in this line:

content.setColor(Color.getHSBColor(color[0], color[1], color[2]));

because content is null.

float array is always created here float[] hsbValues = new float[3] and since color[0], color[1], color[2] are float, they can't be null.

3 Comments

coloris an array and can be null. In that case color[0] would throw a NPE.
It can't, setColor() always returns an array object.
That's true, I just missed that you mentioned that in your answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.