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. :o
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?
Re: Issues with Static environment
Sure thing.
Here's the Charts class:
Code :
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!!
Re: Issues with Static environment
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.
Code Java:
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]
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!
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.
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!