简体   繁体   English

java NullPointerException错误将int转换为string

[英]java NullPointerException error converting int to string

I have an HTML form that calls a Java servlet and the form contains 20 checkboxes (eg with names of c1 , c2 , c3 ... c20 ). 我有一个调用Java servlet的HTML表单,表单包含20个复选框(例如,名称为c1c2c3 ... c20 )。

I'm trying to capture the value of those checkboxes in a java boolean array cbox . 我试图在java布尔数组cbox捕获这些复选框的值。

The following code... 以下代码......

int ii=0;
boolean cbox[] = new boolean[20];
for (ii=0; ii<20; ii++)
   cbox[ii] = (req.getParameter("c"+String.valueOf((int)(ii+1))).equals("on"))?true:false;

gives a java.lang.NullPointerException . 给出了java.lang.NullPointerException

But, I don't get a run time error if I were to change it to (eg remove ii in valueOf ): 但是,如果我要将其更改为(例如,在valueOf删除ii ),则不会出现运行时错误:

   cbox[ii] = (req.getParameter("c"+String.valueOf((int)(1))).equals("on"))?true:false;

Of course, that doesn't get me where I want. 当然,这不能让我到达我想要的地方。 I must be making a silly mistake somewhere but I can't spot it. 我必须在某个地方犯一个愚蠢的错误,但我无法发现它。 Does anyone see it? 有人看到了吗?

A NullPointerException occur when you try to reference an Object that is not initialized (null). 当您尝试引用未初始化的Object(null)时,会发生NullPointerException

Looking at your code, there are two possibilities : 查看代码,有两种可能性:

  • req is null req.getParameter . req为null req.getParameter
  • The parameter you are trying to retrieve does not exists hence is null req.getParameter("c"+String.valueOf((int)(ii+1))) . 您尝试检索的参数不存在因此为null req.getParameter("c"+String.valueOf((int)(ii+1)))

By the way, if your returned parameter is already a boolean, there is no need to check the value of it and return true or false as it value is already true or false. 顺便说一下,如果返回的参数已经是布尔值,则无需检查它的值并返回true或false,因为它的值已经为true或false。 You can simplify it to : 您可以将其简化为:

cbox[ii] = (req.getParameter("c"+String.valueOf((int)(ii+1))).equals("on"));

Edit : To answer to your comment, you could validate if the parameter exist easily : 编辑:要回答您的评论,您可以验证参数是否容易存在:

String param = req.getParameter("c" + String.valueOf(ii + 1));
cbox[ii] = "on".equals(param);

To be even more secure, I would also check req to make sure it is not null. 为了更加安全,我还会检查req以确保它不为空。

if(req != null)
{
    String param = req.getParameter("c" + String.valueOf((ii + 1));
    cbox[ii] = "on".equals(param);
}

Notice that I removed the cast (int)ii + 1 as the expression is already of type integer. 请注意,我删除了cast (int)ii + 1因为表达式已经是整数类型。

Also, if ii is only to be used as an iterator id, you can declare it directly in the loop, so instead of doing this : 另外,如果ii只用作迭代器id,你可以直接在循环中声明它,所以不要这样做:

int ii = 0;
for(ii = 0; ii < 20; ii++)

You can directly write for(int ii = 0; ii < 20; ii++) 你可以直接写for(int ii = 0; ii < 20; ii++)

The problem here is in servlet, you will receive on as default value for html selected checkbox else null. 这里的问题是在servlet中,您将收到on为HTML选择复选框否则返回null默认值。 Since all checkboxes are not checked you get NPE. 由于未选中所有复选框,因此您将获得NPE。 Try this code snippet: 试试这段代码:

boolean[] cbox = new boolean[20];
for(int i = 0; cbox.length > i; i++) {
   cbox[i] = null != req.getParameter("c" + (1 + i));//if not null then true else false
}

You can also go with other option: 您还可以选择其他选项:

in your html/jsp create checkboxes with same name but different value: 在您的html / jsp中创建具有相同名称但值不同的复选框:

<input type="checkbox" name="cbox" value="1"/>
<input type="checkbox" name="cbox" value="2"/>
...
<input type="checkbox" name="cbox" value="20"/>

in servlet you can fetch all checked boxes with single line: servlet中,您可以使用单行获取所有选中的复选框:

String[] cbox = req.getParameterValues("cbox");

Here you won't get on as value, its gona be 1 , 2 and so on as per selection. 在这里,你不会得到on作为价值,它的戈纳是12等为每个选择的。 Only checked boxes you will get here. 只有复选框才能到达此处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM