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.