Results 1 to 6 of 6
- 05-13-2011, 11:46 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
Need help making an applet that displays a pie chart.
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.
Java Code:import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JFrame; import java.applet.Applet; import java.awt.event.ActionListener; import javax.swing.*; import java.awt.*; //import javax.swing.border.EtchedBorder; //import javax.swing.border.TitledBorder; import java.awt.event.ActionEvent; public class PieChart extends Applet implements ActionListener { Double switchy; String doesItMatch = ""; //Declare GUI components private double noRisk; private double lowRisk; private double moderateRisk; private double highRisk; private double noValue; private double lowValue; private double moderateValue; private double highValue; private double totalValue; private double noSlice = 0; private double lowSlice = 0; private double moderateSlice = 0; private double highSlice = 0; private double tempPie1 = 0; private double tempPie2 = 0; private JTextField noRiskTextField; private JTextField lowRiskTextField; private JTextField moderateRiskTextField; private JTextField highRiskTextField; private JLabel noRiskLabel; private JLabel lowRiskLabel; private JLabel moderateRiskLabel; private JLabel highRiskLabel; private JButton drawButton = new JButton(); private JButton resetButton = new JButton(); //implement pieChart as init for JApplet public void innit() { //Create Buttons drawButton = new JButton("Draw Pie Chart"); add(drawButton); drawButton.addActionListener(this); drawButton.setActionCommand("Draw"); resetButton = new JButton("Reset"); add(resetButton); resetButton.addActionListener(this); drawButton.setActionCommand("Reset"); //Create text fields noRiskTextField = new JTextField("",10); add(noRiskTextField); lowRiskTextField = new JTextField("",10); add(lowRiskTextField); moderateRiskTextField = new JTextField("",10); add(moderateRiskTextField); highRiskTextField = new JTextField("",10); add(highRiskTextField); //Create text labels noRiskLabel = new JLabel("No Risk"); add(noRiskLabel); lowRiskLabel = new JLabel("Low Risk"); add(lowRiskLabel); moderateRiskLabel = new JLabel("Moderate Risk"); add(moderateRiskLabel); highRiskLabel = new JLabel("High Risk"); add(highRiskLabel); setSize(350,425); } public void paint(Graphics g) { super.paint(g); if(doesItMatch == "fillColor"); { //get risk values, convert from string to double noRisk = Double.parseDouble(noRiskTextField.getText()) ; lowRisk = Double.parseDouble(lowRiskTextField.getText()); moderateRisk = Double.parseDouble(moderateRiskTextField.getText()); highRisk = Double.parseDouble(highRiskTextField.getText()); setBackground(Color.WHITE); //Determine the percentage of total pie value //as well as individual components of pie. totalValue = noRisk + lowRisk + moderateRisk + highRisk; noValue = ((noRisk/totalValue) *100); lowValue = ((lowRisk/totalValue) * 100); moderateValue = ((moderateRisk/totalValue) * 100); highValue = ((highRisk/totalValue) *100); //Use percentages of pie slices and distrubute by 360 noSlice = (noValue * 360/100); lowSlice = (lowValue * 360/100); moderateSlice = (moderateValue * 360/100); highSlice = (highValue * 360/100); tempPie1 = highSlice + moderateSlice; tempPie2 = tempPie1 + lowSlice; //fill in slices with color g.setColor(Color.RED); g.fillArc(100, 100, 200, 200, (int) tempPie2 ,(int) noSlice); g.setColor(Color.BLUE); g.fillArc(100, 100, 200, 200, (int) tempPie1 ,(int) lowSlice); g.setColor(Color.YELLOW); g.fillArc(100, 100, 200, 200, (int) highSlice ,(int) moderateSlice); g.setColor(Color.GREEN); g.fillArc(100, 100, 200, 200, 0 ,(int) highSlice); } }//End Paint //update the painted pie chart public void update(Graphics g) { paint(g);//will repaint g with the updated values }//end update public void actionPerformed(ActionEvent e) { //Object source = e.getSource(); if(e.getActionCommand() == "Draw") { doesItMatch = "fillColor"; repaint(); } else if(e.getActionCommand() == "Reset") { doesItMatch = ""; noRiskTextField.setText(""); lowRiskTextField.setText(""); moderateRiskTextField.setText(""); highRiskTextField .setText(""); repaint(); } } }
- 05-13-2011, 11:51 PM #2
Please post the full text of the error message.java.lang.NullPointerException exception in my paint function
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.Last edited by Norm; 05-14-2011 at 12:01 AM.
-
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 #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
- 05-14-2011, 12:24 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
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 #6
Similar Threads
-
Issue regarding making chart in jsf
By maulikmodi08 in forum JavaServer Faces (JSF)Replies: 0Last Post: 03-10-2011, 04:39 AM -
Making Applet tags XHTML 1.0 Strict Compatible
By gehrc in forum New To JavaReplies: 0Last Post: 12-15-2010, 12:26 AM -
Applet making cookies
By mneskovic in forum New To JavaReplies: 18Last Post: 08-31-2010, 04:32 AM -
Making a program out of a Java Applet
By Bomber_Will in forum Java AppletsReplies: 1Last Post: 07-16-2009, 02:54 AM -
Making an Applet use a MySQL Database
By Bomber_Will in forum Java AppletsReplies: 5Last Post: 11-30-2008, 04:28 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks