0

I am trying to display the value of percentage in a pie chart, for that I have the percentage value in the Personalityclass and the Chart Class is where I need to show it. My code structure is below:

Personality.JAVA

   Tree C = gson.fromJson(profile.toString(), Tree.class);  

                Tree.SubTree t = C.getTree(); 
                ArrayList<Children> mc = t.getChildren();
                Children c1 = mc.get(0);

                ArrayList<Children1> c2 = c1.getChildren();
                Children1 obj1 = c2.get(0);

                private Children1 obj1; //ERROR-ILEGAL START OF EXPRESSION 


                public Children1 getObj1(){
                return this.obj1;
               } 

         /**       ArrayList<child2> c3 = obj1.children;
                child2 obj2 = c3.get(0);

                ArrayList<child3> c4 = obj2.children;
                child3 obj3 = c4.get(0);  **/


                System.out.println(obj1.getPercentage()); //ERROR - IDENTIFIER EXPECTED

Children1.JAVA

   public class Children1 {
     private String category;
     private String id;
     private String name;
     private double percentage;
     private double sampling_error;
     private ArrayList<Children2> children;

    public Double getPercentage() {
     return percentage;
   }
/**
* 
* @param percentage
* The percentage
*/
    public void setPercentage(Double percentage) {
    this.percentage = percentage;
    }
}

Tree.JAVA(Not sure if this class is necessary but here it is)

public class Tree {
    private SubTree tree;  
    @Override
    public String toString() {
        return "ID: " + id + "\n" + "Name: "+ "\n" + tree+ "\n" ;
    }  
    class SubTree{
    private String id;
    private String name;
    private ArrayList<Children> children; //Gets Arraylist from Class - Children


    public ArrayList<Children> getChildren() {
    return children;
    }

    public void setChildren(ArrayList<Children> children) {
    this.children = children;
    }     
    }

   public SubTree getTree() {
     return tree;
   }

    public void setTree() {
    this.tree = tree;
    }

}

CHART CLASS

private PieDataset createDataset(){
   Personality m = new Personality();
  // Personality.class = Personality();
    DefaultPieDataset result = new DefaultPieDataset();
    result.setValue("Value1",m.obj1 ); //attempted to get obj1 from Persoanlity Class
    result.setValue("Linux", 10);
    result.setValue("Mac", 25);
    return result;
}

So overall program is - get normal paragraph convert to json then to parse to java after using java values produce graph. So in this case get percentage value obj1 to show in Chart class.

Hope you guys understood the question and thankyou for your time :)

Alia Su
  • 45
  • 1
  • 2
  • 9

2 Answers2

1

You have to make the Children1 object visible from the Personality class. At the moment it looks like obj1 is just a local variable and its scope is only visible to that local method

If in Personality, you have a field Children1 obj1 then this could be set by the local method or constructor. If the scope of Children1 obj1 was public OR preferably it had a getter method, then you could read it from the Chart class assuming that the Children1 obj1 is created in the constructor i.e. Personality m = new Personality();

Assuming that your Personality class looks something like

public Personailty {
   ....
}

then you will to add, so that it looks like

public Personailty {
   ....
   private Children1 obj1;

   public Children1 getObj1(){
      return this.obj1;
  } 
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • Thanks for the answer! however I am unsure of how to make the scope public I have attempted to make a getter for Children1 and return as obj1 but it doesn't work I think I am doing it wrong. – Alia Su Nov 25 '16 at 01:01
  • I have updated my answer for you. Please also edit your question to show where the `Personality` code is e.g. Constructor, local method – Scary Wombat Nov 25 '16 at 01:06
  • *Children1 is an arraylist?* **Children1 is a class, see your question** – Scary Wombat Nov 25 '16 at 01:12
  • Sorry my bad its late over here and I am tired. The code is updated I have commented the parts which sows errors. – Alia Su Nov 25 '16 at 01:21
  • Either you are extremely tired or you have bitten off more than you can chew, or my instructions are crap. My edited code shows that you **need** to add `Children1 obj1` as a class field and create a `getter` method. If you can not do this you need to get some more basic help. – Scary Wombat Nov 25 '16 at 01:34
  • Thanks your way did work , I just have to convert an arraylist to a string now to get it showing in the chart.. – Alia Su Nov 25 '16 at 12:45
0

Possible solution below if you have attribute called obj1 in Personality class.

Personality Class should have:

private Children1 obj1;

public Children1 getObj1(){
    return this.obj1;
} 

.

Change this your Code

ArrayList<Children1> c2 = c1.getChildren();
Children1 obj1 = c2.get(0);

to

ArrayList<Children1> c2 = c1.getChildren();
this.obj1 = c2.get(0);

And on your Chart class

DefaultPieDataset result = new DefaultPieDataset();
result.setValue("Value1",m.getObj1()); //attempted to get obj1 from Persoanlity Class
Luminous_Dev
  • 614
  • 6
  • 14