Java null java.lang.IllegalArgumentException 使用 If-Statement 解析问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25039828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:11:40  来源:igfitidea点击:

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

javaparsingif-statementnull

提问by KiteMad

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).

我在解析使用 java.sql.Date.valueOf (java.lang.IllegalArgumentException) 的日期和使用 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.

我有一个使用 HTML5 和 Chrome 请求条形码编号、日期和日期的表单,它连接到后台的 PostgreSQL 数据库,返回一个包含项目的表格。当指定所有 3 个时,这非常有效。

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:

现在,我明白解析函数不喜欢空值,为此我使用了一个 If 语句来检查空值并分配一个值:

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.

我的理解是,If 语句检查语句是否为 True,如果是,则处理代码并退出,如果语句为 False,则跳到“else”部分并处理其代码。因此,如果我不为表单中的 dateFrom 字段输入值,则字符串 'stringDateFrom' 将为空,因此 If 语句的第一部分应该运行并跳过第二部分。

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?

这不会发生,即使 'stringDateFrom' 为空值,我在 If 语句的下面一行收到错误 'java.lang.IllegalArgumentException',我认为应该跳过它?

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;

我得到错误“java.lang.NumberFormatException”,设置完全相同,但解析的是一个整数,代码行是;

barcode = Integer.parseInt(stringBarcode);

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

这与我的解析代码或 if 语句代码有关吗?

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

很抱歉这篇长文章,我是一名学生,这是我项目的一部分,我不是专业的 Java 程序员 ;-)

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.

感谢 unholysampler 为我指明了正确的方向。问题是 setParameter 返回了一个空字符串作为空字符串,因此从未满足条件。

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(""))

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

and

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

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

Thanks all for your help.

感谢你的帮助。

Edit: Stacktrace

编辑:堆栈跟踪

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

“at java.sql.Date.valueOf(Date.java:143)”这一行是指代码;

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

Within java.sql.Data

在 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.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.access0(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)

回答by Eel Lee

Date.valueOf(String s)documentationstates clearly that it

Date.valueOf(String s)文档明确指出它

Throws

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

投掷

IllegalArgumentException - 如果给定的日期不是 JDBC 日期转义格式 (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.

随着问题的解决——在这种情况下,正如作者所说,问题是输入字符串为空("")。因此,对于将来遇到相同问题的其他人 - 也请检查这种可能性。

回答by deejay

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

您是否调试过您的代码并检查它是否真的跳过了该部分或它选择了其他一些值,因此它会抛出 java.lang.IllegalArgumentException