Solved

Java Calendar class - exception when using setTime()

Posted on 2004-09-27
1
1,428 Views
Last Modified: 2008-03-04

Hi,
Can anyone see anything wrong with this while loop?

while (dateResultSet.next()) {                           
               Date directDebitDate = dateResultSet.getDate("direct_debit_date");                           
                         Calendar allowable = Calendar.getInstance();
                    Calendar directDebitDueDate = Calendar.getInstance();
                      
            /* set calendar value to 3 days time */                                                
                           allowable.add(Calendar.DATE, 3);                                                
                           directDebitDueDate.setTime(directDebitDate);
                           
                           /* if it falls before 3 days time it is classified as urgent */
                           if (directDebitDueDate.before(allowable))
                           {
                                 urgentPending ++;    
                                 System.out.println("Urgent pending count " + urgentPending);
                           }else
                           {
                                 regularPending ++;
                                 System.out.println("Regular pending count " + regularPending);
                           }                            
                           
                     }


 When i run it the results from of the system.outs come up fine but the setTime method causes the following exception

Caused by: java.lang.NullPointerException
      at java.util.Calendar.setTime(Calendar.java:902)
      ... 34 more
Caused by:
java.lang.NullPointerException
      at java.util.Calendar.setTime(Calendar.java:902)
      at ie.sentenial.business.database.MySQLDataManager.getClientCustomerStatuses(Unknown Source)
      at ie.sentenial.business.database.MySQLDataManager.getClientCustomerStatuses(Unknown Source)
      at ie.sentenial.application.actions.LoginAction.getClientCustomerStatusDetails(Unknown Source)
      at ie.sentenial.application.actions.LoginAction.execute(Unknown Source)
      at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
      at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
      at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
      at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)

Any ideas anyone???
0
Comment
Question by:fyness
1 Comment
 
LVL 3

Accepted Solution

by:
KeithWatson earned 125 total points
ID: 12161853
It looks as as though there are null values in your database's direct_debit_date column. The fix for this would depend on your application. You could change the database to not allow null values and the problem won't arise (once you've populated your database correctly); however, if it's legal to have null values for the date in your application then you would need to ignore these in your piece of code, so:

while (dateResultSet.next()) {                        
              Date directDebitDate = dateResultSet.getDate("direct_debit_date");                        

               if (directDebitDate==null) {
                   continue;
                }

                       Calendar allowable = Calendar.getInstance();
                  Calendar directDebitDueDate = Calendar.getInstance();
                     
           /* set calendar value to 3 days time */                                          
                        allowable.add(Calendar.DATE, 3);                                          
                        directDebitDueDate.setTime(directDebitDate);
                       
                        /* if it falls before 3 days time it is classified as urgent */
                        if (directDebitDueDate.before(allowable))
                        {
                             urgentPending ++;    
                             System.out.println("Urgent pending count " + urgentPending);
                        }else
                        {
                             regularPending ++;
                             System.out.println("Regular pending count " + regularPending);
                        }                        
                       
                   }
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now