Results 1 to 10 of 10
- 08-05-2011, 07:28 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 22
- Rep Power
- 0
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:
And the constructor of that class looks 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 this is the exception I get:Java Code:public class newPanel extends JPanel{ public newPanel(java.util.Locale l){ ... } ... }
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 07:32 PM.
- 08-05-2011, 09:37 PM #2
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?
- 08-05-2011, 11:03 PM #3
Member
- Join Date
- Aug 2011
- Posts
- 22
- Rep Power
- 0
Hi Norm,
Thanks a bunch for replying.
In my code the exception is triggered when newInstance is called.
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:panelArray[i] = (JPanel) c.newInstance(args);
Using this constructor instead: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();
Getting the same result but not passing the variable which may change at some point.Java Code:public class newPanel extends JPanel{ public newPanel(){ java.util.Locale l = new java.util.Locale("ES","es"); ... } ... }
Thanks again for your help, regards.
- 08-05-2011, 11:28 PM #4
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???
- 08-06-2011, 12:00 AM #5
Member
- Join Date
- Aug 2011
- Posts
- 22
- Rep Power
- 0
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.
- 08-06-2011, 12:01 AM #6
Ok, glad you have a working program.
- 08-06-2011, 08:20 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
What happens if you do this?
kind regards,Java Code:panelArray[i] = (JPanel) c.newInstance(currentLocale);
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-06-2011, 10:57 AM #8
Member
- Join Date
- Aug 2011
- Posts
- 22
- Rep Power
- 0
- 08-06-2011, 12:07 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-06-2011, 01:28 PM #10
Member
- Join Date
- Aug 2011
- Posts
- 22
- Rep Power
- 0
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 01:41 PM.
Similar Threads
-
Exception in thread "main" java.lang.NumberFormatException:input string: "060320
By renu in forum New To JavaReplies: 14Last Post: 04-08-2011, 06:01 PM -
"Exception in thread "Launcher:/Whiteboard" java.lang.RuntimeException: Failed to loa
By Shajith in forum EclipseReplies: 3Last Post: 03-21-2011, 01:48 PM -
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/star/lang/XEventLi
By baktha.thalapathy in forum Advanced JavaReplies: 3Last Post: 06-01-2010, 03:01 PM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 07:58 PM -
[SOLVED] pls help :S . "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerE
By ara in forum New To JavaReplies: 10Last Post: 01-29-2009, 08:00 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks