2014-02-10 4 views
0

Вот мой код:NullPointerException, при чтении HTML ввод

страница JSP

<%@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  
} 

Я получаю NullPointerException, когда я запустить это приложение. Ниже приводится полный ошибка

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

Line 40 является

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

Что не так с этим кодом?

+1

В 'id' атрибуты элементов, на который ссылается сценарий клиента как JavaScript. Атрибуты 'name' поставляются в виде параметров запроса. Следовательно, они доступны на сервере. – Tiny

ответ

3

Используйте name атрибут вместо атрибута id

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

и

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

Read: Introduction to forms

+0

Спасибо. Это сработало! –

3

Чтобы иметь возможность доступа к нему в качестве параметра, 'словник' должен быть указанный как «имя» - не как значение:

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

Кроме того, убедитесь, что вы проверяете поле, чтобы проверить, является ли оно нулевым, прежде чем использовать его в остальной части кода.

+0

Благодарим вас за ответ. +1 от меня :) –

2

Я думаю, вам нужно указать имя Attr:

+0

Спасибо за ответ. +1 от меня :) –

Смежные вопросы