简体   繁体   中英

How to transfer object from one class to another class - JAVA

I am trying to display the value of percentage in a pie chart, for that I have the percentage value in the Personality class 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 :)

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 ie 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;
  } 
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM