title: JavaFX, PieChart - How to add Data
body: <p>I am trying to add data to my PieChart. I was writing the code like this <a href="https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/pie-chart.htm#CIHFDADD" rel="nofollow noreferrer">tutorial</a>, but when I am trying to add variable implemented before it is not working. What's wrong ?</p>
  
  <pre><code>import java.net.URL;
  import java.util.ResourceBundle;
  
  import javafx.collections.FXCollections;
  import javafx.collections.ObservableList;
  import javafx.event.EventHandler;
  import javafx.fxml.FXML;
  import javafx.fxml.Initializable;
  import javafx.geometry.Side;
  import javafx.scene.chart.PieChart;
  import javafx.scene.control.Button;
  import javafx.scene.control.Label;
  import javafx.scene.input.MouseEvent;
  import javafx.scene.layout.StackPane;
  import javafx.scene.paint.Color;
  
  public class HomeScreenController implements Initializable{
  
  
      //Counter dreams "straszny"
      public String Straszny = "Straszny";
      public double Straszny_Counter = 0;
  
      //Counter dreams "smieszny"
      public String Smieszny = "Śmieszny";
      public double Smieszny_Counter = 0;
  
      //Counter dreams "dziwny"
      public String Dziwny = "Dziwny";
      public double Dziwny_Counter = 0;
  
      //Counter dreams "SF"
      public String SF = "SF";
      public double SF_Counter = 0;
  
      //Counter dreams "straszny"
      public String Inny = "Inny";
      public double Inny_Counter = 0;
  
      @FXML
      private PieChart sleepTypes;
  
      ObservableList&lt;PieChart.Data&gt; pieChartData =
              FXCollections.observableArrayList(
              new PieChart.Data(Smieszny, Smieszny_Counter),
              new PieChart.Data(Straszny, Straszny_Counter),
              new PieChart.Data(Dziwny, Dziwny_Counter),
              new PieChart.Data(SF, SF_Counter),
              new PieChart.Data(Inny, Inny_Counter));
  
      @Override
      public void initialize(URL location, ResourceBundle resources) {
  
          for(int i = 0;i&lt;ShowDreamHistoryController.listDreams.size();i++)
          {
              //Check how much "strasznych" dreams is
              if(Straszny.equals(ShowDreamHistoryController.listDreams.get(i).getType()))
              {
                  Straszny_Counter++;
              }
              //Check how much "smiesznych" dreams is
              else if(Smieszny.equals(ShowDreamHistoryController.listDreams.get(i).getType()))
              {
                  Smieszny_Counter++;
              }
              //Check how much "dziwny" dreams is
              else if(Dziwny.equals(ShowDreamHistoryController.listDreams.get(i).getType()))
              {
                  Dziwny_Counter++;
              }
              //Check how much "SF" dreams is
              else if(SF.equals(ShowDreamHistoryController.listDreams.get(i).getType()))
              {
                  SF_Counter++;
              }
              //Check how much "inny" dreams is
              else if(Inny.equals(ShowDreamHistoryController.listDreams.get(i).getType()))
              {
                  Inny_Counter++;
              }
          }//for
  
          System.out.println("Straszych: "+Straszny_Counter);
          System.out.println("Smiesznych: "+Smieszny_Counter);
          System.out.println("Dziwny: "+Dziwny_Counter);
          System.out.println("SF: "+SF_Counter);
          System.out.println("Inny: "+Inny_Counter);
  
          //Add Data to the chart
          sleepTypes.setData(pieChartData);
      }//init
  }//class
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/DgCh1.png" alt="Here, what it looks like after compilation"></p>
  
  <p>The code is working when it is like that:</p>
  
  <pre><code>    ObservableList&lt;PieChart.Data&gt; pieChartData =
              FXCollections.observableArrayList(
              new PieChart.Data(Smieszny, 1),
              new PieChart.Data(Straszny, 2),
              new PieChart.Data(Dziwny, 5),
              new PieChart.Data(SF, 1),
              new PieChart.Data(Inny, 1));
  </code></pre>
  
  <p>But I don't want that one. Any ideas what is wrong with the first code?</p>