Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2008

Tip: Looking for answers? Try searching our database.

Date and null

Thread view: 
teser3@hotmail.com - 22 Apr 2008 04:00 GMT
I have a Date variable in a JavaBean that works with a Oracle
Database.
If there is a Date value in the database it shows in the JSP with no
problems.
But if there is no value in the Date value it gives me a null error in
the JSP.
How do I account for null with a Date object?

If there is not Date value I still want the page to show the Date
Title with nothing there.
For example:
JSP view example if there is a Date value:
Submit Date:  June 25, 2007

JSP view example if there is not a Date value should show like this
but it doesnt show and gives me a null error:
Submit Date:

My classes and JSP:

Bean:

  private Date subDate;

   public String getSubDate(){
       SimpleDateFormat formatter = new SimpleDateFormat ("MMMM dd,
yyyy");
       return formatter.format(this.subDate);
   }

   /*
  Also tried this just to put a value in there if null but it didnt
work:
      public String getSubDate(){
       if((subDate == null)
       {
            subDate = null;
       }
       SimpleDateFormat formatter = new SimpleDateFormat ("MMMM dd,
yyyy");
       return formatter.format(this.subDate);
   }

   */

   public void setSubDate(String subDate){
       this.subDate= subDate;
   }

Helper class:

while(rs.next()){
           AddressRowBean row = new AddressRowBean();
           row.setSubDate(rs.getDate("subDate"));
           ...

JSP Bean call:

Submit Date: <jsp:getProperty name="projectObject"
property="subDate" />
Lew - 22 Apr 2008 04:59 GMT
> I have a Date variable in a JavaBean that works with a Oracle
> Database.
[quoted text clipped - 3 lines]
> the JSP.
> How do I account for null with a Date object?

Test the reference for "== null".

> If there is not Date value I still want the page to show the Date
> Title with nothing there.
[quoted text clipped - 5 lines]
> but it doesnt show and gives me a null error:
> Submit Date:

Well, what do you expect when you attempt to format a null value?

> My classes and JSP:
>
[quoted text clipped - 5 lines]
>         SimpleDateFormat formatter = new SimpleDateFormat ("MMMM dd,
> yyyy");

You could set 'formatter' as a static final variable, since you never change it.

>         return formatter.format(this.subDate);
>     }
[quoted text clipped - 4 lines]
>        public String getSubDate(){
>         if((subDate == null)

This won't even compile.  Are you sure this is what your code was?

>         {
>              subDate = null;

OK, having ascertained via the 'if' that 'subDate' is null, you then
redundantly set it to null yet again, accomplishing no change.

>         }
>         SimpleDateFormat formatter = new SimpleDateFormat ("MMMM dd,
> yyyy");
>         return formatter.format(this.subDate);

It seems that you can't format a null date.  Just return "" if 'subDate == null'.

>     }
>
[quoted text clipped - 15 lines]
> Submit Date: <jsp:getProperty name="projectObject"
> property="subDate" />

If you use Expression Language (EL), it does the right thing with null values.

  Submit Date: <c:out value="${subDate}" />

However, SimpleDateFormat will still blow up on the null reference.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.