Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Error handling problem

 
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note: I'm making a method that will delete something. It not done yet. And can post the whole code if you wish but it like 750 lines or something and requires some external files to work.

With this code, it gets a null value for string "aName" if "id" exceeds the number of lines in the file.


But if want avoid the program from getting this null it jumps to the FileNotFoundException catch


Anyone got an idea as to why that happens?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[B][Olivier]: But if want avoid the program from getting this null it jumps to the FileNotFoundException catch

[/B]
No, you're calling the exception a FileNotFoundException, but in reality its type can be any Exception. Try this:

This will tell you what kind of error you really have here, and what line it's coming from.
 
Olivier Legat
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm... Well I found an alternative way to solve my problem but thanks for that advice Mr.Yingst, could come in handy some time.

Does any one know a way to check and see if a string is empty? That's what I was trying to do but it seemed to bother the compiler so I took an alternative root.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The stack trace would have shown you had a NullPointerException coming from this line:

That's because if aName is null, then .equals() will throw an NPE, no matter what comes after it in parentheses. A better way to check this would be

You shouldn't use == or != to compare strings, except when you're comparing them with null.

To check if the name is blank, I would first check if it's null, then check if its length is 0:

(or perhaps you want to check if it's not blank, in which case you replace the final == with != instead.)
[ February 20, 2008: Message edited by: Jim Yingst ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic