-2

I'm trying to find data according to date throw the exception

java.lang.IllegalArgumentException: null

@SuppressWarnings("deprecation")
    @GetMapping("/product-DateList/{pathDate}")
    public ModelAndView getFindByDateOfPurchase(@PathVariable("pathDate") String pathDate) {
        Date convertDate = new Date(pathDate);
        //Date Converted according to dataBase Format 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String strDate = sdf.format(convertDate);
        Date date = null; 
        try {
            date =   DateFormat.getInstance().parse(strDate);
        } catch (ParseException pe) {
            pe.printStackTrace();
        }

        List<Product> dateofPurchaseList = grofersService.findByDateOfPurchase(date);

        Optional<List<Product>> optional = Optional.of(dateofPurchaseList);
        if (!optional.isPresent()) {
            logger.warn("Not Found Product");
            return new ModelAndView("notFound"); 
        }
        logger.info("Fetching Product according to Date");
        return new ModelAndView("productList", "dateofPurchaseList", dateofPurchaseList); 
    }

Exception (PrintStacktrace)

java.lang.IllegalArgumentException: null
    at java.util.Date.parse(Unknown Source) ~[na:1.8.0_172]
    at java.util.Date.<init>(Unknown Source) ~[na:1.8.0_172]
    at com.javabootstar.controller.GrofersController.getFindByDateOfPurchase(GrofersController.java:66) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_172]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_172]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_172]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_172]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.31.jar:8.5.31]org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.31.jar:8.5.31]
        at java.lang.Thread.run(Unknown Source) [na:1.8.0_172]

search this problem in google does not find this type of problem, how to find data according to date.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ng Sharma
  • 2,072
  • 8
  • 27
  • 49
  • Please post the whole stacktrace. – davidxxx Jul 15 '18 at 10:43
  • Can you share entire stacktrace? – Yogendra Mishra Jul 15 '18 at 10:48
  • okay i will share. – Ng Sharma Jul 15 '18 at 10:48
  • In `GrofersController.java` line 66 you do something with a date that is `null`. – Henry Jul 15 '18 at 11:06
  • but how to find data according to date. – Ng Sharma Jul 15 '18 at 11:10
  • You can configure the controller method to parse automatically the argument into a date. You can check out this answer: https://stackoverflow.com/a/15164985/3921725 – Alex Roig Jul 15 '18 at 11:14
  • And point us what's on line 66. Add a print statement or run the debugger and check what's going on there. Also, what are you exactly doing with the `pathDate`? It's unused in your code. – Alex Roig Jul 15 '18 at 11:16
  • this link is very helpful but one doubt . I'm using PathVariable annotation then throw the exception. @AlexRoig – Ng Sharma Jul 15 '18 at 11:38
  • Since you can use Java 8 (or higher, for example`Optional` (and even if you could not)), you should shun the old classes `Date`, `SimpleDateFormat` and `DateFormat` since they are poorly designed, troublesome and also long outdated. [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) is so much nicer to work with. – Ole V.V. Jul 15 '18 at 11:43
  • @PathVariable("pathDate") @DateTimeFormat(iso=ISO.DATE) Date dateReceived) it possible to use but i'm use this statement throw Exception parseException @Alex Roig – Ng Sharma Jul 15 '18 at 14:34

2 Answers2

1

There are several problems with the code you have posted, more importantly that this would not compile.

Date convertDate = new Date - This is obvsiously wrong. In any way disregarding this there are several more problems:

You seem to pass in a path variable pathDate with which I assume you need to have to do your search. This is never used in the body of the method. Instead you create (in a wrong way) a new variable named convertDate. Which you proceed to convert over to a string for no apparent reason and usage.

You then proceed to create a new variable named date which you attempt to initialize using the strDate variable. All these actions are fundamentally wrong. Also your conversion from String to date is also wrong (hence why you get the IllegalArgumentException).

You should not be using the parent class (DateFormat) to perform date conversion but rather you need to perform the conversion using some implementation of it (DateFormat is abstract and a direct implementation is the SimpleDateFormat).

Based on the above, you should do the following:

  • Grab the date from the path variable.

  • Use SimpleDateFormat to convert over to a date object (alternatively you can use Java 8's LocalDate API)

  • Then pass this date to your service method that will fetch the results from the repository.

Also a few more notes. There is no need to do this:

Optional<List<Product>> optional = Optional.of(dateofPurchaseList);

Assuming that your service method returns either a list of products or an empty list if none are found, the subsequent dateofPurchaseList will never be null. Wrapping this into an optional using the of static creator (which assumes non-nullnes) is wrong. You should simple invoke List#isEmpty to just check whether the returned list is empty or contains any values.

akortex
  • 5,067
  • 2
  • 25
  • 57
0

Use this in repository interface

@Query("select e from Entity where e.dateOfPurchase like %:dateOfPurchase%")
List<Entity> findByDateOfPurchase(@Param("dateOfPurchase ")String dateOfPurchase );
Rahul Baghaniya
  • 291
  • 4
  • 5