@MarkusEfr

Как устранить Status HTTP 500, «org.apache.jasper.JasperException: java.lang.NullPointerException» в web-приложении на glassfish?

Делаю учебное веб-приложение "Библиотека". Есть 2 jsp страницы: index - вход, main - главная (сюда должен выводится список авторов из БД) 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 содержит бд проекта, в Glassfish настроен пул соединения и ресурс для его использования. В папку lib поместил драйвер для postresql 5b98ed8a6462a059536928.png5b98ed935d29e203060835.png Также 3 java класса Author - сущность объекта,
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 - выполняет запрос и наполняет список сущностями из БД.
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 содержит настойки для использования ресурса БД.
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
Буду благодарен за понимание что пошло не так. Где возник NullPoint и как его изгнать ?
  • Вопрос задан
  • 352 просмотра
Решения вопроса 1
@MarkusEfr Автор вопроса
Проблема решена установкой подходящего драйвера и настроек для пула соединений. В понимании помог лог файл glassfish.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
Bell Integrator Ульяновск
До 400 000 ₽
Bell Integrator Хабаровск
До 400 000 ₽
Bell Integrator Ижевск
До 400 000 ₽
19 апр. 2024, в 03:01
1000 руб./за проект
18 апр. 2024, в 21:56
2000 руб./за проект
18 апр. 2024, в 21:00
150 руб./за проект