3
Postgresql 9.4
- I execute my class
Noticias()
. - Within the method
public static void main()
classNoticias()
a method is calledgetConexao()
; - When I execute
pagecontroller?=p=noticias
in the browser it returns to mejava.lang.NullPointerException
. - When I run the class
Noticias()
in Eclipse it does not return a null pointer and makes connection to the database without any error.
Obs.: Inside the directory Webcontent/WEB-INF/lib/ I am with the lib postgresql-9.4-1201.jdbc4.jar but still when I use my class Noticias()
by the browser is not connected to the database, my Tomcat server is running through the Eclipse IDE.
public class PageController extends HttpServlet {
private static final long serialVersionUID = 1L;
public PageController() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Chamou o metodo GET");
String pagina = request.getParameter("p");
if (pagina.equals("noticias")){
Noticias noticias = new Noticias();
List<Noticias> lista = noticias.getNoticias();
request.setAttribute("noticias", lista);
RequestDispatcher saida = request.getRequestDispatcher("noticias.jsp");
saida.forward(request, response);
}
}
Follows class Noticias()
:
public class Noticias{
int grid;
String topico;
String conteudo;
int usuario;
Date data;
public Noticias(){
}
public static void main(String[] args) {
List<Noticias> noticias = getNoticias();
}
public static List<Noticias> getNoticias(){
List<Noticias> noticia_list = new ArrayList<Noticias>();
String sql = "SELECT * from NOTICIAS";
Connection conexao = null;
try {
conexao = DriverManager.getConnection("jdbc:postgresql://localhost:5432/base", "postgres", "postgres");
System.out.print("Conexao com o banco de dados efetuada com sucesso!");
} catch (SQLException ex) {
Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Erro durante a conexao com o banco de dados!");
}
PreparedStatement prepared_sql = null;
try {
prepared_sql = conexao.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
try {
ResultSet result = prepared_sql.executeQuery();
while(result.next()){
Noticias noticia = new Noticias();
noticia.setGrid(result.getInt("grid"));
noticia.setTopico(result.getString("topico"));
noticia.setConteudo(result.getString("conteudo"));
noticia.setUsuario(result.getInt("usuario"));
noticia.setData(result.getDate("data"));
noticia_list.add(noticia);
}
} catch (SQLException e) {
e.printStackTrace();
}
return noticia_list;
}
Include the log with the exception as well, but it’s probably why you create connection on
main
, it won’t run if you don’t call it or run it as a java application. I don’t know what your application looks like, but try callinggetConexao()
ingetNoticias()
– Bruno César
But you have made the postgree jar deploy on your Tomcat?
– Edgar Muniz Berlinck
I’ll do what you said @Brunswick to see if it works.
– Luiz Ricardo Cardoso
@Edgarmunizberlinck did not deploy, I will check this too.
– Luiz Ricardo Cardoso
@Brunswick put it all in one method called
getNoticias()
, within this method he tries to make the connection, but without success!– Luiz Ricardo Cardoso
Include the stack trace, makes it easier to help.
– Bruno César
@Brunocésar thanks for trying to help, the problem has been solved... thank you!
– Luiz Ricardo Cardoso