M
M
MarkusEfr2018-09-12 13:49:05
Java
MarkusEfr, 2018-09-12 13:49:05

How to resolve HTTP Status 500, "org.apache.jasper.JasperException: java.lang.NullPointerException" in glassfish web application?

I am making an educational web application "Library". There are 2 jsp pages: index - input, main - main (the list of authors from the database should be displayed here) main.jsp

<%@ page import="app.AuthorList" %>
<%@ page import="app.Author" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Inside</title>
  <style type="text/css">
   #footer {
    position: fixed; /* Фиксированное положение */
    left: 0; bottom: 0; /* Левый нижний угол */
    padding: 10px; /* Поля вокруг текста */
    background: LightSeaGreen; /* Цвет фона */
    color: #fff; /* Цвет текста */
    width: 100%; /* Ширина слоя */
   }
  </style>
</head>
<body>
<div class="text-center">
<img src="images1.jpg" class="img-thumbnail"  alt="Responsive image">
</div>
<div class="jumbotron">
  <h1 class="text-info">Librus</h1>
  <p>You can find some books here.</p>
  <% AuthorList authorList = new AuthorList();
        for (Author author : authorList.getAuthorList()) {
        	%>
        	<li><a href="#"> <%=author.getFull_name() %></a></li>
      <% }%>  
</div>
  <div id="footer" class="text-center">
   &copy; Markus Efr
  </div>
</body>
</html>

PostgreSQL contains a project database, Glassfish has a connection pool and a resource configured to use it. In the lib folder I placed the driver for postresql 5b98ed8a6462a059536928.png5b98ed935d29e203060835.pngAlso 3 java classes Author - the essence of the object,
package app;

public class Author {

  public Author() {
    
  }
  
  private int auth_id; 
  private String full_name;
  
  public Author (int auth_id, String full_name) {
    this.auth_id=auth_id; 
    this.full_name=full_name;
  }

  public int getAuth_id() {
    return auth_id;
  }

  public void setAuth_id(int auth_id) {
    this.auth_id = auth_id;
  }

  public String getFull_name() {
    return full_name;
  }

  public void setFull_name(String full_name) {
    this.full_name = full_name;
  }
  
}
AuthorList - executes a query and populates the list with entities from the database.
package app;
import app.Connector;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class AuthorList {

  private ArrayList<Author> authorList = new ArrayList<Author>();
  
  private ArrayList<Author> getAuthors () {
    try {
      Connection con = Connector.getConnection();
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("select * from author");
        while (rs.next()) {
        	Author author = new Author();
        	author.setFull_name(rs.getString("full_name"));
        	authorList.add(author);
        }		    
    } catch (SQLException e) {
    Logger.getLogger(AuthorList.class.getName()).log(Level.SEVERE, "Error", e);
    }
      return authorList;
  }	  

     public ArrayList<Author> getAuthorList() {
          if (!authorList.isEmpty()) {
        	  return authorList;
          } else {
        	  return getAuthors();
          }
     }  	 
     }

Connector contains settings for using the database resource.
package app;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class Connector {

  private static Connection con;
  private static InitialContext ic;
  private static DataSource ds;
  
  public static Connection getConnection()  {
 
    try {		
    ic = new InitialContext();
    ds = (DataSource) ic.lookup("jdbc/library");
   if (con==null) {
     con = ds.getConnection();
   }
    } catch (SQLException e) {
Logger.getLogger(AuthorList.class.getName()).log(Level.SEVERE, "Error", e);
    } catch (NamingException e) {
      Logger.getLogger(AuthorList.class.getName()).log(Level.SEVERE, "Error", e);
  }	
    return con;
  } 
}

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Librus</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
    <resource-ref>
    <description>JDBC Connection Pool</description>
    <res-ref-name>jdbc/library</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
  </resource-ref>
</web-app>

5b98eea8d31c1103838642.png
I would be grateful for understanding what went wrong. Where did NullPoint originate and how to exorcise it?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
MarkusEfr, 2018-09-15
@MarkusEfr

The problem is solved by installing the appropriate driver and settings for the connection pool. The glassfish log file helped in understanding.

K
krypt3r, 2015-03-24
@krypt3r

Why is the contact form not working?

Why are you asking us without providing error messages?
Killer "code". Have you ever wondered what the $phone and $name variables will contain if $_POST is empty?

C
Centrino, 2015-03-24
@Centrino

Do at least elementary checks in the code, catch errors. If something does not work, go to the logs or test the code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question