1

I have a file index.html where I can look up a Student (by name and surname). I use a Servlet, StudentInfo, as controller. The POST request on index.html brings me to overview.jsp (hardcoded Students are available). The GET method looks up a Student and give it to me in result.jsp

On result.jsp I have a GET that should bring me to overview.jsp, but I'm getting a NPE.

I tried to make it work before while using the doGet()-method, and broke it now.

My code is as follows:

Student class

package r0256160_Parameters2;

public class Student {
private String voornaam;
private String naam;
private int leeftijd;
private String richting;

public Student(String naam, String voornaam, int leeftijd, String richting){
    this.voornaam=voornaam;
    this.naam=naam;
    this.leeftijd=leeftijd;
    this.richting=richting;
}

public String getVoornaam(){
    return voornaam;
}
public String getNaam(){
    return naam;
}
public int getLeeftijd(){
    return leeftijd;
}
public String getRichting(){
    return richting;
}

public String toString(){
     return getVoornaam() + " " + getNaam() + " " + getLeeftijd() + " " + getRichting();
}

}

StudentService class

package r0256160_Parameters2;

import java.util.ArrayList;

public class StudentenService {
Student student1;
Student student2;
Student student3;
Student student4;
ArrayList<Student> lijst;

public StudentenService(){
    student1 = new Student("Jonckers", "Els", 23, "Toegepaste Informatica");
    student2 = new Student("Meerhout", "Maarten", 21, "Chemie");
    student3 = new Student("Steegmans", "Elke", 16, "Vroedkunde");
    student4 =  new Student("Leysen", "Lore", 54, "Verpleegkunde");

    lijst = new ArrayList<Student>();
    lijst.add(student1);
    lijst.add(student2);
    lijst.add(student3);
    lijst.add(student4);
}

public int getLeeftijd(String gegevenNaam, String gegevenVoornaam){
    for(int i=0; i<lijst.size(); i++){
        if(gegevenNaam.equalsIgnoreCase(lijst.get(i).getNaam()) && gegevenVoornaam.equalsIgnoreCase(lijst.get(i).getVoornaam())){
            return lijst.get(i).getLeeftijd();
        }
    }
    return 0;
}

public String getRichting(String gegevenNaam, String gegevenVoornaam) {
    for(int i=0; i<lijst.size(); i++){
        if(gegevenNaam.equalsIgnoreCase(lijst.get(i).getNaam()) && gegevenVoornaam.equalsIgnoreCase(lijst.get(i).getVoornaam())){
            return lijst.get(i).getRichting();
        }
    }
    return null;
}

public String toString(){
    for(Student s : lijst){
        s.toString();
    }
    return null;
}

public ArrayList<Student> getList(){
    return lijst;
}

}

Index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 <h1>Voeg een nieuwe student toe:</h1>
 <form method="POST" action="StudentInfo?action=add">
  <fieldset>
   <legend>Naam-gegevens</legend>
   <p>
    <label for="naam">Naam:</label> <input id="naam" name="naam"
     type=text placeholder="Familienaam van Student" required>
   </p>
   <p>
    <label for="voornaam">Voornaam:</label> <input id="voornaam"
     name="voornaam" type=text placeholder="Voornaam van Student"
     required>
   </p>
  </fieldset>
  <p>
   <label for="leeftijd">Leeftijd:</label> <input id="leeftijd"
    name="leeftijd" type=number min=0 max=99 placeholder="Leeftijd van Student"
    required>
  </p>
  <p>
   <label for="richting">Richting:</label> <input id="richting"
    name="richting" type=text placeholder="Richting van Student"
    required>
  </p>
  <input type="submit" value="Voer in">
 </form>

 <h1>Zoek een bestaande student op</h1>
 <form action="StudentInfo?action=find">
  <p>
   <label for="naam">Naam: </label> <input id="naam" name="naam"
    type=text required>
  </p>
  <p>
   <label for="voornaam">Voornaam: </label> <input id="voornaam"
    name="voornaam" required>
  </p>
  <p>
   <input type="submit" value="Zoek!">
  </p>
 </form>
</body>
</html>

Overview.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="r0256160_Parameters2.Student" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Lijst van alle studenten: </h1>
<%
ArrayList<Student> lijst = (ArrayList<Student>) request.getAttribute("lijst");

for(int i=0; i<lijst.size(); i++){
 out.println("<p>");
 out.println(lijst.get(i).toString());
 out.println("</p>");
}
%>

<a href="index.html">Ga naar index</a>
</body>
</html>

