JDK-6338812 : (reflect) Class.newInstance() failure for missing nullary constructor needs better detail string
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 5.0
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2005-10-19
  • Updated: 2024-04-12
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
When creating a new instance from the Class.newInstance() method,  I used a class I had made which didn't have a "nullary" constructor.

At the time I didn't know this would throw an error because I didn't know what a nullary constructor was (I *think* I know what it is - a constructor without any arguments?)

I wasted a fair bit of time looking for other problems.



JUSTIFICATION :
It would have saved a lot of time if the error message was better.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The error message should say: "class being instantiated must have a no-argument constructor" or something to that effect.

Comments
SUGGESTED FIX --- Class.java Wed Oct 3 15:19:37 2007 *************** *** 355,361 **** }); cachedConstructor = c; } catch (NoSuchMethodException e) { ! throw new InstantiationException(getName()); } } Constructor<T> tmpConstructor = cachedConstructor; --- 355,361 ---- }); cachedConstructor = c; } catch (NoSuchMethodException e) { ! throw new (new InstantiationException(getName()).initCause(e); } } Constructor<T> tmpConstructor = cachedConstructor; *** (#1 of 1): [ UNSAVED ] ###@###.###
03-10-2007

EVALUATION Based on the description, here's a small test case and associated output: public class Test { public Test(int i) {} public static void main(String [] args) throws Throwable { Test.class.newInstance(); } } $ java Test Exception in thread "main" java.lang.InstantiationException: Test at java.lang.Class.newInstance0(Class.java:358) at java.lang.Class.newInstance(Class.java:326) at Test.main(Test.java:5) A "nullary constructor" is a constructor which takes no arguements. Class.newInstance() is attempting to construct the requested object by invoking Test(), which doesn't exist. Note that the above example would succeed if Test(int) did not exist because the compiler-provided default constructor as described in section 8.8.9 of the JLS Third edition, would qualify as the nullary constructor: http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.8.9 Assuming that the InstantiationException is created only when a nullary constructor is not present (as appears to be the case), it would be trivial to modify the detail string to provide the additional data similar to how similar cases are handled in the code. An even better solution would be to use the exception chaining mechanism.
03-10-2007