Solved

Java Calendar class - exception when using setTime()

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Basic Java Case or If-Else statement... 3 50
Spring Framework HTTPSession management 1 33
table example 4 29
Tomcat: Unable to run tomcat service. 2 18
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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

816 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

12 Experts available now in Live!

Get 1:1 Help Now