PDA

View Full Version : Default constructor cannot handle Ioexception type



rj
02-27-2014, 01:54 AM
So more Java trouble here -.-

When I try to initialize a game object

GameObject spike = new GameObject(40, 40, new TBox(40, 40, 80, 80), 10, 5000, "spike.png");


I get this error:

http://i.imgur.com/BDOzDBX.png

Here is what my game object class looks like

package main;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

class GameObject {

private final int x, y;
private final TBox currentBounds;
private final int damage, value;
private final String fileName;
private final BufferedImage Img;

public GameObject(int x, int y, TBox currentBounds, int damage, int value, String fileName) throws IOException {
this.x = x;
this.y = y;
this.damage = damage;
this.currentBounds = currentBounds;
this.value = value;
this.fileName = fileName;
this.Img = ImageIO.read(new File(fileName));
}

}

Just spent the last 40 minutes looking for answers all I found was to put super(); after

public GameObject(int x, int y, TBox currentBounds, int damage, int value, String fileName) throws IOException {


but I still got the error

Brandon
02-27-2014, 02:04 AM
Lol.. Get a better IDE than whatever crap it is you're using.. That has to be one of the worst error messages I've ever read.

I did however notice that this code is in main's package.. Why is that?

Just try catch the read.. Otherwise try catch wherever it is you're creating GameObject instances.

Apparently you have a child class inheriting from this class. You need to handle the exception within the child class's constructor or else make the child class throw the same exception too.

rj
02-27-2014, 02:13 AM
Lol.. Get a better IDE than whatever crap it is you're using..
If you knew the IDE I was using it would only make you mad :D :D

Anyway, when I had try catch before, I get this error:

http://i.imgur.com/0yVvxSc.png

Brandon
02-27-2014, 02:41 AM
If you knew the IDE I was using it would only make you mad :D :D

Anyway, when I had try catch before, I get this error:

http://i.imgur.com/0yVvxSc.png


So then you initialize it in the catch block to null.

rj
02-27-2014, 03:29 AM
So then you initialize it in the catch block to null.

gotcha thanks