3

I have an issue with parsing both a date using java.sql.Date.valueOf (java.lang.IllegalArgumentException) and an integer using Integer.parseInt (java.lang.NumberFormatException).

I have a form that requests a barcode number, a date-from and date-to using HTML5 and Chrome, this connects to a PostgreSQL database in the background returning a table with the items. This works perfectly when all 3 are specified.

However, I want the user to be able to search using just the barcode, so all relevant entries, regardless of what date, are returned. Similarly, using just dates to get any entry between the specified dates.

This is my Form:

<th><input type="text" name="barcode" size="20"></th>
        <th><input type="date" name="dateFrom"></th>
        <th><input type="date" name="dateTo"></th>
        <th><input type="submit" value="Submit" /></th>

Now, I understand that the parsing functions do not like null values, and to this end I have used an If-statement to check for a null value and assign a value:

Date dateFrom;
String stringDateFrom = request.getParameter("dateFrom");

if (stringDateFrom == null){
        dateFrom = Date.valueOf(LocalDate.MIN);
    } else {
        dateFrom = Date.valueOf(stringDateFrom);
    }

My understanding is that the If-statement checks if the statement is True, if it is, it processes the code and exits, if the statement is False it skips to the "else" section and processes its code. So, if I don't input a value for the dateFrom field in the form the String 'stringDateFrom' would be null, and so the first portion of the If-statement should run and skip the second portion.

This does not happen, even with a null value for 'stringDateFrom' I get the error 'java.lang.IllegalArgumentException' at the line below of the If-statement, which I thought should have been skipped?

dateFrom = Date.valueOf(stringDateFrom);

I get the error 'java.lang.NumberFormatException' with exactly the same setup but parsing an integer, the line of code being;

barcode = Integer.parseInt(stringBarcode);

Is this something with my parsing code or my if-statement code?

Sorry for the long post, I'm a student and this is part of my project, I'm not a java programmer by trade ;-)

Thanks for any and all help.

Edit: Solved

Thanks to unholysampler for pointing me in the right direction. The problem was that the setParameter was returning an empty String as apposed to null and so the condition was never met.

I changed the code to check for an empty String, or not an empty String, and it works, i.e.

if (stringDateFrom == null) to if (stringDateFrom.equals(""))

and

if (stringBarcode != null) to if (!stringBarcode.equals(""))

Thanks all for your help.

Edit: Stacktrace

The line 'at java.sql.Date.valueOf(Date.java:143)' refers to the code;

if (d == null) {
            throw new java.lang.IllegalArgumentException();
        }

Within java.sql.Data

Warning:   StandardWrapperValve[logListServlet]: Servlet.service() for servlet logListServlet threw exception java.lang.IllegalArgumentException

at java.sql.Date.valueOf(Date.java:143)
at org.mypackage.HOIS.logListServlet.doPost(logListServlet.java:57)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:744)
KiteMad
  • 33
  • 1
  • 1
  • 5
  • please post the stacktrace – Philipp Sander Jul 30 '14 at 14:48
  • I'm using Netbeans 8.0 and Glashfish 4.0. Where would I get the stacktrace? Would it be the Glassfish log which looks like this Warning: StandardWrapperValve[logListServlet]: Servlet.service() for servlet logListServlet threw exception java.lang.IllegalArgumentException at java.sql.Date.valueOf(Date.java:143) at org.mypackage.HOIS.logListServlet.doPost(logListServlet.java:57) at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)........ – KiteMad Jul 30 '14 at 16:07
  • yes. this is it. please add it to your question to improve its readability – Philipp Sander Jul 31 '14 at 06:40
  • Updated with the Glashfish stacktrace. – KiteMad Jul 31 '14 at 09:20

2 Answers2

11

Date.valueOf(String s) documentation states clearly that it

Throws

IllegalArgumentException - if the date given is not in the JDBC date escape format (yyyy-[m]m-[d]d)

so I guess you should check the date's format first.

Edit:

As the problem is resolved - in this case, as the author said, the problem was that the input String was empty (""). So, for the other people with the same problem in future - check this possibility too.

Community
  • 1
  • 1
Eel Lee
  • 3,513
  • 2
  • 31
  • 49
  • Exactly. The issue is not about if a value exists or not, it is about the format of the value. `getParameter()` might be returning an empty `String` instead of `null` when no value exists. – unholysampler Jul 30 '14 at 14:51
  • Thanks, I'm pretty sure Chrome uses the yyyy-mm-dd format, going by these articles and the fact that my code works perfectly when all 3 inputs are made, which includes 2 dates. http://stackoverflow.com/questions/7372038/is-there-any-way-to-change-input-type-date-format http://updates.html5rocks.com/2012/08/Quick-FAQs-on-input-type-date-in-Google-Chrome – KiteMad Jul 30 '14 at 16:03
  • Thanks to unholysampler for pointing me in the right direction. The problem was that the setParameter was returning an empty String as apposed to null and so the condition was never met. I changed the code to check for an empty String, or not an empty String, and it works, i.e. if (stringDateFrom == null) to if (stringDateFrom.equals("")) and if (stringBarcode != null) to if (!stringBarcode.equals("")) Thanks all for your help. – KiteMad Jul 31 '14 at 11:23
0

Have you debugged your code and checked if its really skipping that part or its picking up some other value, due to which its throwing java.lang.IllegalArgumentException

deejay
  • 575
  • 1
  • 8
  • 24
  • Hi, I'm using Glashfish and within the log it shows the errors, some of these errors are linked which will highlight the code it relates to, in my case its the line 'dateFrom = Date.valueOf(stringDateFrom);' so I can only assume it isn't skipping this code as expected. – KiteMad Jul 30 '14 at 16:10