我正在尝试从数据库中获取BLOB并将其显示为图像,我搜索了类似的问题,但从未成功
Blob blob = rs.getBlob(12);
InputStream in = blob.getBinaryStream();
BufferedImage imagen = ImageIO.read(in);
Image imagenMonstruo = SwingFXUtils.toFXImage(imagen, null );
执行完此方法并执行方法后,我得到一个空指针异常,因为未加载Image,然后在构造函数中使用带有Image的对象的列表为null,因此我得到了null异常,但我的问题是是:
从BLOB格式的数据库中获取图像并将其转换为在应用程序中显示的图像的正确方法是什么?
@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);
}
}
我得到的日志错误是这个
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)
编辑
我能够解决崩溃问题,但仍然无法显示图像:
我的程序崩溃了,因为我正在使用导入java.awt.image并试图使其成为javafx图像,因此,基本上的解决方案是从Blob过渡到Image,并且程序不会崩溃并显示所有数据,但仍不显示图像。
Image imagenMonstruo = null;
Blob blob = rs.getBlob(12);
InputStream is = blob.getBinaryStream();
imagenMonstruo = new Image(is);
你能帮我吗?
答案 0 :(得分:0)
发布的代码可以正常工作,我的问题是存储在数据库中的图像是错误的,并且由于存储的图像无法显示,所以如果有人遇到与我相同的问题,我建议您需要转到数据库本身并在其中添加图片,然后查看图片的大小,因为从数据库添加的图片的大小为26 KB,而我尝试获取的图片为10B。