• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error handling problem

 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • 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?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic