1

I'm just trying to run a demo of a 'simple' pie chart I found online. I'm running this in Eclipse Kepler and I keep getting,

Exception in thread "main" java.lang.NullPointerException
    at org.apache.fontbox.afm.AFMParser.main(AFMParser.java:304)

when I try to run the program. I would think, considering where I got the code (linked off of a JChart site), that it would run without issue. Just trying to see if anyone can see something I can't.

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;

public class BarChartDemo {
    public static void main(String[] args){
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("Category 1", 50.0);
        dataset.setValue("Category 2", 50.0);
        JFreeChart chart = ChartFactory.createPieChart(
            "Sample Pie Chart",
            dataset,
            true,
            true,
            false
            );

        ChartFrame frame = new ChartFrame("First",chart);
        frame.pack();
        frame.setVisible(true);
    }
}
Matt
  • 3,508
  • 6
  • 38
  • 66
  • 1
    Did you write the class AFMParser? I assume not, but that exception tells you the NullPointer is in line 304 in AFMParser, which would be pretty silly if it's published, finalized code. – snickers10m May 23 '14 at 01:12
  • No, I didn't think I had to do anything other than just test it out. – Matt May 23 '14 at 01:59
  • The best advice I can give right now is just to redownload the APIs. I know it seems ridiculous but if there's something wrong with THEIR code the only way I would fix it is that it might be corrupted. Otherwise, good luck I guess... sorry – snickers10m May 23 '14 at 02:30

2 Answers2

1

I was able to get the chart to appear without changing your code. Did you include both jars (jfreechart, jcommons) in your build path?

Edwin Torres
  • 2,774
  • 1
  • 13
  • 15
  • Yeah, those are both in my path. I also tried to run another class (JFreeChart example) that was working on another computer, and it also fails to run giving me the same exception. – Matt May 23 '14 at 02:09
  • Weird. I found this about AFMParser: "This class is used to parse AFM(Adobe Font Metrics) documents". I'm not sure what this has to do with JFreeChart or anything in your example. – Edwin Torres May 23 '14 at 02:15
0

After some battling and researching (guessing), I came across this class via some Adobe Font Metrics googling. Incidentally I can't remember exactly where I found it, but it solved my issue. I'll spare the SO servers and not post the 1000(+)-line code, but here it is as a gist if anyone has the same problem. I just included it in my project source file as a separate class, and voila...

I'm still not sure why my project wouldn't run without this. It worked on other systems, but not my Eclipse Kepler (Mac OS 10.9). If anyone can tell me exactly why it kept failing, I'd be very thankful.

Matt
  • 3,508
  • 6
  • 38
  • 66