Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7699331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:27:10+00:00 2026-05-31T22:27:10+00:00

I have yet another issue which the answer is eluding me. I wish to

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:27:12+00:00Added an answer on May 31, 2026 at 10:27 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After getting a helpful answer here , I have run into yet another problem:
I have run into yet another issue with my program. I have made several
This is a follow up from Creating a class which inherits from another class
This is a fairly common problem to which I have yet to find an
I'm having yet another WPF binding issue. Just when I think I've got this
I have a database of games and prices, in which I recently found yet
I have searched online for a solution, but have yet to find one that
Okay this may be a simple question but I have yet to come with
I've been doing web development for a while and have yet to read a
I seem to run into this situation quite a lot and have yet to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.