简体   繁体   English

在我的arraylist.set()上获取空值。 使用jsp

[英]Getting a null value on my arraylist.set(). using jsp

My objective is to add words from a html form, store them in my array, display them in the page, and have two links one for delete the item and one for edit the item(the word for edit will be entered in another html form). 我的目标是从html表单中添加单词,将其存储在我的数组中,在页面中显示它们,并具有两个链接,一个用于删除项目,一个用于编辑项目(用于编辑的单词将以另一种html形式输入)。

I am using an arrayList to store the input coming from an Input type. 我正在使用arrayList存储来自Input类型的输入。 I am able to add and delete my item, but when I tried to change the value of an item in my arrayList (I am using arrayList.set()), my value returns null. 我可以添加和删除项目,但是当我尝试更改arrayList中项目的值时(我正在使用arrayList.set()),我的值将返回null。 If I hard code the set() I see my item change but for some reason I can't get the value from my input type. 如果我对set()进行了硬编码,我会看到我的商品发生了变化,但是由于某种原因,我无法从输入类型中获取该值。 Wondering what I am missing. 想知道我缺少什么。

I have two jsp pages. 我有两个jsp页面。 main.jsp and edit.jsp. main.jsp和edit.jsp。 One java class WordOperations with all the validations 一个带有所有验证的Java类WordOperations

Here is my code Sorry if it is messy and if doesn't display right in here. 这是我的代码很抱歉,如果它很乱并且在此处未正确显示。 This is the first time I am posting on stackoverflow 这是我第一次在stackoverflow上发布

Any help will be appreciated. 任何帮助将不胜感激。 Thanks 谢谢

This is my code for the form where i display my items main.jsp 这是我显示项目main.jsp的表单的代码

 <h3>Word List</h3>
        <form action="WordOperations" method="POST">
            Word: <input type="text" name="newWord">
            <input type="submit" value="Add">
        </form>
        <br/>

            <%
                ArrayList<String> words = (ArrayList<String>) session.getAttribute("words");

                if (words!=null)
                {
                    for (int i=0; i<words.size(); i++)
                    {
                         out.println("<p>" +  "<a href='http://www."+ words.get(i) +".com'>" + words.get(i) +"</a>"  + "&nbsp;&nbsp;<a href='WordOperations?delete=" + i + "'>Delete</a>&nbsp;&nbsp;<a href='WordOperations?edit=" + i + "'>Edit</a></p>");

                    }
                }

            %>

This is my code for my for where I enter the new information edit.jsp 这是我输入新信息edit.jsp的代码

  <form action="WordOperations?" method="POST">
        New Link:<input type="text" name="userChange">
        <input type="submit" name="userChangePassword" value="Change link">
        </form>

And this is the code for validation of my forms WordOperations.java 这是验证我的表格WordOperations.java的代码

 HttpSession session = request.getSession();

        String newWord = request.getParameter("newWord");
        String delete = request.getParameter("delete");
        String edit = request.getParameter("edit");
        String newLink = request.getParameter("userChange");

        if (delete!=null) //delete a word
        {
            int delIndex = Integer.parseInt(delete);

            ArrayList<String> words = (ArrayList<String>) session.getAttribute("words");
            words.remove(delIndex);

            RequestDispatcher rd = getServletContext().getRequestDispatcher("/main.jsp");
            request.setAttribute("message", "Word deleted");
            rd.forward(request, response);  
        }
        else if(edit !=null)
        {   


            int edIndex = Integer.parseInt(edit);
            String link = newLink;
            ArrayList<String> words = (ArrayList<String>) session.getAttribute("words");
            words.set(edIndex,"google");
            words.add(newLink);
                         RequestDispatcher rd = getServletContext().getRequestDispatcher("/edit.jsp");


            request.setAttribute("message", "Word edited");
            rd.forward(request, response);

        }
        else //logging in
        {
            if (newWord!=null && !newWord.equals(""))
            {
                ArrayList<String> words = (ArrayList<String>) session.getAttribute("words");

                words.add(newWord);              

                RequestDispatcher rd = getServletContext().getRequestDispatcher("/main.jsp");
                request.setAttribute("message", "Word added");
                rd.forward(request, response);

            }
            else
            {
                RequestDispatcher rd = getServletContext().getRequestDispatcher("/main.jsp");
                request.setAttribute("message", "Word is required");
                rd.forward(request, response);
            }
        }

It's a logical mistake .. 这是一个逻辑错误。

