0
I’m doing some academic work. and I’m not listing the BD data, that is giving the error java.lang.Nullpointerexception I searched something similar found , I saw some examples however the way they use and creating the same attributes within the class Products, however I can not use this way
My Classes
Java brand.
public class Marca {
private long id;
private String nome;
public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}
public String getNome() {
    return nome;
}
public void setNome(String nome) {
    this.nome = nome;
}
}
Product Class.
public class Produto {
private int id;
private String nome;
private double preco;
private Marca marca;
public Produto(String nome, Marca marca) {
    this.nome = nome;
    this.marca = marca;
}
public Produto() {
}
public long getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getNome() {
    return nome;
}
public void setNome(String nome) {
    this.nome = nome;
}
public Marca getMarca() {
    return marca;
}
public void setMarca(Marca marca) {
    this.marca = marca;
}
}
Productive
 public List<Produto> consultarTodos() throws ClassNotFoundException, SQLException {
    Connection con = Conexao.getConnection();
    List<Produto> todosProdutos = new ArrayList<>();
    try (
            PreparedStatement ps = con.prepareStatement("SELECT a.nome,b.idProdutos,b.nomeProdutos FROM marca  as A , produtos  as B where a.id = b.idmarca")) {
        try (ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                Produto p = new Produto();
                p.setId(rs.getInt("idProdutos"));
                p.getMarca().setNome(rs.getString("nome"));
                todosProdutos.add(p);
            }
        } catch (SQLException ex) {
            System.out.println("Erro: " + ex.getMessage());
            // ATENÇÃO: Comer exceções só dando um System.out.println nelas é uma má prática de programação!
        }
        return todosProdutos;
    }
}
Finally the JSP
       <c:forEach var="prod" items="${produtos}" >
                <tbody>
                    <tr>
                <ul class="list-group">
                    <th><li class="list-group-item">${prod.nome}</li></th>
                    <th><li class="list-group-item">${prod.marca.nome}</li></th>
                </ul>
                <th><td><input type="button" value="Atualizar" onclick="javascript: setIDAtualizar(${prod.id});"></td></th>
                <th><td><input type="button" value="Excluir" onclick="javascript: setIDExclusao(${prod.id});"></td></th>
                </tr>
                </tbody>
            </form>
        </thead>
    </c:forEach>
And the Second Generation
    ProdutoDAO dao = new ProdutoDAO();
    List<Produto> produto = null;
    try {
        produto = dao.consultarTodos();
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(BuscarProdutosController.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(BuscarProdutosController.class.getName()).log(Level.SEVERE, null, ex);
    }
    request.setAttribute("produtos", produto);
    request.getRequestDispatcher("/Controle/buscar/listaProdutos.jsp").forward(request, response);
}}
						
It worked, I’ll test it here. Thank you very much
– Somdesp