result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<p> Student <%= request.getParameter("naam") %> <%= request.getParameter("voornaam") %> is <%= request.getAttribute("leeftijd") %> jaar oud en studeert <%= request.getAttribute("richting") %> </p>
<!-- <a href="StudentInfo">Ga naar overview</a> -->
<a href="StudentInfo?action=read">Ga naar overview </a>


</body>
</html>

And finally, the Servlet StudentInfo

package r0256160_Parameters2;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class StudentInfo
 */
@WebServlet("/StudentInfo")
public class StudentInfo extends HttpServlet {
 private static final long serialVersionUID = 1L;
 public StudentenService service = new StudentenService();

 /**
  * @see HttpServlet#HttpServlet()
  */
 public StudentInfo() {
  super();
  // TODO Auto-generated constructor stub
 }

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
  *      response)
  */
 protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
  
  processRequest(request,response);
  // String naam = request.getParameter("naam");
  // String voornaam = request.getParameter("voornaam");

  // StudentenService service = new StudentenService();
  /*
   * Enumeration parameters = request.getParameterNames(); boolean metpara
   * = parameters.hasMoreElements();
   * 
   * if(metpara){
   * 
   * request.setAttribute("leeftijd", service.getLeeftijd(naam,
   * voornaam)); request.setAttribute("richting",
   * service.getRichting(naam, voornaam));
   * 
   * 
   * RequestDispatcher view = request.getRequestDispatcher("result.jsp");
   * view.forward(request, response);
   * 
   * } else {
   * 
   * request.setAttribute("lijst", service.getList()); RequestDispatcher
   * overview = request.getRequestDispatcher("overview.jsp");
   * overview.forward(request, response); }
   */
 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
  *      response)
  */
 protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
  
  processRequest(request,response);
  /*String naam = request.getParameter("naam");
  String voornaam = request.getParameter("voornaam");
  String strLeeftijd = request.getParameter("leeftijd");
  int leeftijd = Integer.parseInt(strLeeftijd);
  String richting = request.getParameter("richting");*/

  /*
   * if(naam == null || voornaam == null || strLeeftijd == null ||
   * richting == null){ RequestDispatcher view =
   * request.getRequestDispatcher("index.html"); view.forward(request,
   * response); }
   */

  /*service.lijst.add(new Student(naam, voornaam, leeftijd, richting));
  request.setAttribute("lijst", service.getList());
  RequestDispatcher overview = request
    .getRequestDispatcher("overview.jsp");
  overview.forward(request, response);*/
 }

 protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
  String destination = "index.html";
  String action = request.getParameter("action");
  // String naam = request.getParameter("naam");

  if (action.equals("read")) {
   destination = toonLijst(request, response);
  } else if (action.equals("find")) {
   destination = vindPersoon(request, response);
  } else if (action.equals("add")){
   destination = voegToe(request,response);
  }
  

  RequestDispatcher view = request.getRequestDispatcher(destination);
  view.forward(request, response);
 }

 private String toonLijst(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  request.setAttribute("lijst", service.getList());
  return "overview.jsp";
 }

 private String vindPersoon(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  String naam = request.getParameter("naam");
  String voornaam = request.getParameter("voornaam");
  
  request.setAttribute("leeftijd", service.getLeeftijd(naam, voornaam));
  request.setAttribute("richting", service.getRichting(naam, voornaam));

  return "result.jsp";
 }
 
 private String voegToe(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  String naam = request.getParameter("naam");
  String voornaam = request.getParameter("voornaam");
  String strLeeftijd = request.getParameter("leeftijd");
  int leeftijd = Integer.parseInt(strLeeftijd);
  String richting = request.getParameter("richting");
  
  service.lijst.add(new Student(naam, voornaam, leeftijd, richting));
  request.setAttribute("lijst", service.getList());
  
  return "overview.jsp";
 }

}

The code I used in the doGet() and doPost() are still there. Eclipse tells me that, in processRequest() (method in the servlet) the local variables action and destination are not used. I don't understand how, since I'm using them in my if-statements.

The error I get when browsing from result.jsp to overview.jsp (using StundentInfo?action="read" is:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
 r0256160_Parameters2.StudentenService.getLeeftijd(StudentenService.java:27)
 r0256160_Parameters2.StudentInfo.doGet(StudentInfo.java:42)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.47 logs.

I honestly don't understand what I am doing wrong.

Any help would be hugely appreciated.

Thank you

user3302053
  • 81
  • 2
  • 8
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it). Also, that looks like more code than strictly needed. Can you reduce the code further until [no code can be removed while still running into the problem](http://stackoverflow.com/help/mcve)? – Nathan Tuggy Jun 11 '15 at 01:52
  • You're not running the code you think you're running. Stack trace doesn't match code. – BalusC Jun 11 '15 at 07:40

0 Answers0