我正在尝试用Java开发图像查看器,我的代码几乎适用于所有格式,但是现在渲染图像时,我正在使用下一个代码:
private ImageIcon scaleImage(int indice, int w, int h, boolean thumbnail) {
ImageIcon image = null;
ImageIO.setUseCache(false);
if(fileRefernces.get(indice).toLowerCase().endsWith("tif") || fileRefernces.get(indice).toLowerCase().endsWith("tiff") ) {
File initialImage = new File(fileRefernces.get(indice));
try {
BufferedImage bImage = ImageIO.read(initialImage);
image = new ImageIcon(bImage);
bImage.flush();
bImage = null;
} catch (IOException ex) {
//Logger.getLogger(ImageRender.class.getName()).log(Level.SEVERE, null, ex);
}
initialImage = null;
} else {
image = new ImageIcon(fileRefernces.get(indice));
}
int nw = image.getIconWidth();
int nh = image.getIconHeight();
if(nw > w) {
nw = w;
nh = (nw * image.getIconHeight()) / image.getIconWidth();
}
if(nh > h) {
nh = h;
nw = (image.getIconWidth() * nh) / image.getIconHeight();
}
System.gc();
return (thumbnail)
? new ImageIcon(image.getImage().getScaledInstance(nw, nh, Image.SCALE_FAST ))
: new ImageIcon(image.getImage().getScaledInstance(nw, nh, Image.DEFAULT));
}
但是,我在运行时遇到下一个错误。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at com.bog.company.render.ImageRender.scaleImage(ImageRender.java:815)
at com.bog.company.render.ImageRender.lambda$CargarDocumento$2(ImageRender.java:700)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source) Exception in thread "Thread-6" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at com.bog.company.render.ImageRender.scaleImage(ImageRender.java:815)
at com.bog.company.render.ImageRender.CompletarMiniaturas(ImageRender.java:752)
at com.bog.company.render.ImageRender.lambda$CargarDocumento$3(ImageRender.java:716)
at java.lang.Thread.run(Unknown Source) Exception in thread "AWT-EventQueue-0" Exception in thread "Thread-9" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at com.bog.company.render.ImageRender.scaleImage(ImageRender.java:815)
at com.bog.company.render.ImageRender.lambda$CargarDocumento$2(ImageRender.java:700)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source) java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at com.bog.company.render.ImageRender.scaleImage(ImageRender.java:815)
at com.bog.company.render.ImageRender.CompletarMiniaturas(ImageRender.java:752)
at com.bog.company.render.ImageRender.lambda$CargarDocumento$3(ImageRender.java:716)
at java.lang.Thread.run(Unknown Source) Exception in thread "Thread-10" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at com.bog.company.render.ImageRender.scaleImage(ImageRender.java:815)
at com.bog.company.render.ImageRender.CompletarMiniaturas(ImageRender.java:752)
at com.bog.company.render.ImageRender.lambda$CargarDocumento$3(ImageRender.java:716)
at java.lang.Thread.run(Unknown Source) Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at com.bog.company.render.ImageRender.scaleImage(ImageRender.java:815)
at com.bog.company.render.ImageRender.lambda$CargarDocumento$2(ImageRender.java:700)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source) Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at com.bog.company.render.ImageRender.scaleImage(ImageRender.java:815)
at com.bog.company.render.ImageRender.lambda$CargarDocumento$2(ImageRender.java:700)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source) Exception in thread "Thread-11" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at com.bog.company.render.ImageRender.scaleImage(ImageRender.java:815)
at com.bog.company.render.ImageRender.CompletarMiniaturas(ImageRender.java:752)
at com.bog.company.render.ImageRender.lambda$CargarDocumento$3(ImageRender.java:716)
at java.lang.Thread.run(Unknown Source)
但是,这发生在tif图像上。有人对如何解决有想法?