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











up vote
0
down vote

favorite












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




























    up vote
    0
    down vote

    favorite












    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


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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 at 10:53









      I Don't Exist

      2,32511418




      2,32511418










      asked Nov 21 at 9:16









      craig157

      53




      53
























          4 Answers
          4






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          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 at 9:41


















          up vote
          1
          down vote













          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 at 10:38


















          up vote
          0
          down vote













          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 at 9:42


















          up vote
          0
          down vote













          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 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',
          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








          up vote
          0
          down vote



          accepted










          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 at 9:41















          up vote
          0
          down vote



          accepted










          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 at 9:41













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          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 at 9:21









          Ravindra Ranwala

          7,94331533




          7,94331533








          • 1




            That's worked perfectly! Thanks Ravindra
            – craig157
            Nov 21 at 9:41














          • 1




            That's worked perfectly! Thanks Ravindra
            – craig157
            Nov 21 at 9:41








          1




          1




          That's worked perfectly! Thanks Ravindra
          – craig157
          Nov 21 at 9:41




          That's worked perfectly! Thanks Ravindra
          – craig157
          Nov 21 at 9:41












          up vote
          1
          down vote













          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 at 10:38















          up vote
          1
          down vote













          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 at 10:38













          up vote
          1
          down vote










          up vote
          1
          down vote









          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 at 9:39









          Hoopje

          9,80152543




          9,80152543












          • Thank you for breaking it down Hoopje
            – craig157
            Nov 21 at 10:38


















          • Thank you for breaking it down Hoopje
            – craig157
            Nov 21 at 10:38
















          Thank you for breaking it down Hoopje
          – craig157
          Nov 21 at 10:38




          Thank you for breaking it down Hoopje
          – craig157
          Nov 21 at 10:38










          up vote
          0
          down vote













          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 at 9:42















          up vote
          0
          down vote













          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 at 9:42













          up vote
          0
          down vote










          up vote
          0
          down vote









          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 at 9:24









          Stephen C

          509k69554908




          509k69554908












          • Thanks Stephen - the orElse(0) did the trick! And yes this field must have a value over 0.1
            – craig157
            Nov 21 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 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 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 at 9:42










          up vote
          0
          down vote













          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 at 9:44















          up vote
          0
          down vote













          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 at 9:44













          up vote
          0
          down vote










          up vote
          0
          down vote









          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 at 9:40









          rogerkl

          312




          312












          • Thanks Roger - I will be doing this on another field so will try your method too
            – craig157
            Nov 21 at 9:44


















          • Thanks Roger - I will be doing this on another field so will try your method too
            – craig157
            Nov 21 at 9:44
















          Thanks Roger - I will be doing this on another field so will try your method too
          – craig157
          Nov 21 at 9:44




          Thanks Roger - I will be doing this on another field so will try your method too
          – craig157
          Nov 21 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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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

          Berounka

          Sphinx de Gizeh

          Different font size/position of beamer's navigation symbols template's content depending on regular/plain...