I have an error when executing a method that returns the result of a query. The error he gives me is this:
'Warning: StandardWrapperValve [jsp]: Servlet.service () for servlet jsp threw exception java.lang.NullPointerException at
com.sun.common.util.logging.LoggingOutputStream $ LoggingPrintStream.println (Logg ingOutputStream.java:228) at
org.apache.felix.gogo.runtime.threadio.ThreadPrintStream.println (ThreadPrintStr eam.java:205) at
DAOSIMP.DaoPersonaImp.consultarAspirantesQuantity (DaoPersonaImp.java:833) '
I already checked the connection and it seems that everything is fine. Attached the connection:
package Conexion;
import java.sql.*;
public class Conexion {
protected Connection conexion;
protected final String URL_DB= "jdbc:mysql://localhost:80/nombre_base_de_datos";
protected final String JDBC_DRIVER ="com.mysql.jdbc.Driver";
protected final String CONSTRASENA_DB="contrasena";
protected final String USUARIO_DB="usuario_BD";
public Conexion() {
}
public Connection conexion(){
return this.conexion;
}
public void conectar(){
try{
conexion= DriverManager.getConnection(URL_DB, USUARIO_DB,CONSTRASENA_DB);
Class.forName(JDBC_DRIVER);
}catch (SQLException e){
e.getMessage();
}catch(ClassNotFoundException e){
System.out.println("Mensaje de error de base de datos" + e.getMessage());
}
}
public void desconectar(){
try{
if(conexion!=null){
if(conexion.isClosed()){
conexion.close();
}
}
}catch(SQLException e){
System.out.println(e.getMessage());
}
}
}
I enclose the class that extends the connection and the method in which I execute the query:
package DAOSIMP;
import Clases.Persona;
import Conexion.Conexion;
import DAOS.DaoPersona;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class DaoPersonaImp extends Conexion implements DaoPersona{
@Override
public long consultarCantidadAspirantes() {
ResultSet rs;
long cantidad=0;
try {
this.conectar();
String sql="SELECT COUNT(*) FROM persona WHERE tipo_persona='A';";
PreparedStatement stm=this.conexion.prepareStatement(sql);//Esta es la linea 833 que dice el error
rs = stm.executeQuery();
while(rs.next()){
cantidad=rs.getLong(1);
}
return cantidad;
}catch(SQLException e){
System.out.println(e.getMessage());
return 0;
}finally{
this.desconectar();
}
}
}
And I'm just calling the method like this:
DaoPersona daoPersona=new DaoPersonaImp();
long cantidadAspirantes=daoPersona.consultarCantidadAspirantes();