• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Catching a Bad Date

 
Ranch Hand
Posts: 2153
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following code on of the dates from the remote data is 10/15/0209. The following line throws a nullpointerexception when this date is reached. The resultset says the value of rs.getString(3) is null. How can I catch this error so the process can continue and complete? What I have tried here does not work because the error happens before the ParseException fires.



 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can think of 3 options:

1) Figure out when the null happens, and try to prevent it from happening by modifying your SQL. I am not SQL expert so I don't know how to do that (maybe build a case for when there is no ftdate? )

2) Check the value for rs.getString(3) for null. If it is null deal with it appropriatedly:


3) Catch the null pointer exception and do something meaningful with it. What you do depends on the situation. Maybe make up some default value or just report the problem... depends on the situation


I would go with option 1 if I had a choice.

p.s. I hope you do a better report on the parse exception tan just errorstring = "Error" That isn't really useful.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another alternative would be to use the getDate method of ResultSet, then you don't need to care about date formatting.
 
Steve Dyke
Ranch Hand
Posts: 2153
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Luke wrote:I can think of 3 options:

1) Figure out when the null happens, and try to prevent it from happening by modifying your SQL. I am not SQL expert so I don't know how to do that (maybe build a case for when there is no ftdate? )


I would go with option 1 if I had a choice.



I don't see how I can use option 1 since the field does have a date even though it is wrong(10/14/0290)
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:

Steve Luke wrote:I can think of 3 options:

1) Figure out when the null happens, and try to prevent it from happening by modifying your SQL. I am not SQL expert so I don't know how to do that (maybe build a case for when there is no ftdate? )


I would go with option 1 if I had a choice.



I don't see how I can use option 1 since the field does have a date even though it is wrong(10/14/0290)



My bad, I thought you were getting nulls because the data being retrieved did not exist. For example isn't there a 'Where x is Not Null' type of statement? Maybe just for certain DBs or statement types... dunno, like I said, I am not an expert. Couldn't that be used on a value being returned as a calculation based on some other field. Examples:

Or


Like I said, I don't know if either would work, I know just enough SQL to break things.


If it can't be fixed in SQL then you have to do something else. Where does the wrong date come from? What is the application you are working on? If your application is the data - entry application then you should check the data in the Java layer to make sure, by the time it gets to the DB, it is correct. If your application is just the data - receiver and the broken date is in the database then your application should dutifully report the problem and move on (without crashing - so using 2 or 3).

* The in-code problem is caused because the dayofweek SQL function returns null - cascading to null being returned as the ftdate column. Your real problem comes from the bad date in the database. The solution to the real problem is to assign the correct date to the broken one in the database and fix your data entry method so as to prevent this type of thing in the future.
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion, rs.getString(whatever number) should be treated with respect separately if that is where you NPE is being caused.

Also, try to use column names.
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic