Solved

java.lang.InstantiationException.

Posted on 2002-06-11
12
473 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

636 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