title: Dynamically adding data into LineChart JavaFX
body: <p>I want to set some values into LineChart but Ive got an error: Duplicate series added, or NPE (I try to <code>clear</code> my series list, calling <code>retainAll</code> but nothing helps) In my controller class Ive stored my <code>chart</code> instance</p>
  
  <pre><code> public static void loadToJSON(LocalDate startDate,LocalDate endDate,Controller controller) {
  
  
  
      Task&lt;Integer&gt; task = new Task&lt;Integer&gt;() {
          @Override protected Integer call() throws Exception {
               double MIN_VAL=Double.MAX_VALUE;
               double MAX_VALUE=Double.MIN_VALUE;
              XYChart.Series&lt;String,Number&gt; series = new  XYChart.Series&lt;String,Number&gt;();
  
  
              for (LocalDate date = startDate;
                   !date.isEqual(endDate.plusDays(1));
                   date = date.plusDays(1)) {
  
                  String formattedDate = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
  
                  URLConnection urlCon = null;
  
                  String baseURL="http://api.fixer.io/";
                  try {
                      URL url = new URL(baseURL+formattedDate+"?symbols=PLN,EUR");
                      urlCon = url.openConnection();
  
                      InputStreamReader in = new InputStreamReader(urlCon.getInputStream());
                      ObjectMapper mapper = new ObjectMapper();
                      ExchangeRate jsonObject = mapper.readValue(in, ExchangeRate.class);
  
                      if(MIN_VAL&gt;jsonObject.getRates().get("PLN"))
                          MIN_VAL=jsonObject.getRates().get("PLN");
                      if(MAX_VALUE&lt;jsonObject.getRates().get("PLN"))
                          MAX_VALUE=jsonObject.getRates().get("PLN");
                      in.close();
  
                        series.getData().clear();
                      series.getData().add(new XYChart.Data&lt;String,Number&gt;(formattedDate,jsonObject.getRates().get("PLN")));
           controller.chart.getData().add(series);
  
                      try {
                          Thread.sleep(200);
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }
  
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          controller.messageBox.setText("Best exchange rate : "+MIN_VAL + " \nWorst exchange rate : "+MAX_VALUE);
              return null;
          }
      };
      Thread th = new Thread(task);
      th.setDaemon(true);
      th.start();
  </code></pre>