I have a pending problem of years without being able to solve regarding the execution of a small project (desktop) made in java with NetBeans. Generating the .jar
file and running it with the .bat
file gives me the following error message:
D:\01 Documentos\00 MI BIBLIOTECA\PROYECTOS PROG\NetbeansProyect\MateDiscreta\dist>java -jar MateDiscreta.jar
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.net.URL.toString()" because the return value of "java.lang.Class.getResource(String)" is null
at ClaseFondo.ImagenFondo.<init>(ImagenFondo.java:19)
at Interfaz.MenuMD.<init>(MenuMD.java:25)
at Interfaz.MenuMD$9.run(MenuMD.java:215)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
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)
That is to say, the program runs without problems from the IDE. Next, I will show you the general structure of the project:
As you can see, the file to be executed (main) is the MenuMD.java, which is the one that calls the other windows that are inside the Interfaz folder through the Programas button .
The following code is from MenuMD.java:
package Interfaz;
import ClaseFondo.ImagenFondo;
import java.net.URISyntaxException;
import javax.swing.ImageIcon;
public class MenuMD extends javax.swing.JFrame {
public MenuMD() {
initComponents();
//Allows to change the window icon
setIconImage(new ImageIcon(getClass().getResource("/Iconos/IconoMD.jpg")).getImage());
//The background class is called, to implement it on the desktop
escritorioMD.setBorder(new ImagenFondo());
//Allows you to maximize the form
this.setExtendedState(MenuMD.MAXIMIZED_BOTH);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
escritorioMD = new javax.swing.JDesktopPane();
menuherraMD = new javax.swing.JMenuBar();
menuprog = new javax.swing.JMenu();
jSeparator1 = new javax.swing.JPopupMenu.Separator();
jSeparator2 = new javax.swing.JPopupMenu.Separator();
menuinfnum = new javax.swing.JMenuItem();
jSeparator3 = new javax.swing.JPopupMenu.Separator();
menunumpri = new javax.swing.JMenuItem();
jSeparator4 = new javax.swing.JPopupMenu.Separator();
progmcdmcm = new javax.swing.JMenuItem();
jSeparator5 = new javax.swing.JPopupMenu.Separator();
menuecudio = new javax.swing.JMenuItem();
jSeparator6 = new javax.swing.JPopupMenu.Separator();
menusalir = new javax.swing.JMenuItem();
menucred = new javax.swing.JMenu();
menudatos = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
escritorioMD.setBackground(new java.awt.Color(0, 204, 204));
javax.swing.GroupLayout escritorioMDLayout = new javax.swing.GroupLayout(escritorioMD);
escritorioMD.setLayout(escritorioMDLayout);
escritorioMDLayout.setHorizontalGroup(
escritorioMDLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 744, Short.MAX_VALUE)
);
escritorioMDLayout.setVerticalGroup(
escritorioMDLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 221, Short.MAX_VALUE)
);
menuprog.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Iconos/System.png"))); // NOI18N
menuprog.setText("Programas");
menuprog.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuprogActionPerformed(evt);
}
});
menuprog.add(jSeparator1);
menuprog.add(jSeparator2);
menuinfnum.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Iconos/Search.png"))); // NOI18N
menuinfnum.setText("Info Número");
menuinfnum.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuinfnumActionPerformed(evt);
}
});
menuprog.add(menuinfnum);
menuprog.add(jSeparator3);
menunumpri.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Iconos/Undo.png"))); // NOI18N
menunumpri.setText("Generar Num. Primos");
menunumpri.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menunumpriActionPerformed(evt);
}
});
menuprog.add(menunumpri);
menuprog.add(jSeparator4);
progmcdmcm.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Iconos/Tip.png"))); // NOI18N
progmcdmcm.setText("MCD y MCM");
progmcdmcm.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
progmcdmcmActionPerformed(evt);
}
});
menuprog.add(progmcdmcm);
menuprog.add(jSeparator5);
menuecudio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Iconos/Stationery.png"))); // NOI18N
menuecudio.setText("Ecuaciones Diofánticas");
menuecudio.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuecudioActionPerformed(evt);
}
});
menuprog.add(menuecudio);
menuprog.add(jSeparator6);
menusalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Iconos/Close.png"))); // NOI18N
menusalir.setText("Salir");
menusalir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menusalirActionPerformed(evt);
}
});
menuprog.add(menusalir);
menuherraMD.add(menuprog);
menucred.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Iconos/user.png"))); // NOI18N
menucred.setText("Crédito");
menucred.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menucredActionPerformed(evt);
}
});
menudatos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Iconos/Info.png"))); // NOI18N
menudatos.setText("Datos del Programa");
menudatos.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menudatosActionPerformed(evt);
}
});
menucred.add(menudatos);
menuherraMD.add(menucred);
setJMenuBar(menuherraMD);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(escritorioMD)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(escritorioMD)
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void menucredActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menucredActionPerformed
}//GEN-LAST:event_menucredActionPerformed
private void menudatosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menudatosActionPerformed
Crédito cred = new Crédito();
escritorioMD.add(cred);
cred.setVisible(true);
}//GEN-LAST:event_menudatosActionPerformed
private void menuprogActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuprogActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_menuprogActionPerformed
private void menusalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menusalirActionPerformed
System.exit(0);
}//GEN-LAST:event_menusalirActionPerformed
private void menuecudioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuecudioActionPerformed
Diofanto dio=new Diofanto();
escritorioMD.add(dio);
dio.setVisible(true);
}//GEN-LAST:event_menuecudioActionPerformed
private void progmcdmcmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_progmcdmcmActionPerformed
McdMcm mm = new McdMcm();
escritorioMD.add(mm);
mm.setVisible(true);
}//GEN-LAST:event_progmcdmcmActionPerformed
private void menunumpriActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menunumpriActionPerformed
GenNumPrim gnp = new GenNumPrim();
escritorioMD.add(gnp);
gnp.setVisible(true);
}//GEN-LAST:event_menunumpriActionPerformed
private void menuinfnumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuinfnumActionPerformed
NatNum inf = new NatNum();
escritorioMD.add(inf);
inf.setVisible(true);
}//GEN-LAST:event_menuinfnumActionPerformed
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MenuMD().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JDesktopPane escritorioMD;
private javax.swing.JPopupMenu.Separator jSeparator1;
private javax.swing.JPopupMenu.Separator jSeparator2;
private javax.swing.JPopupMenu.Separator jSeparator3;
private javax.swing.JPopupMenu.Separator jSeparator4;
private javax.swing.JPopupMenu.Separator jSeparator5;
private javax.swing.JPopupMenu.Separator jSeparator6;
private javax.swing.JMenu menucred;
private javax.swing.JMenuItem menudatos;
private javax.swing.JMenuItem menuecudio;
private javax.swing.JMenuBar menuherraMD;
private javax.swing.JMenuItem menuinfnum;
private javax.swing.JMenuItem menunumpri;
private javax.swing.JMenu menuprog;
private javax.swing.JMenuItem menusalir;
private javax.swing.JMenuItem progmcdmcm;
// End of variables declaration//GEN-END:variables
}
And then the code for the ImagenFondo.java
class:
package ClaseFondo;
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("../Iconos/FondoUnacFIIS.jpg").toString());
back = ImageIO.read(imagePath);
} catch (IOException ex) {
}
}
@Override
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);
}
@Override
public Insets getBorderInsets(Component c) {
return new Insets(0,0,0,0);
}
@Override
public boolean isBorderOpaque() {
return false;
}
}
Believe me that I have tried in every possible way with information from the network and with similar cases exposed on this platform, but without success. I don't know how or where to apply the obtaining of the path of the resources that the execution of the .jar mentions when executing it.
It would be very helpful, since it would allow me to close and complete this project that I have pending since university.
Thank you.