1

I am showing an error in my main method for this statement: // non-static variable this cannot be referenced from a static context

frame.getContentPane().add(new PieChart()); 

I thought this would be as easy as loading a content pane and adding the PieChart class to it. I spent several hours today and was hoping I could get help with this issue. I have 10 weeks experience with Java and haven't wandered out of my depth until now. Any advice is greatly appreciated.

Here is my PieChart program:


package iapiechart;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
import javax.swing.JFrame;

class IAPieChart{

    double arcValue;        // passes a value for the calculation of the arc.
    Color marker;            // holds value for color (expressed as an integer 

    public IAPieChart(double value, Color color){

        this.arcValue = value;
        this.marker = color;
    }


    public class PieChart extends JComponent { 

        IAPieChart[] pieValue = {new IAPieChart(5, Color.green),
                                new IAPieChart(33, Color.orange),
                                new IAPieChart(20, Color.blue),
                                new IAPieChart(15, Color.red)

        };

        public void paint(Graphics g) {

            drawPie((Graphics2D) g, getBounds(),  pieValue);

        }

        void drawPie(Graphics2D g, Rectangle area, IAPieChart[] pieValue){

            double sum = 0.0D;
            for (int i = 0; i < pieValue.length; i++) {

                sum += pieValue[i].arcValue;
            }

            double endPoint =  0.0D;
            int arcStart = 0; 
            for (int i = 0; i < pieValue.length; i++){

                endPoint = (int) (endPoint * 360 / sum);
                int radius = (int) (pieValue[i].arcValue * 360/ sum);
                g.setColor(pieValue[i].marker);
                g.fillArc(area.x, area.y, area.width, area.height, arcStart, radius);
                radius += pieValue[i].arcValue;
            }

        }
    }
     public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.getContentPane().add(new PieChart()); // This is where the error occurs. 
        frame.setSize(500, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        }
 }
syb0rg
  • 8,057
  • 9
  • 41
  • 81
  • To those attempting to close this question as a duplicate: This question is about making the inner class static, and not a method static, so this is not a duplicate. – rgettman Apr 13 '13 at 00:35

3 Answers3

3

From a static method main you are attempting to instantiate an inner class PieChart that is not static -- declare it static.

public static class PieChart extends JComponent { 

If you were to keep PieChart non-static, then you would need an instance of IAPieChart to create an instance of PieChart, and you don't have an instance of IAPieChart in main.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • rgettman: I owe you a case of your favorite beverage. I spent 9 hours on this one issue. Just thanking you wouldn't be enough. I will share this with others, my friend. –  Apr 12 '13 at 23:56
  • Thanks again Gettman. You guys were a real help last week. This isn't complete but it did meet the requirements and then some. I have a basis and a good understanding of JApplet thanks to your help. http://www.apexsouthwest.com/PieChart/ –  Apr 18 '13 at 15:42
0

Since you create the IaPieChart in the PieChart class your code should be something like:

public class PieChart extend JComponent
{

    static class IaPieChart(..)
    {
    }
}

That is your IaPieChart class is really a helper class for the PieChart class so it should be defined within that class, not the other way around.

Also, custom painting is done by overriding the paintComponent() method, not the paint() method.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

You can't access non-static stuff from a static member, as discussed a couple hours ago.-

"non static method cannot be referenced from a static context" JPA Java

Community
  • 1
  • 1
ssantos
  • 16,001
  • 7
  • 50
  • 70