How to write double value into JTextField?












1















I want to input a price and write it into a text file, but it gives a "double cannot be dereferenced" error.



This is a snippet of my code.



String id = "P" + ProductID.getText();
String name = ProductName.getText();
String brand = ProductBrand.getText();
String model = ProductModel.getText();
String type = ProductType.getText();
double price = Double.parseDouble(ProductPrice.getText());

if (id.isEmpty() || name.isEmpty() || brand.isEmpty() || model.isEmpty() || type.isEmpty() || price.isEmpty())
{
JOptionPane.showMessageDialog(null, "Please Fill In Everything!", "Error", JOptionPane.ERROR_MESSAGE);
}









share|improve this question

























  • Best practice would be to put a try catch block around where you call parseDouble. If you get a ParseException here, you can / should show a message to the user saying that they haven't entered a valid number in this field

    – ControlAltDel
    Nov 15 '18 at 16:25
















1















I want to input a price and write it into a text file, but it gives a "double cannot be dereferenced" error.



This is a snippet of my code.



String id = "P" + ProductID.getText();
String name = ProductName.getText();
String brand = ProductBrand.getText();
String model = ProductModel.getText();
String type = ProductType.getText();
double price = Double.parseDouble(ProductPrice.getText());

if (id.isEmpty() || name.isEmpty() || brand.isEmpty() || model.isEmpty() || type.isEmpty() || price.isEmpty())
{
JOptionPane.showMessageDialog(null, "Please Fill In Everything!", "Error", JOptionPane.ERROR_MESSAGE);
}









share|improve this question

























  • Best practice would be to put a try catch block around where you call parseDouble. If you get a ParseException here, you can / should show a message to the user saying that they haven't entered a valid number in this field

    – ControlAltDel
    Nov 15 '18 at 16:25














1












1








1








I want to input a price and write it into a text file, but it gives a "double cannot be dereferenced" error.



This is a snippet of my code.



String id = "P" + ProductID.getText();
String name = ProductName.getText();
String brand = ProductBrand.getText();
String model = ProductModel.getText();
String type = ProductType.getText();
double price = Double.parseDouble(ProductPrice.getText());

if (id.isEmpty() || name.isEmpty() || brand.isEmpty() || model.isEmpty() || type.isEmpty() || price.isEmpty())
{
JOptionPane.showMessageDialog(null, "Please Fill In Everything!", "Error", JOptionPane.ERROR_MESSAGE);
}









share|improve this question
















I want to input a price and write it into a text file, but it gives a "double cannot be dereferenced" error.



This is a snippet of my code.



String id = "P" + ProductID.getText();
String name = ProductName.getText();
String brand = ProductBrand.getText();
String model = ProductModel.getText();
String type = ProductType.getText();
double price = Double.parseDouble(ProductPrice.getText());

if (id.isEmpty() || name.isEmpty() || brand.isEmpty() || model.isEmpty() || type.isEmpty() || price.isEmpty())
{
JOptionPane.showMessageDialog(null, "Please Fill In Everything!", "Error", JOptionPane.ERROR_MESSAGE);
}






java swing double jtextfield






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 23:44









Andrew Thompson

154k28163345




154k28163345










asked Nov 15 '18 at 16:17









bk23bk23

103




103













  • Best practice would be to put a try catch block around where you call parseDouble. If you get a ParseException here, you can / should show a message to the user saying that they haven't entered a valid number in this field

    – ControlAltDel
    Nov 15 '18 at 16:25



















  • Best practice would be to put a try catch block around where you call parseDouble. If you get a ParseException here, you can / should show a message to the user saying that they haven't entered a valid number in this field

    – ControlAltDel
    Nov 15 '18 at 16:25

















Best practice would be to put a try catch block around where you call parseDouble. If you get a ParseException here, you can / should show a message to the user saying that they haven't entered a valid number in this field

– ControlAltDel
Nov 15 '18 at 16:25





Best practice would be to put a try catch block around where you call parseDouble. If you get a ParseException here, you can / should show a message to the user saying that they haven't entered a valid number in this field

– ControlAltDel
Nov 15 '18 at 16:25












3 Answers
3






active

oldest

votes


















2














price is a primitive type of double - it is not an object. So you cannot perform an operation like .xxx() on it.



Instead you can check for example



if(price != 0.0)





share|improve this answer
























  • Good answer, but you shouldn't use equals operators on doubles. Math is weird.

    – killjoy
    Nov 15 '18 at 16:24



















0














Try it:



String tmp = ProductPrice.getText();
if(tmp.isEmpty()) // error
else{
double price = Double.parseDouble(tmp);
// do something
}





