0

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();
   }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) Use common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). .. – Andrew Thompson Aug 07 '20 at 12:40
  • .. 3) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 4) *"I am trying to use a jLabel as a background"* That leads to all sorts of problems. Instead use custom painting to show the image. But then, you seem to be a newbie, so I'd recommend avoiding ;screen candy' in GUIs until you are more experienced. 5) Rather than use a `KeyListener` on a `JTextField`, add an `ActionListener`. – Andrew Thompson Aug 07 '20 at 12:43

1 Answers1

0

Check your classpath. Just because it runs in your IDE doesn't mean it runs if executed as a jar.

Does getClass().getResource("/resources/Purple hell.png") really return an InputStream to the png?

raupach
  • 3,092
  • 22
  • 30
  • 1
    *"Does `getClass().getResource("/resources/Purple hell.png")` really return an `InputStream`"* No. You're thinking of a variant of that method. It will return either a `URL` or `null` (as seen in this case). For a variety of reasons, I recommend using the version that returns a `URL`. – Andrew Thompson Aug 07 '20 at 12:34