Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6335533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:52:37+00:00 2026-05-24T18:52:37+00:00

I have this code and when I run the script, I pass in valid

  • 0

I have this code and when I run the script, I pass in valid parameters, but I keep on getting a NPE.
Help?

Code:

private static Date getNearestDate(List<Date> dates, Date currentDate) {
    long minDiff = -1, currentTime = currentDate.getTime();
    Date minDate = null;
    if (!dates.isEmpty() && currentDate != null) {
        for (Date date : dates) {
            long diff = Math.abs(currentTime - date.getTime());
            if ((minDiff == -1) || (diff < minDiff)) {
                minDiff = diff;
                minDate = date;
            }
        }
    }
    return minDate;
}

I get the NullPointerException from line 2 of the code above and I use the following code to pass in thisDate as the currentDate variable.

Date thisDate = null;
try {
    thisDate = (new SimpleDateFormat("MM/dd/yyyy")).parse(Calendar.getInstance().getTime().toString());
} catch (Exception e) {}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:52:38+00:00Added an answer on May 24, 2026 at 6:52 pm

    Since you’ve indicated that the NullPointerException is thrown on line 2, we can deduce that you’re passing in null for the currentDate argument. currentDate.getTime() is the only part of line 2 that can cause a NullPointerException.

    Update:

    I just wrote the following Test.java code to really understand what your problem is:

    import java.util.*;
    import java.text.*;
    
    class Test {
        public static void main(String[] args) {
            Date thisDate = null;
            try {
                thisDate = (new SimpleDateFormat("MM/dd/yyyy")).parse(Calendar.getInstance().getTime().toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    When I run it, I get:

    java.text.ParseException: Unparseable date: "Sat Aug 20 12:42:30 PDT 2011"
        at java.text.DateFormat.parse(DateFormat.java:337)
        at Test.main(Test.java:8)
    

    So the problem is that your SimpleDateFormat.parse() expects the month/day/year format, but the Date class’s toString() method is giving you something different.

    It seems as though all you really want is the current date. Why bother formatting it? Just trim it down to this and be done with it:

    Date thisDate = new Date(); // this gives you the current date
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tryed to run this code in my console: script/plugin install git://github.com/apotonick/cells.git ...but
I have this code to run a slideshow (this is only part of what
I have created this code, and when I run it, don't get any errors
I have a question concerning this code which I want to run on QNX:
I have a delphi 7 form: and my code: when I run this form
I have got some code to pass in a variable into a script from
I have this code: <script language=javascript> $(function() { $('#items,#button-search').hide(); $(#companies).click(function() { $(this).attr(value,); $('#address,#new-company-form').empty(); });
I have this code in jQuery, that I want to reimplement with the prototype
I have this code: chars = #some list try: indx = chars.index(chars) except ValueError:
I have this code that performs an ajax call and loads the results into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.