0

I'm reading a file that I previously wrote as a HashMap<String,String>. Now, I have to read it again, but it is giving me "warning: [unchecked] unchecked conversion", and it's not compiling.

The line of code causing error:

HashMap<String,String> map_stage = Utils.readObject(staging_area, HashMap.class);

staging_area is a File obj, that is working fine. The warning is lamenting between the HashMap<String,String> type on the left, and HashMap.class on the right

The warning:

  required: HashMap<String,String>
  found:    HashMap
Repository.java:156: warning: [unchecked] unchecked conversion
            HashMap<String,String> map_stage = Utils.readObject(staging_area, HashMap.class);
                                                               ^

This is Utils.readObject:

    /** Return an object of type T read from FILE, casting it to EXPECTEDCLASS.
     *  Throws IllegalArgumentException in case of problems. */
    static <T extends Serializable> T readObject(File file,
                                                 Class<T> expectedClass) {
        try {
            ObjectInputStream in =
                new ObjectInputStream(new FileInputStream(file));
            T result = expectedClass.cast(in.readObject());
            in.close();
            return result;
        } catch (IOException | ClassCastException
                 | ClassNotFoundException excp) {
            throw new IllegalArgumentException(excp.getMessage());
        }
    }

^ this method was given to me, so you can assume is correct.

Compiler error:

grader/submit/AGTester.java:90: error: unreported exception IOException; must be caught or declared to be thrown
        Main.main(args);
                 ^
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
Nello
  • 141
  • 4
  • 15
  • A warning does not prevent code from compiling. – chrylis -cautiouslyoptimistic- Jul 18 '21 at 03:09
  • For this compiler, it does – Nello Jul 18 '21 at 03:17
  • 1
    Then you need to explain what "this compiler" is, since it's different from standard compilers. – chrylis -cautiouslyoptimistic- Jul 18 '21 at 03:18
  • grader/submit/AGTester.java:90: error: unreported exception IOException; must be caught or declared to be thrown Main.main(args); ^ Note: Some input files use unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error – Nello Jul 18 '21 at 03:20
  • 2
    You have an IOException, the issue is not with the warning you specified – Ofek Jul 18 '21 at 03:21
  • try adding `throws IOException` after `... public static void main(String[] args)` (before the `{`) – Ofek Jul 18 '21 at 03:23
  • Thank you everybody for your help so far! I'm already doing "throws IOException" in the main – Nello Jul 18 '21 at 03:27
  • can you add your main class? – Ofek Jul 18 '21 at 03:54
  • The testing framework that your teacher is using doesn't expect `main` to declare any exceptions. You'll have to catch the exceptions, wrap in a `RuntimeException` and rethrow. – tgdavies Jul 18 '21 at 04:08
  • @tgdavies "...unreported exception IOException; must be caught **or declared to be thrown**" – Ofek Jul 18 '21 at 04:52
  • @Ofek what I think is happening is that Main is the OP’s class, and it’s being compiled with the testing framework class, which doesn’t expect the submitted classes to throw anything from their main methods. – tgdavies Jul 18 '21 at 05:43
  • [What does "error: unreported exception ; must be caught or declared to be thrown" mean and how do I fix it?](https://stackoverflow.com/questions/72751842) – Stephen C Mar 06 '23 at 21:22

2 Answers2

0

We found the following workaround:

parent_commit_map =  (HashMap <String, String>) readObject(parent_commit_file) 

I wouldn't advocate as the best implementation, but it solved the problem. Thank you everyone for your suggestions!

Nello
  • 141
  • 4
  • 15
-1

This problem was already answered in this thread. I specify the final code that is in the edited question.

public static <T> T loadData(Class<T> expectedClass, String filename) {
    T o = null;
    try {
        FileInputStream fi = new FileInputStream(filename);
        ObjectInputStream oi = new ObjectInputStream(fi);
        o = expectedClass.cast(oi.readObject());
        oi.close();
        fi.close();
    } catch (Exception e) {}
    return o;
}

Casting to dynamic class crashes in method call

If your problem persists please provide the file you are testing with to find an alternative. :)