0

I'm trying to have the file "TutorialMap" used as the map in this TutorialMission. I keep getting told that the MapReader "reader" needs to be static, but when it's static, I get told "Unhandled exception type FileNotFoundException" with the error on the constructor of reader.

static MapReader reader = new MapReader("TutorialMap");

static Territory[][] missionMap = reader.getMap();

public TutorialMission() throws FileNotFoundException {

    super(missionMap, Size, AircraftCarrierID, AircraftCarrierID);

}

The Super class' constructor:

public class MissionIF extends Map {
public MissionIF(Territory[][] load, String size, int StartingMoney, int powerLevel)
{
    // Set money per mission.
    super();

Thanks for your time.

  • the constructor on your super class is launching this exception, please post that code – fmodos May 27 '13 at 13:37
  • What is TutorialMap ? Is it a file? If yes, then it must have an extension like `TutorialMap.txt` or `TutorialMap.dat`. That could be the reason for `FileNotFoundException`. – Rahul Bobhate May 27 '13 at 13:41
  • The error is underlining the declaration of reader, encasing " new MapReader("TutorialMap.txt") " – user1933516 May 27 '13 at 13:50

3 Answers3

0

surrounds the code throwing "Unhandled exception type FileNotFoundException" with try catch block. Your are getting this exception because its a checked exception and you are forced to handle this. i would suggest using IDE like eclipse(if you are not using this already) which is really helpful for development.

try {
        reader = new MapReader("TutorialMap");
    } catch(FileNotFoundException e) {
        throw new RunTimeException(e);
    }
M Sach
  • 33,416
  • 76
  • 221
  • 314
  • I am using Eclipse, which is showing me the error. When I try to configure a try catch block, I get an error on any line that has try, catch, or the {} that they encase. – user1933516 May 27 '13 at 13:46
0

I don't know why it must be static, but since the constructor throws the checked exception, it has to be handled at the place of calling. Therefore do something like this:

static MapReader reader = null;

static Territory[][] missionMap = null;

static {  
   try { 

        reader = new MapReader("TutorialMap");
   } catch(FileNotFoundException e) { 
        e.printStackTrace();
   }
   missionMap = reader.getMap();

  }
jan.zanda
  • 146
  • 2
0

Compiler requires your reader to be static because you invoke it when initializing other static variable missionMap.

When you mark it as static compiler is going forward and sees that you do not catch exception thrown from your constructor TutorialMission.

Since I do not understand what do you really want to do I can just suggest you:

If you want all this stuff to be static initialize reader into static initializer and catch exception:

static MapReader reader;
static {
    try {
        reader = new MapReader("TutorialMap");
    } catch(FileNotFoundException e) {
        throw new IllegalArgumentException(e);
    }
}

Your checked exception is now wrapped by unchecked one.

Alternatively (if you do not really want to hold this data in static variables just remove static modifier and perform initalization in constructor:

public TutorialMission(MapReader reader, Territory[][] missionMap) throws FileNotFoundException {
    super(missionMap, Size, AircraftCarrierID, AircraftCarrierID);
    missionMap = reader.getMap();
}

Now caller is responsible on creating and passing here the reader.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • I tried doing that, but the variable has to not be null before it hits the super statement, and the super statement has to be the first thing in a constructor method. – user1933516 May 27 '13 at 13:53