0

I am getting input==null while trying to load an Image. I can't seem to figure out why, even after browsing a good number of StackOverflow questions. It looks right to me.

Here is the code:

private BufferedImage image;

public practice(){
    setLayout(new BorderLayout());

    timeBall.start();


    JPanel panelNorth = makePanel();
    panelNorth.setBackground(Color.CYAN);
    add(panelNorth, BorderLayout.NORTH);

    try{
         image = ImageIO.read(practice.class.getResource("/pics/flying.png"));

    }catch(IOException e){
        e.printStackTrace();
    }






}
public Dimension getPreferredSize() {
    return image == null ? new Dimension(400, 300): new Dimension(image.getWidth(), image.getHeight());
}
public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.BLUE);     

    ellipse = new Ellipse2D.Double(Ox, Oy+Speedy, height, height);
    Graphics2D graphics = (Graphics2D)g;
    g.drawImage(image, 0, 0, this);           
    graphics.fill(ellipse);
}
@Override
public void actionPerformed(ActionEvent e) {    

    repaint();
    Oy = Oy + Speedy / 10;
    Ox = Ox + Speedx / 10;
}
Blake Yarbrough
  • 2,286
  • 1
  • 20
  • 36
  • `/pics/flying.png` is not available via the CLASSPATH. Where exactly is in your JAR file? – user207421 Dec 07 '15 at 20:50
  • @EJP I am new to putting images in Java so I don't know what JAR file is, but I created a new source folder and named it pics. That's where the png file is. –  Dec 07 '15 at 20:54
  • Well you need to learn how to make it so. – user207421 Dec 07 '15 at 20:57
  • 1
    http://stackoverflow.com/questions/9864267/load-icon-image-exception – Pshemo Dec 07 '15 at 20:58
  • @Pshemo that's what I used originally, but it still isn't working. –  Dec 07 '15 at 21:00
  • @EJP I can do that, but can you help me afterwards? –  Dec 07 '15 at 21:01
  • 1
    To get better help you need to provide enough information to allow us reproduce your problem. How do you execute your code? Is it JAR or is it project in your IDE? What IDE are you using? Where is file you are trying to read located in your project? – Pshemo Dec 07 '15 at 21:04
  • I am using an IDE, Eclipse. The file is in a new source folder I created called pics. @Pshemo –  Dec 07 '15 at 21:05
  • 1
    Is the *source folder* called `pics` or does it *contain* folder called `pics`? Notice that source folder is not part of path, it just contains other resources which we should be able to access. – Pshemo Dec 07 '15 at 21:07
  • Then the path is `/flying.png` Look in your built jar (zip format), or in the build/target/dist folder under classes. – Joop Eggen Dec 07 '15 at 21:07
  • Creating the JAR did it. Thanks to everyone for the help. I've never done that before. –  Dec 07 '15 at 21:10

0 Answers0