Buenas chicos tengo este error al colocar una imagen en netbeans 11.3, incluso me sale el mismo error al intentar hacer con las opciones de netbeans, solo me da el problema al colocar una imagen, si no hay ni una sola imagen el codigo se ejecuta todo correctamente.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Librerias.ImagenFondo.<init>(ImagenFondo.java:19)
at Presentacion.MDIPrincipal.<init>(MDIPrincipal.java:22)
at Presentacion.MDIPrincipal$1.run(MDIPrincipal.java:118)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Este es mi cogido en clase principal:
public MDIPrincipal() {
initComponents();
desktopPrincipal.setBorder(new ImagenFondo());
}
Este es mi codigo de clase ImagenFondo:
package Librerias;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.border.Border;
public class ImagenFondo implements Border{
public BufferedImage back;
public ImagenFondo(){
try {
URL imagePath = new URL(getClass().getResource("../Imagenes/bg.jpg").toString());
back = ImageIO.read(imagePath);
} catch (IOException ex) {
}
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
g.drawImage(back, (x + (width - back.getWidth())/2),(y + (height - back.getHeight())/2), null);
}
public Insets getBorderInsets(Component c) {
return new Insets(0,0,0,0);
}
public boolean isBorderOpaque() {
return false;
}
}
La linea 19 de ImagenFondo es:
URL imagePath = new URL(getClass().getResource("../Imagenes/bg.jpg").toString());
Mis imagenes las tengo en un package Imagenes, y el MDIPrincipal en un package Presentación