java.lang.nullpointerexception when loading table with images

0

I am in a dead end and I need help, my mistake is as follows. When loading a table with images from my BD data% MySQL I get it

  

java.lang.nullpointerexception

and I do not know what its cause or motive is. The method I use to load my table is:

private void cargarTabla(){
        try {
            Connection miComando = AdministradorConfiguracion.obtenerComandoMySql();
            CallableStatement obtenerProductos = miComando.prepareCall("call obtener_productos()");
            ResultSet rs = obtenerProductos.executeQuery();
            //Instanciamos la clase "ImagenTabla" para renderizarla e mostrar las imagenes.
            jTablaProducto.setDefaultRenderer(Object.class, new ImagenTabla());
            ResultSetMetaData rsmd = rs.getMetaData();
            String Titulo[] = {"Nro", "Producto", "Stock", "Precio Costo", "Precio Venta", "Marca", "Descripción", "Categoria", "Proveedor", "Fecha Vecimiento",
            "Meses Restantes", "Dias Restantes", "Imagen"};
            Object[] fila = new Object[rsmd.getColumnCount()];
            modelo = new DefaultTableModel(null, Titulo);
            while (rs.next()) {                
                fila[0] = rs.getInt("Nro");
                fila[1] = rs.getString("Producto");
                fila[2] = rs.getInt("Stock");
                fila[3] = rs.getDouble("Precio Costo");
                fila[4] = rs.getDouble("Precio Venta");
                fila[5] = rs.getString("Marca");
                fila[6] = rs.getString("Descripción");
                fila[7] = rs.getString("Categoria");
                fila[8] = rs.getString("Proveedor");
                fila[9] = rs.getDate("Fecha Vecimiento");
                fila[10] = rs.getInt("Meses Restantes");
                fila[11] = rs.getInt("Dias Restantes");
                Blob blob = rs.getBlob(13);
                if (blob != null) {
                    byte[] data = blob.getBytes(1, (int)blob.length());
                    BufferedImage img = null;
                    try {
                        img = ImageIO.read(new ByteArrayInputStream(data));
                    } catch (Exception ex) {
                        System.out.println(ex.getMessage());
                    }
                    >>>ImageIcon icono = new ImageIcon(img);<<<
                    fila[12] = new JLabel(icono);
                }else{
                    fila[12] = "";
                }
                modelo.addRow(fila);
            }
            jTablaProducto.setModel(modelo);
            jTablaProducto.setRowHeight(64);
            ContarFilas();
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Error al intentar obtener los productos:\n"
                    + e, "Error en la operación", JOptionPane.ERROR_MESSAGE);
        }
    }

The nullpointerexception comes to me in the line of ImageIcon icono = new ImageIcon(img); I would greatly appreciate your help

    
asked by Gerardo Ferreyra 20.06.2017 в 03:26
source

1 answer

1

If all your pre-problem is the nullPointer. You can avoid it with a simple if. I include the exact code snippet

if(img !=null){
    ImageIcon icono = new ImageIcon(img);
    fila[12] = new JLabel(icono);
}else{
    fila[12] = "";
}

As you have an error trace, you will be able to know when the image is not generated by an error. To happen that blob is not null.

    
answered by 20.06.2017 / 10:11
source