Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: instantiation exception

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default instantiation exception

    i dont know how to create an instance of an inner class using classloader? here is my code in java.
    plz help. emergency question

    package a;
    public class outer{
    public outer(){}
    public class inner{
    public inner(){}
    }

    }
    ------------
    package test;
    public class main{
    static void main(){
    //how to create an instance of inner class by classLoader.loadclass??
    //i have tried but I face instantiation exception
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: instantiation exception

    Can you post more details? What is the name of the classs?
    What class and methods are you trying to use to load that class?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: instantiation exception

    Quote Originally Posted by Norm View Post
    Can you post more details? What is the name of the classs?
    What class and methods are you trying to use to load that class?
    tnx for ur reply
    i used this code:
    public static void main(){
    outer o=new outer();
    inner i=o.new inner ();
    i=(inner)Class.forName("a.outer$inner").newInstanc e();
    }

    the error was instantiation exception

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: instantiation exception

    Can you make a small complete program that compiles, executes and shows the problem?
    Be sure to wrap the code in code tags. What you posted won't compile or execute.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: instantiation exception

    <code>
    package a;


    public class A {

    public A(){System.out.println("a constructed");}


    public class B{

    public B(){System.out.println("b constructed");}

    }

    }


    --------------------------------------------
    package helloworld;

    import a.A;
    import a.A.B;


    public class Main {


    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {

    A a=new A();
    B b=a.new B();
    b=(B)Class.forName("a.A$B").newInstance();



    }

    }
    </code>
    ---------------------------------
    output is:

    a constructed
    b constructed
    Exception in thread "main" java.lang.InstantiationException: a.A$B
    at java.lang.Class.newInstance0(Class.java:340)
    at java.lang.Class.newInstance(Class.java:308)
    at helloworld.Main.main(Main.java:25)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

    --- Update ---

    [CODE]

    package a;


    public class A {

    public A(){System.out.println("a constructed");}


    public class B{

    public B(){System.out.println("b constructed");}

    }

    }




    package helloworld;

    import a.A;
    import a.A.B;


    public class Main {


    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {

    A a=new A();
    B b=a.new B();
    b=(B)Class.forName("a.A$B").newInstance();



    }

    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: instantiation exception

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Can you remove the packages. They make the testing too hard to setup.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: instantiation exception

    Quote Originally Posted by Norm View Post
    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    ok, really Sorry

     
    package a;
     
     
    public class A {
     
        public A(){System.out.println("a constructed");}
     
     
        public class B{
     
            public B(){System.out.println("b constructed");}
     
        }
     
    }
     
     
     
    package helloworld;
     
    import a.A;
    import a.A.B;
     
     
    public class Main {
     
     
        public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
     
         A a=new A();
         B b=a.new B();
         b=(B)Class.forName("a.A$B").newInstance();
     
     
     
     
     
        }
     
    }

    output:
    a constructed
    b constructed
    Exception in thread "main" java.lang.InstantiationException: a.A$B
    at java.lang.Class.newInstance0(Class.java:340)
    at java.lang.Class.newInstance(Class.java:308)
    at helloworld.Main.main(Main.java:25)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: instantiation exception

    Have you researched if this is possible?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: instantiation exception

    Quote Originally Posted by Norm View Post
    Have you researched if this is possible?
    I have a code that has run before, but now i cant run it. i know there should be a way...

    --- Update ---

    HooRAY, I found the way

    package helloworld;
     
    import a.A;
    import a.A.B;
     
    /**
     *
     * @author alieh
     */
    public class Main {
     
     
     
        public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
     
     
            Class<B> b=null;
            ClassLoader classLoader = ClassLoader.getSystemClassLoader();
            b = ( Class<B> )Class.forName( "a.A$B",false,classLoader);
     
     
     
     
     
     
     
        }
     
    }


    --- Update ---

    Dear Norm,
    really tnx for ur attention and reminding me to research;-)

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: instantiation exception

    Thanks for sharing the solution.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Instantiation of bean failed
    By peejayuk in forum Member Introductions
    Replies: 1
    Last Post: August 15th, 2011, 02:35 PM
  2. Instantiation troubles
    By hello_world in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 16th, 2011, 08:24 PM
  3. [SOLVED] What kind of object instantiation is this?
    By Lord Voldemort in forum Object Oriented Programming
    Replies: 3
    Last Post: July 7th, 2011, 07:00 AM
  4. ActionServlet Instantiation
    By tcstcs in forum Web Frameworks
    Replies: 0
    Last Post: April 5th, 2011, 06:12 AM
  5. Instantiation from within Methods
    By John Davies in forum Object Oriented Programming
    Replies: 2
    Last Post: March 10th, 2011, 04:39 PM

Tags for this Thread