0

I've been working on a project that I described in this question that I asked previously. I'm trying to invoke a method from a class who's name is dynamically generated (and the class compiled while the program runs). I call Class watchFace = Class.forName("pebbleos.PebbleOS_" + fileName); followed by currentWatchFace = watchFace.newInstance(); in the method loadWatchFace(), and then in the method runWatchFace() I try to invoke the method using this:

Method method = null;
        try {
            method = currentWatchFace.getClass().getMethod("initializeFace");
        } catch (SecurityException | NoSuchMethodException e) {
            System.out.println("Error");
        }
        method.invoke(currentWatchFace);

My watch face's code is being taken from a text file, which looks like this:

package pebbleos;

public class PebbleOS_Default {

    public PebbleOS_Default () {

    }

    public void initializeFace() {
        System.out.println(“Hello World”);
    }

}

Just a quick note, the above is supposedly the "cause" of this error: java.lang.reflect.InvocationTargetException

Community
  • 1
  • 1
Avery Vine
  • 154
  • 10
  • 1
    Use `e.printStackTrace()` rather than your naive `System.out.println("Error")` to **check the details of the exception** and do a relevant search on the subject. – Luiggi Mendoza Jun 20 '15 at 04:21
  • That portion (thought you are certainly correct) is irrelevant at the moment, because that portion of the code is not being called, i.e. the method is apparently being initialized properly. – Avery Vine Jun 20 '15 at 04:23
  • 2
    Wrap that code into another `try-catch` statement and use `e.printStackTrace()` to get the proper details of the exception. You evaluate the exception based on the details it provides, not only on its type. – Luiggi Mendoza Jun 20 '15 at 04:24
  • So the error is definitely in the statement `method.invoke(currentWatchFace)`, and it is saying that the uncompilable source code in on line 10 of the file, i.e. `System.out.println("Hello World");`. – Avery Vine Jun 20 '15 at 04:28
  • Not completely sure, but you're using `“` when it should be `"` in `System.out.println(“Hello World“);` (check the difference of the double quote). – Luiggi Mendoza Jun 20 '15 at 04:29
  • That's brilliant, that was it. Thank you. I replaced the curled quotation marks with the straight ones, and it works perfectly now. – Avery Vine Jun 20 '15 at 04:31

1 Answers1

1

From the code you posted, seems that you're using the wrong character that looks a lot like " but they're not the same. Fix it, recompile the code and try again.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332