You are running servlet WordOperations.java twice in editing the word and passing edit parameter once and passing userChange parameter in another and NOT together which would solve the problem. 您正在运行servlet WordOperations.java 两次 ,这是为了编辑该单词,一次传递edit参数,然后一次传递userChange参数,而不是一起传递userChange参数,这将解决问题。

Here is the mistake. 这是错误。

But first set the Arraylist words in session on adding of first word. 但是,首先设置ArrayList的words在会议上就加入第一个字的。 You can do like this: 您可以这样:

else //logging in
            {
                if (newWord != null && !newWord.equals("")) {
                    ArrayList<String> words = (ArrayList<String>) s.getAttribute("words");
                    if (words == null) {
                        words = new ArrayList<>();
                        words.add(newWord);
                        s.setAttribute("words", words);
                    } else {
                        words.add(newWord);
                    }

                    RequestDispatcher rd = getServletContext().getRequestDispatcher("/main.jsp");
                    request.setAttribute("message", "Word added");
                    rd.forward(request, response);

                } 

Now Here is the mistake : 现在这是一个错误:

When the Arraylist words is listed down in main.jsp . Arraylist wordsmain.jsp列出时。 It contains 它包含

word Delete_link Edit_link Word Delete_link Edit_link

Now Delete_link > <a href='WordOperations?delete=" + i + "'>Delete</a> is working fine because you are just redirecting it to WordsOperations.java with parameter delete = i and deleting the word from ArrayList. 现在Delete_link> <a href='WordOperations?delete=" + i + "'>Delete</a>可以正常工作,因为您只是使用参数delete = i将其重定向到WordsOperations.java并从ArrayList中删除了该单词。

WordsOperations.java WordsOperations.java

String delete = request.getParameter("delete");

 if (delete!=null) //delete a word
        {
            int delIndex = Integer.parseInt(delete);

            ArrayList<String> words = (ArrayList<String>) session.getAttribute("words");
            words.remove(delIndex);

            RequestDispatcher rd = getServletContext().getRequestDispatcher("/main.jsp");
            request.setAttribute("message", "Word deleted");
            rd.forward(request, response);  
        }

BUT

In Edit_link > <a href='WordOperations?edit=" + i + "'>Edit</a> You are calling servlet WordsOperations.java with just parameter edit = i and NOT passing userChange parameter with it. 在EDIT_LINK> <a href='WordOperations?edit=" + i + "'>Edit</a>要调用servlet的WordsOperations.java只有参数edit = i通过userChange参数吧。

Thus newLink is NULL on 因此, newLink在上为NULL

String newLink = request.getParameter("userChange");

SO the code in WordsOperation.java ie 因此, WordsOperation.java的代码即

else if(edit !=null)
        {   


            int edIndex = Integer.parseInt(edit);
            String link = newLink;
            ArrayList<String> words = (ArrayList<String>) session.getAttribute("words");
            words.set(edIndex,"google");
            words.add(newLink);
                         RequestDispatcher rd = getServletContext().getRequestDispatcher("/edit.jsp");


            request.setAttribute("message", "Word edited");
            rd.forward(request, response);

        }

Will get executed as edit!=null is true . 将被执行为edit!=nulltrue But it will add newLink in Arraylist words which happens to be NULL. 但是它将在Arraylist words添加恰好为NULL的newLink Hence null is getting added to your list. 因此,会将null添加到您的列表中。

There is no problem with Arraylist.set() method. Arraylist.set()方法没有问题。

Now in the above code you are calling edit.jsp after adding newLink . 现在,在上面的代码中,您在添加newLink之后调用了edit.jsp But when WordsOperation.java gets called from edit.jsp form, it is just passing userChange parameter and NOT edit parameter which is needed to be there to execute the else if(edit!=null) block of servlet WordsOperations.java . 但是,当从edit.jsp形式调用WordsOperation.java时,它只是传递了userChange参数,而没有传递执行Servlet WordsOperations.javaelse if(edit!=null)块所需的edit参数。

<form action="WordOperations?" method="POST">
            New Link:<input type="text" name="userChange">
            <input type="submit" name="userChangePassword" value="Change link">
        </form>

Rest is on you. 休息就在你身上。 Just take care you pass edit and UserChange parameter both to Servlet WordsOperations.java to get your work done. 只需注意将edit和UserChange参数都传递给Servlet WordsOperations.java即可完成工作。

Suggestion: Pass edit parameter to edit.jsp and then pass edit and UserChange parameter to WordsOperation.java . 建议:将edit参数传递给edit.jsp ,然后将edit and UserChange参数传递给WordsOperation.java

Hope it helps. 希望能帮助到你。

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

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