Solved

Java Calendar class - exception when using setTime()

Posted on 2004-09-27
1
1,446 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java asynchronous logging 4 50
unix in java example 9 53
simple java question 3 44
even odd program using while loop 3 29
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

920 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

13 Experts available now in Live!

Get 1:1 Help Now