Solved

java.lang.InstantiationException.

Posted on 2002-06-11
12
459 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 98
Java asynchronous logging 4 50
eclipse formatting 6 71
Java Jpanels and Jframe 8 28
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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 …

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now