Solved

java.lang.InstantiationException.

Posted on 2002-06-11
12
456 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
Comment Utility
You cannot instantiate a Grahics object as it is an abstrcat class.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Why do you want to do this?
0
 
LVL 4

Expert Comment

by:kylar
Comment Utility
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
Comment Utility
> You can't instantiate abstract objects using reflection

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

Expert Comment

by:kylar
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
> I gave him a workaround..

But Graphics2D is also abstract :)

A split is fine :-)
0
 
LVL 4

Expert Comment

by:kylar
Comment Utility
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
Comment Utility
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumDigits  challenge 7 60
count8 challlenge 13 84
where is session ID cache stored 1 38
Securing Jmx Console and web console 2 45
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…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

772 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

9 Experts available now in Live!

Get 1:1 Help Now