Solved

java.lang.InstantiationException.

Posted on 2002-06-11
12
469 Views
Last Modified: 2011-10-03
hai,

can any one solve this for me

Class class = Class.forName("java.awt.Graphics");
Object obj = class.newInstance();

giving an error of java.lang.InstantiationException.
how to instantiate this type of abstract classess.


0
Comment
Question by:hellohowareu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 7072010
You cannot instantiate a Grahics object as it is an abstrcat class.
0
 
LVL 92

Expert Comment

by:objects
ID: 7072012
Why do you want to do this?
0
 
LVL 4

Expert Comment

by:kylar
ID: 7074055
You can't instantiate abstract objects using reflection. You need to instantiate a concrete subclass or else instantiate the abstract class and provide implementation for the unknown pieces..

eg:

AbstractTableModel model = new AbstractTableModel();//Won't Work.

AbstractTableModel model = new AbstractTableModel(){
public int getRowCount(){return 0;}
public int getColumnCount(){return 0;}
public Object getValueAt(int row, int col){return "";}
};//works...

Class myClass = Class.forName("javax.swing.table.AbstractTableModel");
Object obj = class.newInstance();//won't work.

Class myClass = Class.forName("javax.swing.table.DefaultTableModel");
Object obj = class.newInstance();//proper use. works.

Cheers,
Kylar
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 7074233
> You can't instantiate abstract objects using reflection

You can't instantiate abstract objects *ever*.
0
 
LVL 4

Expert Comment

by:kylar
ID: 7076076
Braaaap. Brainfart. I guess I never considered that you're not really instantiating the Abstract class, you're instantiating an anonymous concrete subclass...
Damn you're good :)

Cheers,
Kylar
0
 
LVL 4

Expert Comment

by:kylar
ID: 7076086
And what I also forgot to say.. was to answer your question hello... try this instead:

Class cl = Class.forName("java.awt.Graphics2D");
Object obj = cl.newInstance();

Good?

Cheers,
Kylar
0
 
LVL 92

Expert Comment

by:objects
ID: 7076739
Graphics2D is abstract as well :-)

The only way I know of to get a Graphics object is from a component, image or some object that has a graphics context associated with it.
0
 
LVL 92

Expert Comment

by:objects
ID: 7084926
Hello,

This question has been open for quite a while now and needs to be wrapped up.

Please do NOT accept this comment as an answer, as I am simply trying to alert those involved that the
question is still open.

If any of the experts could come back and post any suggestions as to how they feel this could be wrapped
up (delete, 0 PAQ, award points, etc), I'm sure the moderators would appreciate it.

Thank you,
0
 
LVL 4

Expert Comment

by:kylar
ID: 7085020
What do you say objects? Split it? You told him it can't be done (and kept my muddled rambling straight)... I gave him a workaround..
Cheers,
Kylar
0
 
LVL 92

Expert Comment

by:objects
ID: 7086552
> I gave him a workaround..

But Graphics2D is also abstract :)

A split is fine :-)
0
 
LVL 4

Expert Comment

by:kylar
ID: 7090385
LOL I meant that you need to create a concrete subclass. Man I don't know where my head has been lately.. too much writing engineering specs and not enough coding.

Cheers
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7121674
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question