java.lang.NullPointerException to Load Image

0

Good morning everyone,

I have a java.lang.NullPointerException error when I try to upload a photo to a JLabel.

this is the following code based on the MVC pattern:

  • this is the final method that will be executed in the JFrame

    private void consultar() {
    PersonaVO usuarioVO = miCoordinador.consultarUsuario(txtConsultar.getText().trim());
    
    
    if (usuarioVO != null) {
        txtNombre.setText(usuarioVO.getNombre());
        txtDocumento.setText(usuarioVO.getDocumento());
        txtProfesion.setText(usuarioVO.getProfesion());
        txtEdad.setText(usuarioVO.getEdad() + "");
        txtTelefono.setText(usuarioVO.getTelefono());
        txtDireccion.setText(usuarioVO.getDireccion());
    
        InputStream is = miCoordinador.consultarImagen(usuarioVO);
        //se carga el inputStream en una variable ImageIcon
        ImageIcon imagen = Funciones.extraerImagen(is);
    
        //lo visualizamos en el label
        lblFoto.setIcon(imagen);
    
    
    } else {
        JOptionPane.showMessageDialog(null, "El usuario no se encuentra registrado en la base de datos!", "Advertencia", JOptionPane.WARNING_MESSAGE);
    }
    
    }//fin consultar
    
  • this is the method in the controller:

    public InputStream consultarImagen(PersonaVO img) {
    
    return miPDAO.consultarImagen(img);
    }
    
  • This is the method in the DAO:

    public InputStream consultarImagen(PersonaVO user){
    
    Connection conn = null;
    InputStream is=null;
    
    try{
        String sql="select foto from clientes where documento=?";
    
        PreparedStatement ps = conn.prepareStatement(sql);
    
        ps.setString(1, user.getDocumento());
    
        ResultSet rs = ps.executeQuery();
    
        while(rs.next()){
            is = rs.getBinaryStream("foto");
        }
        return is;
    }catch(Exception e){
        System.out.println("consultar imagen: "+e);
        return null;
     }
     }//consultarImagen*/
    
  • and the latter is the VO:

    private String documento;
    
    //insersion de la foto y consulta
    private FileInputStream Foto;
    private int fotoLongitud;
    
    //se realizaron dos contructores para poder colocar la imagen
    public PersonaVO() {
    }
    
    public PersonaVO(FileInputStream foto_familia, int foto_longitud, String documento) {
    this.documento = documento;
    this.Foto = foto_familia;
    this.fotoLongitud = foto_longitud;
    }//fin de la realizacion de insersion de la foto
    
    
    public FileInputStream getFoto() {
     return Foto;
    }
    
    public void setFoto(FileInputStream Foto) {
     this.Foto = Foto;
    }
    
    public int getFotoLongitud() {
     return fotoLongitud;
    }
    
    public void setFotoLongitud(int fotoLongitud) {
     this.fotoLongitud = fotoLongitud;
    }
    
    
    
    public String getDocumento() {
     return documento;
    }
    
    public void setDocumento(String documento) {
     this.documento = documento;
    }
    
  • I will be very grateful that you help me solve this problem.

        
    asked by Mirlino 30.05.2017 в 20:50
    source

    1 answer

    -1

    I was able to solve it thanks to gbianchi.

    was that it had the variable started with null; but I already corregi.

    Here is the answer to any NPE question:   What is the solution to all present, past, and future NullPointerException errors?

        
    answered by 31.05.2017 в 02:48