So I'm working on an applet that takes in 4 different user input values and displays them via a pie chart. I've done all the calculations to divide up the input values and insert them into each piece of the pie chart, inserted them. That's all I have to say, sorry, help, please.

I keep getting a -> java.lang.NullPointerException exception in my paint function. Frankly my eyes are a little tired of looking at this code and I'm not sure what's wrong with this code.

*noRisk = Double.parseDouble(noRiskTextField.getText()) ;*//line that is highlighted as error.

Please post the full text of the error message.
Look at the line of code where the NPE occurred and see what variable is null.
It can help if you print out all the variables on that line to see which one is null.

Comment on testing value of String. Do NOT use ==
if(doesItMatch == "fillColor");

Use the String equals method

Also test your logic flow by adding print outs to show the value of variables at key points (ie before if tests and at the beginning of methods).
What is the value of doesItMatch in the paint method?

Is the init method called?

Use the @Override statement before any methods that you override to have the compiler verify that you have in fact overridden a method and not created a new one.

05-13-2011, 11:52 PM

Fubarable

Quote:

Originally Posted by jeskoston

I keep getting a -> java.lang.NullPointerException exception in my paint function. Frankly my eyes are a little tired of looking at this code and I'm not sure what's wrong with this code.

*noRisk = Double.parseDouble(noRiskTextField.getText()) ;*//line that is highlighted as error.

Seeing this should tell you that a variable that is being "dereferenced" does not refer to an object but rather is null. The only variable on that line that could cause this problem is the noRiskTextField. So, you have to ask yourself, why is noRiskTextField not being initialized since you seem to do it in your init method. Then you have to wonder, is init being called? I think not since init is not spelled correctly and so the JVM won't find it and call it. Fix the spelling of this method.

Other suggestions:

Since this is a Swing application, use a Swing JApplet, not an AWT Applet.

Don't check for String equality with == but rather use the equals(...) method or equalsIgnoreCase(...).

You're better off doing your graphics from within a paintComponent method of a JPanel that is then added to the applet rather than from a paint method in the JApplet itself.

05-14-2011, 12:16 AM

dlorde

Quote:

Originally Posted by Fubarable

Then you have to wonder, is init being called? I think not since init is not spelled correctly and so the JVM won't find it and call it. Fix the spelling of this method.

That's well spotted, innit? :D

05-14-2011, 12:24 AM

jeskoston

Well spotted indeed, kudos, but not the end of my problems. I'll look over it a bit more, modify and ask more specific questions if I have any. Thanks. -O.O

05-14-2011, 12:33 AM

Norm

There's more to find.
If you add some print outs to the code to show variables as they are used and change and to show execution flow, that'll help you find the other problems.