0

I'm trying to get certain values out of my Database by a web application via JSP. But it always gives me an Internal Server Fail because of a NullpointerException. These are my files:

  1. File:

     <%@ page import="java.sql.*" %>
      <%@ page contentType="text/html" pageEncoding="UTF-8"%>
      <% Class.forName("com.mysql.jdbc.Driver"); %>
     <!DOCTYPE html>
       <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Webformular</title>
         </head>
        <body>
    <h1>Webformular</h1>
     <%!
      public class Actor {
        String url = "jdbc:mysql://localhost:3306/sakila";
        String Benutzername = "root";
        String Passwort = "";
    
        Connection con = null;
        PreparedStatement selectActors = null;
        ResultSet resultSet = null;
    
        public Actor(){
            try {
    
    
            con = DriverManager.getConnection(url, Benutzername, Passwort);
            selectActors = con.prepareStatement("Select a.first_name,a.last_name, c.title"
            + "FROM actor a, film_actor b, film c"
            +"WHERE a.first_name= ?"
            +"AND a.last_name = ?"
            + "AND a.actor_id = b.actor_id"
            +"AND b.film_id = c.film_id");
             } catch (SQLException e){
                 e.printStackTrace();
             }
        }
    
        public ResultSet getActors(String first, String last){
    
            try {
                selectActors.setString(1,first);
                selectActors.setString(2,last);
             resultSet = selectActors.executeQuery();
            } catch (SQLException e){
                e.printStackTrace();
            }
    
    
        return resultSet;
    }}
    %>
    <%
    String firstName = new String();
    String lastName = new String();
    
    if (request.getParameter("first") !=null) {
        firstName = request.getParameter("first");
    
    } 
     if (request.getParameter("last") !=null) {
        lastName = request.getParameter("last");
    
    } 
    
     Actor actor = new Actor();
     ResultSet actors = actor.getActors(firstName, lastName);
    
    %>
    <table border="1">
    
        <tbody>
            <tr>
                <td>First Name</td>
                <td>Last Name</td>
                <td>Title</td>
            </tr>
            <%while(actors.next()) { %>
            <tr>
                <td><%= actors.getString("first_name") %></td>
                <td><%= actors.getString("last_name") %></td>
                <td><%= actors.getString("title") %></td>
            </tr>
            <% } %>
        </tbody>
    </table>
    
    
      </body>
     </html>
    
  2. File:

      <%@page contentType="text/html" pageEncoding="UTF-8"%>
          <!DOCTYPE html>
      <html>
       <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Spezifische Daten auswerten</title>
       </head>
           <body>
             <h1>Spezifische Daten auswerten</h1>
          <form name="Formular" action="anzeige.jsp" method="POST">
    
    <table border="1">
    
        <tbody>
            <tr>
                <td>First Name:</td>
                <td><input type="text" name="first" value="" size="50" /></td>
            </tr>
            <tr>
                <td>Last Name:</td>
                <td><input type="text" name="last" value="" size="50" /></td>
            </tr>
        </tbody>
    </table>
    
    <input type="submit" value="Weiter" name="weiter" />
    <input type="reset" value="Löschen" name="löschen" />
          </form>
      </body>
      </html>
    

Does anyone has encountered that Problem before and might help me? I already did some Research and fixed certain bugs but nothing changed in the deployment. My first File always deploys but whenever i want to retrieve the data the NullpointerException appears.

  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – AxelH Nov 30 '16 at 14:20
  • _Internal Server Fail because of a NullpointerException_ And where is the stacktrace ? Something tell me the constructor of Actor catch an Exception that you simply ignore ... you should not work like this, don't store a Resultset into a instance, declare it, read it, close it. – AxelH Nov 30 '16 at 14:24

1 Answers1

0

Your SQL is concatenating the AND operators without a space before

+"WHERE a.first_name= ?"+"AND a.last_name = ?"+"AND a.actor_id = b.actor_id"

This will probably cause the prepareStatement() or executeQuery() to fail, but your JSP will continue executing because your are catching the exception and printing the stack trace to your web server log.

So afterwards, either selectActors or resultset will be null and you'll get a NullPointerException as soon as you try to use them.

So: add try adding a space after the question marks in your SQL.

Serg M Ten
  • 5,568
  • 4
  • 25
  • 48
  • thank you so much! that was a tricky mistake but nice to know for future Events. Next time i'll try to sout something between the catch so i see what happens :) –  Nov 30 '16 at 14:36