-1

I am beginner in using JSP and have a problem with the java.lang.NullPointerException. The problem occurs if I use the following part (the full source is below). If i delete the source the page is loading without any returns. I would be happy if anyone helped me to run the code.

Background information. What should the application do:

This code should perform a SQL SELECT Statement and provide the information which is stored in the MySQL Database.

{ <%@page import="java.sql.*"%>
<% Class.forName("com.mysql.jdbc.Driver");%>
<%@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>Hello World!</h1>

    <%!

    public class Actor{

        String URL = "jdbc:mysql://localhost:3306/sakila";
        String USERNAME = "root";
        String PASSWORD = "admin";

        Connection connection = null;
        PreparedStatement selectActors = null;
        ResultSet resultSet = null;

        public Actor(){                
            try{                    
                connection = DriverManager.getConnection( 
     "jdbc:mysql://localhost:3306/sakila", "root", "MySQL2019.");

                selectActors = connection.prepareStatement(
                    "SELECT actor_id, first_name, last_name FROM actor");                    
            }catch (SQLException e){                    
                e.printStackTrace();
            }                
        }
            public ResultSet getActors(){                    
                try{                        
                    resultSet = selectActors.executeQuery();                        
                }catch (SQLException e){
                    e.printStackTrace();
                }
                return resultSet;
            }
        }
    %>

    <%
    Actor actor = new Actor();
    ResultSet actors = actor.getActors();
   %> 
<table border="1">
<tbody>
<tr>
<td>Actor ID</td>
<td>First Name</td>
<td>Last Name</td>
</tr>
<tr>
<% while (actors.next()) {%>
<td> <%= actors.getInt("actor_id")%>   </td>
<td><%= actors.getString("first_name")%> </td>
<td><%= actors.getString("last_name")%> </td>
</tr>
<%}%>
</tbody>
</table>

</body>
</html> }

Problem code:

{<%    
Actor actor = new Actor();
ResultSet actors = actor.getActors();
%> }
Tim Mironov
  • 849
  • 8
  • 22
Moki66
  • 3
  • 1

2 Answers2

2

A few words of advice:

  1. Don't use scriptlet code in JSPs.
  2. Learn JSP Standard Tag library (JSTL)
  3. Don't embed database code in model objects like Actor
  4. Externalize database configuration
  5. Decompose your problem into separate classes that are testable
  6. Create an Actor repository interface and write an implementation
  7. You should run the code and give error messages.

It's incorrect to expect that someone here would build, package, and run your app just to see what you get for an error.

Whatever book or tutorial is recommending that you use scriptlets, please throw it away and find something better. Scriptlets are a failed 1990s experiment that no one uses anymore.

public interface ActorRepository {
    List<Actor> findAll();
    List<Actor> findByName(String firstName, String lastName);
}
duffymo
  • 305,152
  • 44
  • 369
  • 561
0

Your exception handling is probably the cause. You're catching SqlException and do nothing with them.

So if you have an exception in the following code (like a DB connection error)

try{

                connection = DriverManager.getConnection( 
     "jdbc:mysql://localhost:3306/sakila", "root", "MySQL2019.");

                selectActors = connection.prepareStatement(
                    "SELECT actor_id, first_name, last_name FROM actor");

            }catch (SQLException e){

                e.printStackTrace();
            }

you'll continue with a null selectActors variable, which could be the cause of your NullPointerException later.

Thomas Martin
  • 678
  • 8
  • 19
  • This is the exception report which i see on my browser `HTTP Status 500 - Internal Server Error type Exception report messageInternal Server Error descriptionThe server encountered an internal error that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: java.lang.NullPointerException root cause java.lang.NullPointerException note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.0 logs. GlassFish Server Open Source Edition 4.0 ` – Moki66 Aug 21 '19 at 22:01
  • Thank you very much for your response. I will try to use another way of Exception handling :-) – Moki66 Aug 21 '19 at 22:05