0

Here is my code:

JSP Page

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1><center>Web Site Analizer</center></h1>
        <br/>
        <form action=http://localhost:8080/WebSiteAnalizer/SiteAnalizer method=post>
            Enter the Percentage (0-100): <input type="Text" id="percentage">
            <br/><br/><br/>

            Enter the Words (Separated from New Line (/n)): <br/>
            <textarea id='wordList' value='wordList'></textarea>            
            <br/><br/>

            <input type="submit" value="Submit">

        </form>
    </body>
</html>

Servlet

public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
    String[] listOfWords = request.getParameter("wordList").toLowerCase().trim().split("\n"); //Get the List of words
    int percentage = Integer.parseInt(request.getParameter("percentage")); // Get the percentage value
    int numberOfWordsInProgramHash = 0; //Keep tracks of how many words in "program" per webpage
    int primaryKey = 0; //Store the primary key    
}

I get the NullPointerException, when I run this application. Below is the full error

java.lang.NullPointerException
    SiteAnalizer.doPost(SiteAnalizer.java:40)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

The Line 40 is

String[] listOfWords = request.getParameter("wordList").toLowerCase().trim().split("\n"); //Get the List of words

What is wrong with this code?

halfer
  • 19,824
  • 17
  • 99
  • 186
PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • 1
    The `id` attributes of elements are referenced by a client script like JavaScript. The `name` attributes are supplied as request parameters. Hence, they are available on the server. – Tiny Feb 10 '14 at 17:39

3 Answers3

3

Use the name attribute instead of the id attribute

<input type="Text" name="percentage">

and

<textarea name='wordList' value='wordList'>

Read: Introduction to forms

Reimeus
  • 158,255
  • 15
  • 216
  • 276
3

To be able to access it as a parameter, 'wordList' should be specified as the 'name' - not as the value:

<textarea id='wordList' name='wordList'></textarea>  

Also, make sure you validate the field to check if it is null, before you use it in the rest of the code.

HateScraperKavi
  • 188
  • 1
  • 7
2

I think you need to specify the name attr:

xcoder
  • 1,336
  • 2
  • 17
  • 44