How to get an BLOB from database and display it like an Image

I’m trying to get an BLOB from database and display it like an Image, I search similar questions but never works

Blob blob = rs.getBlob(12);
InputStream in = blob.getBinaryStream();  
BufferedImage imagen = ImageIO.read(in);
Image imagenMonstruo = SwingFXUtils.toFXImage(imagen, null );

After doing this and execute the methos I get a null pointer exception because the Image is not loaded and then the list that take the object with the Image in the constructor is null and because of that I get the null exception but my question is:

What is the correct way to take an image from a database in BLOB format and turning into an image that display in a application?

@Override
public List<MonstruoDTO> getMonstruos() {
    ArrayList<MonstruoDTO> lista = new ArrayList<MonstruoDTO>();
    con = ConexionBDA.getInstance().getCon();
    try {
        if(con!= null) {
            statement = con.createStatement();
        ResultSet rs=statement.executeQuery("SELECT * FROM monstruos");
            while(rs.next()){
                String nombre = rs.getString(2);
                String habitat = rs.getString(3);
                String estado = rs.getString(4);
                String colaCercenable = rs.getString(5);
                String dragonAnciano = rs.getString(6);
                int debilidadFuego = rs.getInt(7);
                int debilidadAgua = rs.getShort(8);
                int debilidadRayo = rs.getInt(9);
                int debilidadHielo = rs.getInt(10);
                int debilidadDraco = rs.getInt(11);
                Blob blob = rs.getBlob(12);
                InputStream in = blob.getBinaryStream();  
                BufferedImage imagen = ImageIO.read(in);
                Image imagenMonstruo = SwingFXUtils.toFXImage(imagen, null );
                MonstruoDTO monstruo = new MonstruoDTO(nombre, habitat, estado, colaCercenable, dragonAnciano, debilidadFuego, debilidadAgua, debilidadRayo, debilidadHielo, debilidadDraco, imagenMonstruo);
                lista.add(monstruo);
   }
}

And the log error i’ve got is this

java.lang.NullPointerException
at javafx.embed.swing.SwingFXUtils.toFXImage(SwingFXUtils.java:85)
at modelo.ImplementacionMonstruosDAO.getMonstruos(ImplementacionMonstruosDAO.java:57)
at controlador.ConsultarMonstruosController.obtenerListaMonstruos(ConsultarMonstruosController.java:163)
at controlador.ConsultarMonstruosController.initialize(ConsultarMonstruosController.java:83)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at controlador.VentanaMonstruosController.eventoBotonConsultar(VentanaMonstruosController.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)

EDIT

i was able to fix the crash but i still don’t get the image shown:

My program crashed because i was using the import java.awt.image and trying to make it a javafx image, so basically the solution is to past from a Blob to an Image and the program don’t crash and show all the data but still not showing the image.

Image imagenMonstruo = null;
                Blob blob = rs.getBlob(12);
                InputStream is = blob.getBinaryStream();
                imagenMonstruo = new Image(is);

Can you help me please?

Answer

The code posted is fine and works, the problem i had is that the image stored in my database was wrong, and because of that the image stored cannot be display, so if someone is having the same problem as me, I recomend you to go to the database itself and add a picture there, the take a look to the size of the picture because a picture added from the database had a size of 26 KB and the picture i was trying to get was 10B.

Leave a Reply

Your email address will not be published. Required fields are marked *