KiteMad
KiteMad

Reputation: 33

Java null java.lang.IllegalArgumentException parsing issue using If-Statement

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)

Upvotes: 3

Views: 19586

Answers (2)

Eel Lee
Eel Lee

Reputation: 3543

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.

Upvotes: 11

deejay
deejay

Reputation: 575

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

Upvotes: 0

Related Questions