SRL Archive - Java Help and Tutorials

Default constructor cannot handle Ioexception type


rj

2014-02-26 20:54:52

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:


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

2014-02-26 21:04:19

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

2014-02-26 21:13:16

From: Brandon
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:


Brandon

2014-02-26 21:41:37

From: Officer Barbrady
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:




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

rj

2014-02-26 22:29:36

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


gotcha thanks