share|improve this answer































    0














    As Nicholas K said, price is a primitive variable and we can't invoke any methods on this.



    Also want to note that if ProductPrice.getText() has null value, then you will have NullPointerException when try to parseDouble.



    Also you can get NumberFormatException in case the ProductPrice.getText() has empty string.



    So I guess it would be good to handle the possible exception with try catch block like this:



    try {
    double price = Double.parseDouble(ProductPrice.getText());
    } catch (NumberFormatException e) {
    // handle error case
    }





    share|improve this answer























      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%2f53323657%2fhow-to-write-double-value-into-jtextfield%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      price is a primitive type of double - it is not an object. So you cannot perform an operation like .xxx() on it.



      Instead you can check for example



      if(price != 0.0)





      share|improve this answer
























      • Good answer, but you shouldn't use equals operators on doubles. Math is weird.

        – killjoy
        Nov 15 '18 at 16:24
















      2














      price is a primitive type of double - it is not an object. So you cannot perform an operation like .xxx() on it.



      Instead you can check for example



      if(price != 0.0)





      share|improve this answer
























      • Good answer, but you shouldn't use equals operators on doubles. Math is weird.

        – killjoy
        Nov 15 '18 at 16:24














      2












      2








      2







      price is a primitive type of double - it is not an object. So you cannot perform an operation like .xxx() on it.



      Instead you can check for example



      if(price != 0.0)





      share|improve this answer













      price is a primitive type of double - it is not an object. So you cannot perform an operation like .xxx() on it.



      Instead you can check for example



      if(price != 0.0)






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 15 '18 at 16:19









      Nicholas KNicholas K

      7,74661537




      7,74661537













      • Good answer, but you shouldn't use equals operators on doubles. Math is weird.

        – killjoy
        Nov 15 '18 at 16:24



















      • Good answer, but you shouldn't use equals operators on doubles. Math is weird.

        – killjoy
        Nov 15 '18 at 16:24

















      Good answer, but you shouldn't use equals operators on doubles. Math is weird.

      – killjoy
      Nov 15 '18 at 16:24





      Good answer, but you shouldn't use equals operators on doubles. Math is weird.

      – killjoy
      Nov 15 '18 at 16:24













      0














      Try it:



      String tmp = ProductPrice.getText();
      if(tmp.isEmpty()) // error
      else{
      double price = Double.parseDouble(tmp);
      // do something
      }





      share|improve this answer




























        0














        Try it:



        String tmp = ProductPrice.getText();
        if(tmp.isEmpty()) // error
        else{
        double price = Double.parseDouble(tmp);
        // do something
        }





        share|improve this answer


























          0












          0








          0







          Try it:



          String tmp = ProductPrice.getText();
          if(tmp.isEmpty()) // error
          else{
          double price = Double.parseDouble(tmp);
          // do something
          }





          share|improve this answer













          Try it:



          String tmp = ProductPrice.getText();
          if(tmp.isEmpty()) // error
          else{
          double price = Double.parseDouble(tmp);
          // do something
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 16:24









          TixTix

          616




          616























              0














              As Nicholas K said, price is a primitive variable and we can't invoke any methods on this.



              Also want to note that if ProductPrice.getText() has null value, then you will have NullPointerException when try to parseDouble.



              Also you can get NumberFormatException in case the ProductPrice.getText() has empty string.



              So I guess it would be good to handle the possible exception with try catch block like this:



              try {
              double price = Double.parseDouble(ProductPrice.getText());
              } catch (NumberFormatException e) {
              // handle error case
              }





              share|improve this answer




























                0














                As Nicholas K said, price is a primitive variable and we can't invoke any methods on this.



                Also want to note that if ProductPrice.getText() has null value, then you will have NullPointerException when try to parseDouble.



                Also you can get NumberFormatException in case the ProductPrice.getText() has empty string.



                So I guess it would be good to handle the possible exception with try catch block like this:



                try {
                double price = Double.parseDouble(ProductPrice.getText());
                } catch (NumberFormatException e) {
                // handle error case
                }





                share|improve this answer


























                  0












                  0








                  0







                  As Nicholas K said, price is a primitive variable and we can't invoke any methods on this.



                  Also want to note that if ProductPrice.getText() has null value, then you will have NullPointerException when try to parseDouble.



                  Also you can get NumberFormatException in case the ProductPrice.getText() has empty string.



                  So I guess it would be good to handle the possible exception with try catch block like this:



                  try {
                  double price = Double.parseDouble(ProductPrice.getText());
                  } catch (NumberFormatException e) {
                  // handle error case
                  }





                  share|improve this answer













                  As Nicholas K said, price is a primitive variable and we can't invoke any methods on this.



                  Also want to note that if ProductPrice.getText() has null value, then you will have NullPointerException when try to parseDouble.



                  Also you can get NumberFormatException in case the ProductPrice.getText() has empty string.



                  So I guess it would be good to handle the possible exception with try catch block like this:



                  try {
                  double price = Double.parseDouble(ProductPrice.getText());
                  } catch (NumberFormatException e) {
                  // handle error case
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 16:45









                  RuslanRuslan

                  3,426826




                  3,426826






























                      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%2f53323657%2fhow-to-write-double-value-into-jtextfield%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

                      List item for chat from Array inside array React Native

                      Thiostrepton

                      Caerphilly