• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

java.lang.NumberFormatException: Invalid int: “” error in Android

 
Ranch Hand
Posts: 36
Android Java ME
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am making an app where user must enter basic info about self.First name,second name, city and quantity that he or she wish to order.If he or she  skip one field toast will pop up with message what field is skipped. And that is ok.That works but if he skip quantity field I got error on my phone and it send user back on previously activity.When I inspect my LogCat I see error message . Now I know why that message appears.Because it expected to find Integer, if statment. but actually finds String. But do not know how to fix it.The last if/else statemen is an Issue:
Now what i tried so far:
1) Tried to set primitive int as object Integer
2)Tried to use kolicina==0;
3)Tried to use kolicina.equals(null) / kolicina.equals(0);
4)Tried to use .isEmty();

my code:



 
Marshal
Posts: 3557
505
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will get an exception with Integer.parseInt(narucenaKolicina) if narucenaKolicina is empty or does not represent a valid number.

You could catch the exception and take correction action by wrapping the call to parseInt in a try-catch block.  For example:

 
Milos Gojic
Ranch Hand
Posts: 36
Android Java ME
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Thank you 10000 time ))))
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You do a lot of calls like:

which, looking at the method, seems to be checking the field isn't empty.
However you do nothing with the boolean result.

In fact, the check done inside there seems to be incorrect (and EditText will never be equal to a test string for example).

It looks to me like you should be correcting that method so it checks the values actually exist, and then do something with the return result.
That is, if it returns false then pop up the toast and exit.  If true, then continue to check the rest of the fields.  If all true then (and only then) deal with the data.

Currently you only validate after you start using the data.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic