Results 1 to 6 of 6
  1. #1
    jeskoston is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default 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();
    		}
    	}
    
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,330
    Rep Power
    25

    Default

    java.lang.NullPointerException exception in my paint function
    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.
    Last edited by Norm; 05-14-2011 at 12:01 AM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jeskoston View Post
    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.

  4. #4
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by Fubarable View Post
    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

  5. #5
    jeskoston is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    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

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,330
    Rep Power
    25

    Default

    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.

Similar Threads

  1. Issue regarding making chart in jsf
    By maulikmodi08 in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 03-10-2011, 04:39 AM
  2. Making Applet tags XHTML 1.0 Strict Compatible
    By gehrc in forum New To Java
    Replies: 0
    Last Post: 12-15-2010, 12:26 AM
  3. Applet making cookies
    By mneskovic in forum New To Java
    Replies: 18
    Last Post: 08-31-2010, 04:32 AM
  4. Making a program out of a Java Applet
    By Bomber_Will in forum Java Applets
    Replies: 1
    Last Post: 07-16-2009, 02:54 AM
  5. Making an Applet use a MySQL Database
    By Bomber_Will in forum Java Applets
    Replies: 5
    Last Post: 11-30-2008, 04:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •