I am trying to use a jLabel as a background but is not working. The code works as soon as I delete the label. I have the image within a package in the project separate to the form. I cannot tell if the issue is to do with the storage of the image or if there is an issue with the code.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.desktop/javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at com.mycompany.dungeoncrawler.UI2.initComponents(UI2.java:74)
at com.mycompany.dungeoncrawler.UI2.<init>(UI2.java:30)
at com.mycompany.dungeoncrawler.UI2$3.run(UI2.java:136)
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(AccessController.java:391)
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)
Here is my code for the Form. The code where the image is initialized is pointed to in the output error at line 64.
Backround.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/Purple hell.png"))); // NOI18N
getContentPane().add(Backround);
Backround.setBounds(2, 1, 1210, 700);
pack();
}// </editor-fold>
private void txtInputKeyPressed(java.awt.event.KeyEvent evt) {
if(evt.getKeyCode()==KeyEvent.VK_ENTER){
Input = txtInput.getText();
try {
Out = Player.Action(Input);
} catch (IOException ex) {
Logger.getLogger(UI2.class.getName()).log(Level.SEVERE, null, ex);
}
if ("Invalid".equals(Out)){
JOptionPane.showMessageDialog(null, "Invalid Input");
}else txtOutput.setText(Out);
txtInput.setText("");
txtInput.requestFocus();
}
}