after my wits end i am posting question here. i am getting "java.lang.NullPointerException" error while developing a small JSP web application. Please help me to fix it.
Here below i mention my code:
<body>
<%!
public class Actor{
String URL = "jdbc:mysql://localhost:3306/henry_books";
String USERNAME = "root";
String PASSWORD = "adminroot";
Connection connection = null;
PreparedStatement selectActors = null;
ResultSet resultSet = null;
public Actor(){
try{
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
selectActors = connection.prepareStatement("SELECT * FROM author");
}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();
while(actors.next()){
%>
<span><%= actors.getInt("authorNum")%></span>
<% }
%>
</body>
Getting Error on ResultSet actors = actor.getActors(); line Error:
org.apache.jasper.JasperException:
An exception occurred processing JSP page /book_app/web/index.jsp at line 48
45: %>
46: <%
47: Actor actor = new Actor();
48: ResultSet actors = actor.getActors();
49: while(actors.next()){
50: %>
51: <span><%= actors.getInt("authorNum")%></span>
Stacktrace:
root cause
java.lang.NullPointerException
org.apache.jsp.book_005fapp.web.index_jsp$Actor.getActors(index_jsp.java:39)
org.apache.jsp.book_005fapp.web.index_jsp._jspService(index_jsp.java:109)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
Add Driver class before establishing connection and also make sure you have added mysql-connector.jar file in your lib folder.
public Actor(){
try{
Class.forName("com.mysql.jdbc.Driver"); //Add this line
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
selectActors = connection.prepareStatement("SELECT * FROM author");
}catch(SQLException e){
e.printStackTrace();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.