0

This code is giving me a error, most likely its the URL path object

package prosjekt_1139;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")
public class HovedVinduet extends JFrame implements ActionListener {

private JPanel hukommelsepanel, hovedPanel, yathzyPanel,menypanel;
private JButton knapp1 = new JButton("Sudoku");
private JButton knapp2 = new JButton("Hukommelse");
private JButton knapp3 = new JButton("Yathzy");


public HovedVinduet() throws IOException {
    super("Spillet");


    hukommelsepanel = new Hukommelse(this);
    yathzyPanel = new Yathzy(this);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //most likely its the url here that causing the problem
    URL path = getClass().getResource("/Image/grass.jpg");
    BufferedImage image = ImageIO.read(path);

    Background contentPane = new Background(image);

    contentPane.setOpaque(true);
    contentPane.setLayout(new GridBagLayout());
    contentPane.add(knapp1);
    contentPane.add(knapp2);
    contentPane.add(knapp3);

    knapp1.addActionListener(this);
    knapp2.addActionListener(this);
    knapp3.addActionListener(this);

    this.setContentPane(contentPane);
    this.setSize(800, 600);
    this.setLocation(200,200);
    this.setVisible(true);
    menypanel = contentPane;
}

public void setAktivtPanel(JPanel aPanel){
    this.hovedPanel = aPanel;
    this.setContentPane(hovedPanel);
    this.pack();
    this.setVisible(true);
}

public void setMenyPanelAktivt(){
    this.setTitle("Hovedvinduet");

    this.setSize(800, 600);
    this.setLocation(200,200);
    setAktivtPanel(menypanel);
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            try {

                new HovedVinduet();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

}
@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if (e.getSource() == knapp1){
        new Sudoku().setVisible(true);
    }
    if (e.getSource() == knapp2){
        this.setAktivtPanel(hukommelsepanel);
        this.setTitle("Hukommelse");
    }
    if (e.getSource() == knapp3){
        this.setAktivtPanel(yathzyPanel);
        this.setTitle("Yathzy");
    }
 }

}

The exception is:

    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException:input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at prosjekt_1139.HovedVinduet.<init>(HovedVinduet.java:32)
at prosjekt_1139.HovedVinduet$1.run(HovedVinduet.java:74)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
3
  • 5
    Please post the full stack trace and indicate which line is giving the error. Commented Mar 29, 2011 at 11:27
  • So this returns null: URL path = getClass().getResource("/Image/grass.jpg"); - I guess because the resource doesn't exist. Commented Mar 29, 2011 at 11:35
  • Please add a comment to you code that shows in which line exactly does the exception happen. Commented Mar 29, 2011 at 11:35

1 Answer 1

1

The following line returns null:

URL path = getClass().getResource("/Image/grass.jpg");

Check that the file "Image/grass.jpg" is available in your classpath.

Sign up to request clarification or add additional context in comments.

6 Comments

it is part of my workspace src, the image works if i put it into a imageIO.read(new File("Image/grass.jpg"))
What's your project layout? If "Image" is a regular folder, i.e., not part of the build path, getResource() won't find it. If "Image" is directly added to the class path, then you need to call getResource("grass.jpg"), as it is in the top-level package. What your code is saying is, "load the file grass.jpg from the package Image". For the call getResource("/Image/grass.jpg") to succeed, the image would have to be located e.g. at "src/Image/grass.jpg".
src/prosjekt_1139, src/Image is how my project layout
I understand that the image is at the path "src/Image/grass.jpg", correct? If so, your code should work. Be aware that the path might be case-sensitive on linux or mac. Although in that case, 'ImageIO.read(new File("Image/grass.jpg"))' should not work, if your working directory is the project folder.
yes that is correct, but it works with the File but Not as a URL
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.