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 4565160
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:36:13+00:00 2026-05-21T18:36:13+00:00

I have been working to better understand Java’s reflection tools, and am now trying

  • 0

I have been working to better understand Java’s reflection tools, and am now trying to create a new instance of a class with a constructor that has been chosen by the user in a drop-down-box.

  //ask user what to name the new instance of chosen Class ( passed into this method )
            String instanceName = JOptionPane.showInputDialog(null, "Under what name would you like to store this Instance?", 
                    "Create an Instance of a Class", JOptionPane.QUESTION_MESSAGE);

            Object chosenC = JOptionPane.showInputDialog(null, "Please choose a Constructor for" + chosenClass, 
                    "Create an Instance of a Class", 3, null, getConstructors(chosenClass), JOptionPane.QUESTION_MESSAGE);

            Constructor c = (Constructor)chosenC;
            Class[] params = c.getParameterTypes(); //get the parameters of the chosen Constructor


    try {

            //create the instance with the correct constructor
            instance = c.newInstance((Object[])params); 

        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e){
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } 

Right now, I get an ArgumentTypeMismatch Exception if I select anything but the default constructor(no parameters). Am I missing something blatantly obvious?

Thank you for any help.

EDIT
Thank you all for your answers.

But then how would I ask for the parameters? If I get the length of the params array, then I would have to determine what type of class each index was and ask the user to input a value in the correct form? Then how would I pass that new array into the newInstance parameter?

  • 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-21T18:36:14+00:00Added an answer on May 21, 2026 at 6:36 pm

    Given a class with a constructor like so:

    MyClass(String s, Long l)
    

    Your code is actually invoking the following:

    new MyClass(String.class, Long.class)
    

    Where it should be invoking:

    new MyClass("", 1L)
    

    You need an instance of each of the constructor parameter types, but you’re only passing classes. Interestingly, if you constructor only takes instances of Class, it would work, but would give unexpected results.

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

Sidebar

Related Questions

I have been working on Eclipse recently. I am fairly new to java programming,
I have been trying to create a chat bubble for about a month now...
So lately I have been working on writing a simple compiler to better understand
I have recently been working to better my understanding of sorting algorithms and their
I have been working on a Windows Mobile app for a little while now
I have been working on this problem for 2 days now and it's an
I have an Eclipse project I've been working on for some time now, and
I have been working on Java Swing project. Its design is pretty poor. I
I have been working on an object class for my iPhone app that lazy
I have been working on a new project for a little while and already

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.