2

I have yet another issue which the answer is eluding me. I wish to take a class from an external jar at runtime and grab a method from it and pass it a parameter. My code below currently opens the jar and grabs the class and runs the method but when I try and pass it a parameter, the method runs but I get an InvocationTargetException. Any ideas?

Here is my code:

    String path = "test.jar";
    URL[] classes = {new File(path).toURI().toURL()};
    URLClassLoader child = new URLClassLoader (classes, this.getClass().getClassLoader());
    try {
        Class classToLoad = Class.forName("testClass", true, child);
        Method method = classToLoad.getDeclaredMethod ("testMethod", String.class);
        Object instance = classToLoad.newInstance();
        Object result = method.invoke(instance, new String("Test from method!"));
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }

And here is the error it throws:

Test from method!
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at Load.loadJar(LoadTerrem.java:33)
    at Load.<init>(LoadTerrem.java:18)
    at Load.main(LoadTerrem.java:13)
Caused by: java.lang.NullPointerException
    at MenuSingleplayer.LoadWorlds(MenuSingleplayer.java:210)
    at MenuSingleplayer.setup(MenuSingleplayer.java:89)
    at M0.LoadGame(M0.java:76)
    ... 7 more

As you can see, the method executes, printing out the string passed to it but then throws an error on the line:

Object result = method.invoke(instance, new String("Test from LoadTerrem!"));

Any ideas? Thanks!

0

2 Answers 2

3

An InvocationTargetException is thrown when the invoked method (testMethod in your case) throws an exception. From the docs:

InvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor.

Judging from your stack trace (the "caused by"-part to be precise) it looks like testMethod causes MenuSingleplayer.LoadWorlds to be called, which raises a NullPointerException. This NullPointerException propagates up until it reaches the reflective call, at which point the it is wrapped in an InvocationTargetException.

Sign up to request clarification or add additional context in comments.

Comments

3

The problem is in the MenuSingleplayer.LoadWorlds method. It says a NPE happened there. There is nothing to do with reflection. It's OK:

Caused by: java.lang.NullPointerException
at MenuSingleplayer.LoadWorlds(MenuSingleplayer.java:210)

1 Comment

Well, I feel a little... stupid now. Thanks though you solved it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.