Question:
PostgreSQL 9.4
- I run my
Noticias()
class. - Inside the
public static void main()
of theNoticias()
class, agetConexao()
method isgetConexao()
; - When I run
pagecontroller?=p=noticias
in the browser itpagecontroller?=p=noticias
mejava.lang.NullPointerException
. - When I run the
Noticias()
class in Eclipse it doesn't return a null pointer and connects to the database without any error.
Obs.: Inside the WebContent/WEB-INF/lib/ directory I have the lib postgresql-9.4-1201.jdbc4.jar but even so when I use my Noticias()
class by the browser, the connection with the database is not made. data, my Tomcat server is running by 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);
}
}
Here is the Noticias()
class:
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;
}
Answer:
Check that the driver .jar
is in the /WEB-INF/lib
.
Are you making pagecontroller?=p=noticias
?
It has 2 equals in this parameter, which is totally mocking. Knife
pagecontroller?p=noticias
The nullpointer
seems to be because it doesn't find the getParameter("p")
then you do .equals
resulting in the error .
UPDATE
Add Class.forName("org.postgresql.Driver");
before your connection:
try {
Class.forName("org.postgresql.Driver");
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!");
}