Fehler : Uncaught error fetching image: java.lang.NullPointerException

Den image-Pfad ist alles richtig, nur, ich weiß nicht, warum dieser Fehler kommt.
Jemand eine Idee?

Code ist für das setzen eines hintergrund-Bildes in einem frame-und button hinzufügen, über das Bild

Den Fehler

Uncaught error fetching image:
    java.lang.NullPointerException
        at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:99)
        at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:113)
        at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:240)
        at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
        at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)

Code

Fehler kommt, während der code ausgeführt wird :

        public class BackgroundImg extends JPanel {

            private Image img;

          public BackgroundImg (String img)
          {
              this(new ImageIcon(img).getImage());
          }

          public BackgroundImg (Image img)
          {
            this.img = img;
            Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
            setPreferredSize(size);
            setMinimumSize(size);
            setMaximumSize(size);
            setSize(size);
            //setLayout(null);
          }

            @Override
          public void paintComponent(Graphics g)
            {
                super.paintComponent(g);
            g.drawImage(img, 0, 0, null);
          }
        }

        public class ApplicationFrame {

        public static void main(String[] args) throws ImageAccessException {
        BackgroundImg panel = new BackgroundImg(Toolkit.getDefaultToolkit().createImage(ApplicationFrame.class.getResource("C:\\test.png")));
    JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(800, 600);

                frame.getContentPane().add(panel);
                ApplicationContents.addContentsToPane(panel);

                frame.setVisible(true);
                //  frame.setVisible(true);

            }

        }


    }
public class ApplicationContents {
    public static void addContentsToPane(Container pane)
    {
        //pane.setLayout(null);
        pane.setLayout(new FlowLayout());

        JButton b1 = new JButton("Test");

        pane.add(b1);

        //Insets insets = pane.getInsets();
        //Dimension size = new Dimension(120,32);

        //b1.setBounds(0 + insets.left, 0 + insets.top,
         //           size.width, size.height);


         b1.addActionListener(
                 new ActionListener()
         {
             public void actionPerformed(ActionEvent event)
            {

                 JOptionPane.showMessageDialog(null, "Test" , "Test", JOptionPane.ERROR_MESSAGE);
            }
         }
         );
    }

}
  • Ist dieser Weg wirklich richtig? C:\\Users\\245284\\Documents\\NetBeansProjects\\cts.gto.rd.advisor\\Img\\test.png Haben Sie einen Ordner mit dem Namen cts.gto.rd.advisor oder ist es nur eine package-Namen?
InformationsquelleAutor Raj | 2012-05-21
Schreibe einen Kommentar