Search code examples
javajfreechart

Variables as Parameter in Chart


I want to make a piechart with JFreeChart and use variables that I got out of an XML-File

Methode Class:

package Statistik;

import xmlModul.XmlLoader;

public class Data {
    XmlLoader xml = new XmlLoader();

    int deleted;
    int created;
    int edited;

    public int getDeleted() {
        return deleted;
    }

    public void setDeleted(int deleted) {
        this.deleted = deleted;
    }

    public int getCreated() {
        return created;
    }

    public void setCreated(int created) {
        this.created = created;
    }

    public int getEdited() {
        return edited;
    }

    public void setEdited(int edited) {
        this.edited = edited;
    }



    public void setDeletedValue(String s) {
       xml.setStringByTagName("Deleted", "Statistik", s);
    }

    public void setCreatedValue(String s) {
        xml.setStringByTagName("Created", "Statistik", s);
    }

    public void setEditedValue(String s) {
        xml.setStringByTagName("Edited", "Statistik", s);
    }

    public int getDeletedValue() {
        return Integer.parseInt(xml.getStringByTagName("Deleted", "Statistik"));
    }

    public int getCreatedValue() {
        return Integer.parseInt(xml.getStringByTagName("Created", "Statistik"));
    }

    public int getEditedValue() {
        return Integer.parseInt(xml.getStringByTagName("Edited", "Statistik"));
    }

    public void setAll(String d, String c, String e){
        xml.setStringByTagName("Deleted", "Statistik", d);
        xml.setStringByTagName("Created", "Statistik", c);
        xml.setStringByTagName("Edited", "Statistik", e);
    }

}

Button call in statistic class:

    DefaultPieDataset pieDataset = new DefaultPieDataset();
    Data data = new Data();

    int created = data.getCreatedValue();
    int deleted = data.getDeletedValue();
    int edited = data.getEditedValue();

    pieDataset.setValue("one", new Integer(15));
    pieDataset.setValue("two", new Integer(40));
    pieDataset.setValue("created", new Integer(created));
    pieDataset.setValue("edited", edited);
    pieDataset.setValue("deleted", deleted);

    JFreeChart chart = ChartFactory.createPieChart("Pie Chart", pieDataset, true, true, true);
    ChartFrame frame = new ChartFrame("Pie Chart", chart);

    frame.setSize(600, 800);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

Before I tried to do it with variables and I only used statements like

pieDataset.setValue("one", new Integer(15));

and it worked. But now that I try to use the int variables deleted, edited and created it doesn't work in any way.

This is my error output:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: " 0 "
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:569)
    at java.lang.Integer.parseInt(Integer.java:615)
    at Statistik.Data.getCreatedValue(Data.java:65)
    at Statistik.StatistikWithFreeChart_USE_THIS.jButton1ActionPerformed(StatistikWithFreeChart_USE_THIS.java:65)
    at Statistik.StatistikWithFreeChart_USE_THIS.access$000(StatistikWithFreeChart_USE_THIS.java:8)
    at Statistik.StatistikWithFreeChart_USE_THIS$1.actionPerformed(StatistikWithFreeChart_USE_THIS.java:26)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6525)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:719)
    at java.awt.EventQueue$4.run(EventQueue.java:717)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Edit: I tried to set the data with

data.setAll("10", "20", "5");

and it worked but when I use this method in another button, it won't take the values out of this so I need to set it in the method but I have to take it out of the xml.


Solution

  • The error message tells you all you need to know: there are blanks in your input strings, so the strings cannot be parsed to integers.

    So, in your functions getDeletedValue(), getCreatedValue() and getEditedValue(), you need to do a trim() which removes these unnecessary blanks. Example:

    public int getCreatedValue() {
        return Integer.parseInt(xml.getStringByTagName("Created", "Statistik").trim());
    }