Optional cannot be converted to Int (for use on GUI Progress Bar)












0















I am receiving below error, when I try to set value on a JProgressBar.




"Optional cannot be converted to Int"




Could someone please advise any workarounds/Solution??



public GUI(){
initComponents();
tL = new TasksToDo();
jProgressBar1.setValue(tL.retrieveTotalHours());// [Where my error occurs]}
}


And from the TaskToDo Class, Originally I set this to ArrayList but the warnings said needed to switch to Optional:



  public class TasksToDo {

public static ArrayList<Task> taskList;

public TasksToDo(){
taskList = new ArrayList<Task>();
taskList.add(new Task(0,"Whitepaper", "Write first draft of Whitepaper", 7));
taskList.add(new Task(1,"Create Database Structure", "Plan required fields and tables", 1));
taskList.add(new Task(2,"Setup ODBC Connections", "Create the ODBC Connections between SVR1 to DEV-SVR", 2));

}

public void addTask (int taskId, String taskTitle, String taskDescription, int taskHours){}

public ArrayList<Task> retrieveTask(){
return taskList;
}

public Optional<Integer> retrieveTotalHours(){
return taskList.stream().map(e -> e.getTaskHours()).reduce(Integer::sum);
}
}









share|improve this question





























    0















    I am receiving below error, when I try to set value on a JProgressBar.




    "Optional cannot be converted to Int"




    Could someone please advise any workarounds/Solution??



    public GUI(){
    initComponents();
    tL = new TasksToDo();
    jProgressBar1.setValue(tL.retrieveTotalHours());// [Where my error occurs]}
    }


    And from the TaskToDo Class, Originally I set this to ArrayList but the warnings said needed to switch to Optional:



      public class TasksToDo {

    public static ArrayList<Task> taskList;

    public TasksToDo(){
    taskList = new ArrayList<Task>();
    taskList.add(new Task(0,"Whitepaper", "Write first draft of Whitepaper", 7));
    taskList.add(new Task(1,"Create Database Structure", "Plan required fields and tables", 1));
    taskList.add(new Task(2,"Setup ODBC Connections", "Create the ODBC Connections between SVR1 to DEV-SVR", 2));

    }

    public void addTask (int taskId, String taskTitle, String taskDescription, int taskHours){}

    public ArrayList<Task> retrieveTask(){
    return taskList;
    }

    public Optional<Integer> retrieveTotalHours(){
    return taskList.stream().map(e -> e.getTaskHours()).reduce(Integer::sum);
    }
    }









    share|improve this question



























      0












      0








      0








      I am receiving below error, when I try to set value on a JProgressBar.




      "Optional cannot be converted to Int"




      Could someone please advise any workarounds/Solution??



      public GUI(){
      initComponents();
      tL = new TasksToDo();
      jProgressBar1.setValue(tL.retrieveTotalHours());// [Where my error occurs]}
      }


      And from the TaskToDo Class, Originally I set this to ArrayList but the warnings said needed to switch to Optional:



        public class TasksToDo {

      public static ArrayList<Task> taskList;

      public TasksToDo(){
      taskList = new ArrayList<Task>();
      taskList.add(new Task(0,"Whitepaper", "Write first draft of Whitepaper", 7));
      taskList.add(new Task(1,"Create Database Structure", "Plan required fields and tables", 1));
      taskList.add(new Task(2,"Setup ODBC Connections", "Create the ODBC Connections between SVR1 to DEV-SVR", 2));

      }

      public void addTask (int taskId, String taskTitle, String taskDescription, int taskHours){}

      public ArrayList<Task> retrieveTask(){
      return taskList;
      }

      public Optional<Integer> retrieveTotalHours(){
      return taskList.stream().map(e -> e.getTaskHours()).reduce(Integer::sum);
      }
      }









      share|improve this question
















      I am receiving below error, when I try to set value on a JProgressBar.




      "Optional cannot be converted to Int"




      Could someone please advise any workarounds/Solution??



      public GUI(){
      initComponents();
      tL = new TasksToDo();
      jProgressBar1.setValue(tL.retrieveTotalHours());// [Where my error occurs]}
      }


      And from the TaskToDo Class, Originally I set this to ArrayList but the warnings said needed to switch to Optional:



        public class TasksToDo {

      public static ArrayList<Task> taskList;

      public TasksToDo(){
      taskList = new ArrayList<Task>();
      taskList.add(new Task(0,"Whitepaper", "Write first draft of Whitepaper", 7));
      taskList.add(new Task(1,"Create Database Structure", "Plan required fields and tables", 1));
      taskList.add(new Task(2,"Setup ODBC Connections", "Create the ODBC Connections between SVR1 to DEV-SVR", 2));

      }

      public void addTask (int taskId, String taskTitle, String taskDescription, int taskHours){}

      public ArrayList<Task> retrieveTask(){
      return taskList;
      }

      public Optional<Integer> retrieveTotalHours(){
      return taskList.stream().map(e -> e.getTaskHours()).reduce(Integer::sum);
      }
      }






      java netbeans stream optional jprogressbar






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 10:53









      Suryavel TR

      2,46311420




      2,46311420










      asked Nov 21 '18 at 9:16









      craig157craig157

      155




      155
























          4 Answers
          4






          active

          oldest

          votes


















          1














          You have to unwrap the optional and grab the value in it like this. Otherwise you can't assign an Optional where int is needed.



          tL.retrieveTotalHours().orElse(0);





          share|improve this answer



















          • 1





            That's worked perfectly! Thanks Ravindra

            – craig157
            Nov 21 '18 at 9:41



















          2














          An Optional means that the value need not be there. It is basically there to force the caller to explicitly decide what to when a value does not exist. In your case, you can specifify a default value:



          jProgressBar1.setValue(tL.retrieveTotalHours().orElse(0));


          However, your retrieveTotalHours method probably should not return an Optional in the first place. Stream.reduce returns Optional.empty() when the stream is empty, but in your case it probably should return 0 when the list of tasks is empty. So you can do:



          public int retrieveTotalHours(){
          return taskList.stream().map(e -> e.getTaskHours()).reduce(0, Integer::sum);
          }


          (The 0 argument is the identity, which is returned when the stream is empty.)



          or even:



          public int retrieveTotalHours(){
          return taskList.stream().mapToInt(e -> e.getTaskHours()).sum();
          }





          share|improve this answer
























          • Thank you for breaking it down Hoopje

            – craig157
            Nov 21 '18 at 10:38



















          1














          Well, basically, an Optional<Integer> is not assignment compatible with int.



          But Integer is (after unboxing) ... so change:



          jProgressBar1.setValue(tL.retrieveTotalHours());


          to



          jProgressBar1.setValue(tL.retrieveTotalHours().orElse(0));


          Note that you must provide an integer value when you call setValue. Null or "nothing" is not acceptable.






          share|improve this answer
























          • Thanks Stephen - the orElse(0) did the trick! And yes this field must have a value over 0.1

            – craig157
            Nov 21 '18 at 9:42



















          0














          If you are only interested in the sum of hours, you don't need the Optional and can make it simpler:



          public int retrieveTotalHours()
          {
          return taskList.stream().mapToInt(e -> e.getTaskHours()).sum();
          }





          share|improve this answer
























          • Thanks Roger - I will be doing this on another field so will try your method too

            – craig157
            Nov 21 '18 at 9:44











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408681%2foptional-integer-cannot-be-converted-to-int-for-use-on-gui-progress-bar%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You have to unwrap the optional and grab the value in it like this. Otherwise you can't assign an Optional where int is needed.



          tL.retrieveTotalHours().orElse(0);





          share|improve this answer



















          • 1





            That's worked perfectly! Thanks Ravindra

            – craig157
            Nov 21 '18 at 9:41
















          1














          You have to unwrap the optional and grab the value in it like this. Otherwise you can't assign an Optional where int is needed.



          tL.retrieveTotalHours().orElse(0);





          share|improve this answer



















          • 1





            That's worked perfectly! Thanks Ravindra

            – craig157
            Nov 21 '18 at 9:41














          1












          1








          1







          You have to unwrap the optional and grab the value in it like this. Otherwise you can't assign an Optional where int is needed.



          tL.retrieveTotalHours().orElse(0);





          share|improve this answer













          You have to unwrap the optional and grab the value in it like this. Otherwise you can't assign an Optional where int is needed.



          tL.retrieveTotalHours().orElse(0);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 9:21









          Ravindra RanwalaRavindra Ranwala

          9,34731636




          9,34731636








          • 1





            That's worked perfectly! Thanks Ravindra

            – craig157
            Nov 21 '18 at 9:41














          • 1





            That's worked perfectly! Thanks Ravindra

            – craig157
            Nov 21 '18 at 9:41








          1




          1





          That's worked perfectly! Thanks Ravindra

          – craig157
          Nov 21 '18 at 9:41





          That's worked perfectly! Thanks Ravindra

          – craig157
          Nov 21 '18 at 9:41













          2














          An Optional means that the value need not be there. It is basically there to force the caller to explicitly decide what to when a value does not exist. In your case, you can specifify a default value:



          jProgressBar1.setValue(tL.retrieveTotalHours().orElse(0));


          However, your retrieveTotalHours method probably should not return an Optional in the first place. Stream.reduce returns Optional.empty() when the stream is empty, but in your case it probably should return 0 when the list of tasks is empty. So you can do:



          public int retrieveTotalHours(){
          return taskList.stream().map(e -> e.getTaskHours()).reduce(0, Integer::sum);
          }


          (The 0 argument is the identity, which is returned when the stream is empty.)



          or even:



          public int retrieveTotalHours(){
          return taskList.stream().mapToInt(e -> e.getTaskHours()).sum();
          }





          share|improve this answer
























          • Thank you for breaking it down Hoopje

            – craig157
            Nov 21 '18 at 10:38
















          2














          An Optional means that the value need not be there. It is basically there to force the caller to explicitly decide what to when a value does not exist. In your case, you can specifify a default value:



          jProgressBar1.setValue(tL.retrieveTotalHours().orElse(0));


          However, your retrieveTotalHours method probably should not return an Optional in the first place. Stream.reduce returns Optional.empty() when the stream is empty, but in your case it probably should return 0 when the list of tasks is empty. So you can do:



          public int retrieveTotalHours(){
          return taskList.stream().map(e -> e.getTaskHours()).reduce(0, Integer::sum);
          }


          (The 0 argument is the identity, which is returned when the stream is empty.)



          or even:



          public int retrieveTotalHours(){
          return taskList.stream().mapToInt(e -> e.getTaskHours()).sum();
          }





          share|improve this answer
























          • Thank you for breaking it down Hoopje

            – craig157
            Nov 21 '18 at 10:38














          2












          2








          2







          An Optional means that the value need not be there. It is basically there to force the caller to explicitly decide what to when a value does not exist. In your case, you can specifify a default value:



          jProgressBar1.setValue(tL.retrieveTotalHours().orElse(0));


          However, your retrieveTotalHours method probably should not return an Optional in the first place. Stream.reduce returns Optional.empty() when the stream is empty, but in your case it probably should return 0 when the list of tasks is empty. So you can do:



          public int retrieveTotalHours(){
          return taskList.stream().map(e -> e.getTaskHours()).reduce(0, Integer::sum);
          }


          (The 0 argument is the identity, which is returned when the stream is empty.)



          or even:



          public int retrieveTotalHours(){
          return taskList.stream().mapToInt(e -> e.getTaskHours()).sum();
          }





          share|improve this answer













          An Optional means that the value need not be there. It is basically there to force the caller to explicitly decide what to when a value does not exist. In your case, you can specifify a default value:



          jProgressBar1.setValue(tL.retrieveTotalHours().orElse(0));


          However, your retrieveTotalHours method probably should not return an Optional in the first place. Stream.reduce returns Optional.empty() when the stream is empty, but in your case it probably should return 0 when the list of tasks is empty. So you can do:



          public int retrieveTotalHours(){
          return taskList.stream().map(e -> e.getTaskHours()).reduce(0, Integer::sum);
          }


          (The 0 argument is the identity, which is returned when the stream is empty.)



          or even:



          public int retrieveTotalHours(){
          return taskList.stream().mapToInt(e -> e.getTaskHours()).sum();
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 9:39









          HoopjeHoopje

          10k52544




          10k52544













          • Thank you for breaking it down Hoopje

            – craig157
            Nov 21 '18 at 10:38



















          • Thank you for breaking it down Hoopje

            – craig157
            Nov 21 '18 at 10:38

















          Thank you for breaking it down Hoopje

          – craig157
          Nov 21 '18 at 10:38





          Thank you for breaking it down Hoopje

          – craig157
          Nov 21 '18 at 10:38











          1














          Well, basically, an Optional<Integer> is not assignment compatible with int.



          But Integer is (after unboxing) ... so change:



          jProgressBar1.setValue(tL.retrieveTotalHours());


          to



          jProgressBar1.setValue(tL.retrieveTotalHours().orElse(0));


          Note that you must provide an integer value when you call setValue. Null or "nothing" is not acceptable.






          share|improve this answer
























          • Thanks Stephen - the orElse(0) did the trick! And yes this field must have a value over 0.1

            – craig157
            Nov 21 '18 at 9:42
















          1














          Well, basically, an Optional<Integer> is not assignment compatible with int.



          But Integer is (after unboxing) ... so change:



          jProgressBar1.setValue(tL.retrieveTotalHours());


          to



          jProgressBar1.setValue(tL.retrieveTotalHours().orElse(0));


          Note that you must provide an integer value when you call setValue. Null or "nothing" is not acceptable.






          share|improve this answer
























          • Thanks Stephen - the orElse(0) did the trick! And yes this field must have a value over 0.1

            – craig157
            Nov 21 '18 at 9:42














          1












          1








          1







          Well, basically, an Optional<Integer> is not assignment compatible with int.



          But Integer is (after unboxing) ... so change:



          jProgressBar1.setValue(tL.retrieveTotalHours());


          to



          jProgressBar1.setValue(tL.retrieveTotalHours().orElse(0));


          Note that you must provide an integer value when you call setValue. Null or "nothing" is not acceptable.






          share|improve this answer













          Well, basically, an Optional<Integer> is not assignment compatible with int.



          But Integer is (after unboxing) ... so change:



          jProgressBar1.setValue(tL.retrieveTotalHours());


          to



          jProgressBar1.setValue(tL.retrieveTotalHours().orElse(0));


          Note that you must provide an integer value when you call setValue. Null or "nothing" is not acceptable.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 9:24









          Stephen CStephen C

          518k70572928




          518k70572928













          • Thanks Stephen - the orElse(0) did the trick! And yes this field must have a value over 0.1

            – craig157
            Nov 21 '18 at 9:42



















          • Thanks Stephen - the orElse(0) did the trick! And yes this field must have a value over 0.1

            – craig157
            Nov 21 '18 at 9:42

















          Thanks Stephen - the orElse(0) did the trick! And yes this field must have a value over 0.1

          – craig157
          Nov 21 '18 at 9:42





          Thanks Stephen - the orElse(0) did the trick! And yes this field must have a value over 0.1

          – craig157
          Nov 21 '18 at 9:42











          0














          If you are only interested in the sum of hours, you don't need the Optional and can make it simpler:



          public int retrieveTotalHours()
          {
          return taskList.stream().mapToInt(e -> e.getTaskHours()).sum();
          }





          share|improve this answer
























          • Thanks Roger - I will be doing this on another field so will try your method too

            – craig157
            Nov 21 '18 at 9:44
















          0














          If you are only interested in the sum of hours, you don't need the Optional and can make it simpler:



          public int retrieveTotalHours()
          {
          return taskList.stream().mapToInt(e -> e.getTaskHours()).sum();
          }





          share|improve this answer
























          • Thanks Roger - I will be doing this on another field so will try your method too

            – craig157
            Nov 21 '18 at 9:44














          0












          0








          0







          If you are only interested in the sum of hours, you don't need the Optional and can make it simpler:



          public int retrieveTotalHours()
          {
          return taskList.stream().mapToInt(e -> e.getTaskHours()).sum();
          }





          share|improve this answer













          If you are only interested in the sum of hours, you don't need the Optional and can make it simpler:



          public int retrieveTotalHours()
          {
          return taskList.stream().mapToInt(e -> e.getTaskHours()).sum();
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 9:40









          rogerklrogerkl

          312




          312













          • Thanks Roger - I will be doing this on another field so will try your method too

            – craig157
            Nov 21 '18 at 9:44



















          • Thanks Roger - I will be doing this on another field so will try your method too

            – craig157
            Nov 21 '18 at 9:44

















          Thanks Roger - I will be doing this on another field so will try your method too

          – craig157
          Nov 21 '18 at 9:44





          Thanks Roger - I will be doing this on another field so will try your method too

          – craig157
          Nov 21 '18 at 9:44


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408681%2foptional-integer-cannot-be-converted-to-int-for-use-on-gui-progress-bar%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          Npm cannot find a required file even through it is in the searched directory