This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Error handling problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error handling problem" Watch "Error handling problem" New topic
Author

Error handling problem

Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

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?


Olly
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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.


"I'm not back." - Bill Harding, Twister
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

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

Joined: Jan 30, 2000
Posts: 18671
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 ]
 
Consider Paul's rocket mass heater.
 
subject: Error handling problem
 
Similar Threads
Writing a file
random access files
LinearProgramming in Java
RandomAccessFile: backspacing and inserting
Unexcepted Types... why?