0

I am developing a comment forum that is people will comment and can see their comments after posting but i am getting null pointer exception in servlet.

index.jsp

<form action="Comment" method="post">

  <textarea style="resize: none;" cols="30" rows="3" id="myTextarea" name="myTextarea"></textarea>

</form>

Comment servlet

  try{
    String comment=request.getParameter("myTextarea");

       ArrayList al1=null;
        ArrayList emp_list =new ArrayList();
         al1.add(comment);       
         emp_list.add(al1);                 
         request.setAttribute("empList",emp_list);        
        String nextJSP = "/result.jsp";
       RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
      dispatcher.forward(request,response);
    }       
    catch(Exception e){       
    out.println("exception"+e);//exception coming 

    }

3 Answers 3

2

You al1 will be null when you try to add to it:

ArrayList al1=null;
ArrayList emp_list =new ArrayList();
al1.add(comment); 

Try changing the first line to:

ArrayList al1 = new ArrayList();

This will result in forwarding an array list inside an array list to your next jsp page. It might be simpler to just do:

ArrayList emp_list =new ArrayList();
emp_list.add(comment); 

...and remove the varialble al1 altogether.

Sign up to request clarification or add additional context in comments.

4 Comments

yes this solved the problem, i just need few help , how can i get this emp_list.add(comment) in result.jsp ? will i get multiple comments?
Your call to setAttribute should make it a request parameter you can access in result.jsp. No, as it is you will get one comment inside an array in the parameter empList.
ya i am getting only one comment and it is getting replaced if i am entering a different comment, how can i get multiple comments? please help me little bit
@sujit - A lot depends on the other parts of your code. Often, you would just write the comment to a database in this handler, and read a list from there for display inside your results page. Can you create another question with more explanation of the path the data takes?
1

Your al1 arraylist is instantiated to null! try

ArrayList<String> al1 = new ArrayList<String>();
al1.add(comment);

2 Comments

i have added string comment to al1 by al1.add(comment)
when you call al1.add(comment), you're not adding the string to the an arraylist object, you're adding your "comment" to "null" because you instantiated al1 = null;
0

Dude you are assigning the reference to a null. that is al1.add(), just assign it with the memory using new, it will solve your problem....... Enjoy........

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.