Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Issues with Static environment

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Issues with Static environment

    First and foremost, I know that the error "non-static variable 'whatever' cannot be referenced from a static context" has been brought up often, but I think my situation is a little weird.

    I have two classes: one called Existing that reads from a file and computes several values, and one called Charts which uses iText to make PDF charts. Charts uses variables from Existing in its process, or, rather, I'm trying to let that be the case.

    When I use variables from Existing in my Charts class, I get the "non-static variable in a static environment" error, as expected. I tried removing the static keyword from the method declaration, but that inhibits the production of charts. I tried making another instance of the Existing class and using the object to reference the variables, but the values come out to zero for whatever reason. I've tried making a reference to the Existing class within the generateBarChart() method, but that same method is applied later on, so it can't accept that class.

    My question is, are there any other ways to reference the variables in Existing in a static manner?

    Thanks in advance and my apologies for the verbosity of my post.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Issues with Static environment

    Please see this post: Common Java Mistakes: Referencing Non-static variables/methods in a static way

    edit: nvm, didn't read through your post thouroughly. Could you post your code so we can see what's the problem?

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Issues with Static environment

    Sure thing.

    Here's the Charts class:


    package gradebook;
     
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;
    import java.io.FileOutputStream;
    import org.jfree.chart.ChartFactory; 
    import org.jfree.chart.JFreeChart; 
    import org.jfree.chart.plot.PlotOrientation; 
    import org.jfree.data.category.DefaultCategoryDataset; 
    import com.lowagie.text.*;
    import com.lowagie.text.pdf.*;
    import org.jfree.data.general.DefaultPieDataset;
     
    public class Charts{
     
        /** Creates a new instance of Charts */
        public static JFreeChart generateBarChart() {
            DefaultCategoryDataset dataset = new DefaultCategoryDataset();
            dataset.setValue(75, "Average", "Grade One");
            dataset.setValue(50, "Average", "Grade Two");
            dataset.setValue(50, "Average", "Grade Three");
            dataset.setValue(50, "Average", "Grade Four");
            dataset.setValue(50, "Average", "Grade Five");
     
            JFreeChart chart = ChartFactory.createBarChart(
                    "Grade Averages", "Grades", "Percent Average", 
                    dataset, PlotOrientation.VERTICAL, false, true, false);
     
            return chart;
     
     
        }
        public static void chartWriter(){
            String desktopPath = System.getProperty("user.home") + "//Desktop//barchart.pdf";
     
            writeChartToPDF(generateBarChart(), 700, 400, desktopPath);
        }
        public static void writeChartToPDF(JFreeChart chart, int width, int height, String filename){ 
                PdfWriter writer = null;
     
                Document document = new Document();
     
                try {
                    writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
                    document.open();
                    PdfContentByte contentByte = writer.getDirectContent();
                    PdfTemplate template = contentByte.createTemplate(width, height);
                    Graphics2D graphics2D = template.createGraphics(width, height, new DefaultFontMapper());
                    Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, width, height);
     
                    chart.draw(graphics2D, rectangle2D);
     
                    graphics2D.dispose();
                    contentByte.addTemplate(template, 0, 0);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                document.close();
                JOptionPane.showMessageDialog(null, "A PDF file has been created.");
     
        }
     
     
     
     
     
     
    }


    The values in question are those at the start of the first method. I know that they're values (75, 50, ...), but I want them to be the variables from the Existing class.

    Thanks in advance!!

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Issues with Static environment

    Any suggestions?

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Issues with Static environment

    So you want to be able to take in 5 values and create a bar chart?

    Pass them as a parameter to the generateBarChart method.

        public static JFreeChart generateBarChart(int[] data) {
            DefaultCategoryDataset dataset = new DefaultCategoryDataset();
            dataset.setValue(data[0], "Average", "Grade One"); // this could actually be automated better using a loop
            dataset.setValue(data[1], "Average", "Grade Two");
            dataset.setValue(data[2], "Average", "Grade Three");
            dataset.setValue(data[3], "Average", "Grade Four");
            dataset.setValue(data[4], "Average", "Grade Five");
     
            JFreeChart chart = ChartFactory.createBarChart(
                    "Grade Averages", "Grades", "Percent Average", 
                    dataset, PlotOrientation.VERTICAL, false, true, false);
     
            return chart;
     
     
        }

    Then, to use this:
    [highlight=Java]public static void main(String[] args)
    {
    int[] grades = new int[]{75, 50, 50, 50, 50};
    JFReeChart chart = generateBarChart(grades);
    }
    [/highlight=Java]

  6. #6
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Issues with Static environment

    Thanks for the reply!

    If I pass the data array parameter into the generateBarChart() method, I still can't use the writeChartToPDF() method later on in the class because it has to accept the parameters given.

    But using an array is a good idea... I'll play around with that!

    Thanks again!

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Issues with Static environment

    I'm still not entirely sure what it is you want. You need to make sure all your parameters get passed into the methods that need them if you want your methods to perform tasks on that data. A better way is to create an object which contains all of those values and then pass that object to your writeChartToPDF method.

  8. #8
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Issues with Static environment

    I want to use variables from one class in the static method of another class.
    I'll try passing an object created from the Existing class in the WritetoChart method.

    Thanks!

Similar Threads

  1. Environment issue with Bonjour API
    By DarkRoast in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 29th, 2010, 07:02 PM
  2. non-static method cannot be referenced from a static context
    By Kaltonse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2010, 06:51 PM
  3. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM
  4. Static to non-static - Organization
    By copeg in forum Object Oriented Programming
    Replies: 5
    Last Post: December 22nd, 2009, 12:56 PM
  5. Replies: 1
    Last Post: July 10th, 2008, 05:03 AM