Search code examples
javahibernatejfreechart

Issue in JFreeChart and Hibernate


Can someone help me to solve this? I'm trying to create a chart using hibernate database, but I am getting an error in for loop "actual and formal arguments lists differ in length". I try to find this error online, but am not getting any solution. I decided to seek help from here. Any help would be appreciated

Here's the code for my entity class:

class Student1
{
    private int rno,marks1,marks2,marks3;
    private String name;

public Student1() {  }

public Student1(int rno,String name,int marks1,int marks2,int marks3)
{
    this.rno = rno;
    this.name = name;
    this.marks1 = marks1;
    this.marks2 = marks2;
    this.marks3 = marks3;

 //getter and setters
}

Here's the code of method charts:

public static void chartsStudent()
{

    DefaultCategoryDataset d1 = new DefaultCategoryDataset();

    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");

    SessionFactory sfact = cfg.buildSessionFactory();
    Session session = null;

    try
    {
        session = sfact.openSession();
        System.out.println("connected");
        session.beginTransaction();

        List<Student1> student1list = new ArrayList<>();
        student1list = session.createQuery("from Student1").list();
        
        for(Student1 b : student1list)
        {
            d1.setValue(b.getName(),b.getMarks1(),b.getMarks2(),b.getMarks3());
        }
        
    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
    }
    finally
    {
        if (session !=null)
        {
            session.close();
            System.out.println("disconnected");
        }
    }
JFreeChart chart = ChartFactory.createBarChart("Student Preformance",
"Subjects","Marks",d1,PlotOrientation.VERTICAL,true,true,false);
int width = 400;
int height = 400;
}

ERROR IMAGE of an ERROR


Solution

  • As noted here, the DefaultCategoryDataset mutators that add or set a value all take three parameters, not four. Each value is the Number of interest, each rowKey is a series and each columnKey is an element in the series. For example,

    public void setValue(double value, Comparable rowKey, Comparable columnKey)
    

    Based on your Student class, each value is the grade/mark, each series (rowKey) is a student's name, and each element (columnKey) is an assignment/test.

    d1.setValue(b.getMarks1(), b.getName(), "Test1");
    d1.setValue(b.getMarks2(), b.getName(), "Test2");
    …
    

    Alternatively, JDBCCategoryDataset builds a CategoryDataset from a SQL query in one step; the source is an example; more may be found here.