1

I'm running this code, and it's returning the following stack trace:

Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:115)
at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:125)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:263)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:205)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:169)

I'm not sure where I made the mistake to output a Null Pointer exception. Tips on how to fix this?

import java.awt.BorderLayout;  
import java.awt.EventQueue;  
import java.awt.Graphics;  
import java.awt.Image;  
import java.awt.Toolkit;  
import javax.swing.JFrame;  
import javax.swing.JPanel;  
import javax.swing.border.EmptyBorder;  

public class MyFrame extends JFrame {  
     private JPanel contentPane;  

     public static void main(String[] args) {  
          EventQueue.invokeLater(new Runnable() {  
              public void run() {  
                    try {  
                         MyFrame frame = new MyFrame();  
              frame.setVisible(true);  
               } catch (Exception e) {  
                         e.printStackTrace();  
                   }  
               }  
        });  
     }   
   public MyFrame() {  
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        setBounds(100, 100, 450, 300);  
        contentPane = new JPanel() {  
             public void paintComponent(Graphics g) {  
                  Image img = Toolkit.getDefaultToolkit().getImage(  
                           MyFrame.class.getResource("/images/Ella and Louis_front.jpg"));  
                 g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);  
             }  
        };  
       contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));  
        contentPane.setLayout(new BorderLayout(0, 0));  
        setContentPane(contentPane);         }  

}

Victor2748
  • 4,149
  • 13
  • 52
  • 89
Tack
  • 121
  • 1
  • 12
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Martijn Pieters Dec 14 '14 at 00:12
  • 2
    Please provide the stacktrace – Victor2748 Dec 14 '14 at 00:13
  • 1
    When you get an EXCEPTION, or "error" please post the stack trace, which will tell us in which line the exception occurred. The stack trace is meant to make your life simpler. Post it here. – Erran Morad Dec 14 '14 at 00:14
  • I answered the question, please reply if it helped/didn't help – Victor2748 Dec 14 '14 at 00:19

1 Answers1

1

when getting a resource: MyFrame.class.getResource("/images/Ella and Louis_front.jpg") it returns a null, I suggest doing a check: I suggest doing a check:

URL url = MyFrame.class.getResource("/images/Ella and Louis_front.jpg")
if (url != null) {
    Image img = Toolkit.getDefaultToolkit().getImage(url);
}
Victor2748
  • 4,149
  • 13
  • 52
  • 89