Results 1 to 10 of 10
  1. #1
    b0rt is offline Member
    Join Date
    Aug 2011
    Posts
    22
    Rep Power
    0

    Default Lost in "java.lang.InstantiationException"

    Hi all,

    I was wishing someone could orient me in finding the source of this exception in my code. I have a loop that instantiates different classes by their name, doing it without arguments works like a charm, but when adding arguments I get this "java.lang.InstantiationException" exception. The code is like follows:
    Java Code:
    java.util.Locale currentLocale = new java.util.Locale ("ES","es");
    ...
                        Class newClass = Class.forName(classNameString);
                        Class[] argsClass = new Class[] {java.util.Locale.class};
                        Constructor c = newClass.getConstructor(argsClass);
                        Object[] args = new Object[] {currentLocale};
                        panelArray[i] = (JPanel) c.newInstance(args);
    ...
    And the constructor of that class looks like follows:

    Java Code:
    public class newPanel extends JPanel{
        public newPanel(java.util.Locale l){
        ...
        }
    ...
    }
    And this is the exception I get:
    Java Code:
    java.lang.InstantiationException:
    at java.lang.Class.newInstance0(Class.java:340)
    at java.lang.Class.newInstance(Class.java:308)
    ...

    Anyone got some hint for me?
    Many thanks in advance!
    Last edited by b0rt; 08-05-2011 at 08:32 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Please post the full text of the error message. There is data you have left off showing where in your program the error occurred.

    What is the value of classNameString?

  3. #3
    b0rt is offline Member
    Join Date
    Aug 2011
    Posts
    22
    Rep Power
    0

    Default

    Hi Norm,

    Thanks a bunch for replying.

    In my code the exception is triggered when newInstance is called.
    Java Code:
     panelArray[i] = (JPanel) c.newInstance(args);
    The value of classNameString is "cataplus.panels.newPanel", which is the complete path of the class to be instantiated, but may not be the problem since the class is found properly, and there's no exception if I remove the argument:

    Java Code:
                        Class newClass = Class.forName(classNameString);
                        Class[] argsClass = new Class[] {};
                        Constructor c = newClass.getConstructor(argsClass);
                        Object[] args = new Object[] {currentLocale};
                        panelArray[i] = (JPanel) c.newInstance();
    Using this constructor instead:
    Java Code:
    public class newPanel extends JPanel{
        public newPanel(){
            java.util.Locale l = new java.util.Locale("ES","es");
            ...
        }
    ...
    }
    Getting the same result but not passing the variable which may change at some point.

    Thanks again for your help, regards.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Can you make a small complete program (SSCCE) that shows your problem. Your bits and pieces of code are hard to put together and see what is happening. For example what is the value of the args variable???

  5. #5
    b0rt is offline Member
    Join Date
    Aug 2011
    Posts
    22
    Rep Power
    0

    Default

    Hi again Norm,

    I'm really sorry to have wasted your time, the problem has disappeared but I still have no clue what caused the exception.
    I have been debugging and testing this over and over since yesterday and now I just replaced the patch with the original code - as I did at least 20 times before - and it just did work without exception.

    I'm happy to see it working but embarrassed for not being able to see where the problem was.

    Thanks a lot for your kind intention and please excuse me for being such a mess.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Ok, glad you have a working program.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    What happens if you do this?

    Java Code:
    panelArray[i] = (JPanel) c.newInstance(currentLocale);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    b0rt is offline Member
    Join Date
    Aug 2011
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    What happens if you do this?

    Java Code:
    panelArray[i] = (JPanel) c.newInstance(currentLocale);
    kind regards,

    Jos
    Oh, didn't notice that would work as well. Since it asks for an array I thought it had to be that way.
    Thanks Jos!

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by b0rt View Post
    Oh, didn't notice that would work as well. Since it asks for an array I thought it had to be that way.
    Thanks Jos!
    You're welcome of course; my suggestion doesn't 'work as well'; it simply is the only way that works ;-) The method wants to see an Object ... (note the ellipses) You were supplying it an Object[] which (to the compiler) is also an Object so what it does is wrap it up in another Object[] and passes it to that method. That array of Objects now has an array of Objects as its first element but the method (constructor) doesn't take an Object[] array as a parameter, hence the Exception. You were just trying to 'help' the compiler a bit too much.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    b0rt is offline Member
    Join Date
    Aug 2011
    Posts
    22
    Rep Power
    0

    Default

    Well, it does work with the array as well, I know, it is meant to receive the same type of arguments the referenced constructor has, but it takes the array with 1 element as valid too, that wasn't causing the exception, though I don't either know what was.
    Last edited by b0rt; 08-06-2011 at 02:41 PM.

Similar Threads

  1. Replies: 14
    Last Post: 04-08-2011, 07:01 PM
  2. Replies: 3
    Last Post: 03-21-2011, 02:48 PM
  3. Replies: 3
    Last Post: 06-01-2010, 04:01 PM
  4. Replies: 1
    Last Post: 11-20-2009, 08:58 PM
  5. Replies: 10
    Last Post: 01-29-2009, 09:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •