wood burning stoves 2.0*
The moose likes Java in General and the fly likes Why am I not able to get Instance of this subclasses? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why am I not able to get Instance of this subclasses?" Watch "Why am I not able to get Instance of this subclasses?" New topic
Author

Why am I not able to get Instance of this subclasses?

Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 435
Hi,


I have an Editor class...



I can access all of its subclasses eg FieldConditionEditor easily using


Class.ForName("FieldConditionEditor").newInstance();


But

Then I have ConditionBean


and it has subclasses too. FieldCondition is one of the bean extending it...
FieldCondition




But I don't get anything ie no instance of that class or any of its subclasses. I need that Instance... :|

What am I doing wrong...

Thanks in advance,



Maki Jav


Help gets you when you need it!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

What exactly do you mean by you don't "get an instance" of for example class FieldCondition? Where and how do you expect to "get" that instance?

Can't you just do new FieldCondition();?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 435
I have explained that

Class.forName("").newInstance() is returning nothing....

and I cannot do

FieldCondition fc = new FieldCondition();


At runtime I don't know how many child classes of ConditionBean exists so my method cannot do this

for example for three classes and its corresponding editors...


This is hard-coded way to do this...

Somewhere in class ie in Constructor fill this map:

conditionEditorsMap.put(FieldCondition.class, FieldConditionEditor.class);
conditionEditorsMap.put(UserCondition.class, UserConditionEditor.class);
conditionEditorsMap.put(TaskCondition.class, TaskConditionEditor.class);

and my method
getEditorFor(ConditionBean bean){


}

then

returns coresponding editor class by queries the above map.

I am getting Editor classes initiated correctly, but not the ConditionBena classes. Any other class in the same folder is getting initiated, that I can use. There seems to be somthing wrong with theConditionBean and like wise with other Super classes EventBean and ActionBean as none of their subclasses are getting initiated. So no object ---> no work


Thanks,

Maki Jav
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Maki Jav wrote:Class.forName("").newInstance() is returning nothing....

What does that mean exactly? Does it return null? Does it throw an exception? (I guess you meant to put the name of the class between the quotes). It's certainly not "returning nothing" because the return type of the method newInstance() is Object, not void. Only void methods return nothing.

If it's an exception, then what exactly is the exception message? Please TellTheDetails - tell us exactly what code you tried to compile and run and tell us exactly what happened when you tried, and how this differs from what you expected.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4444
    
    5

I have seen this kind of thing before and if it goes for you like it did for those others, it's not going to end well.

1. Using Class.forName().newInstance() adds complexity. A lot.
2. There is a circular dependency between your factory and the class of objects that it creates (Edit: Actually, I'm not sure I understand what your factory actually creates)
3. You're trying to do way too much without having something that actually works yet.
4. You probably don't have any unit tests for this yet.
5. Your design/code has some "smells" (an expression that means "there's something that needs to be changed")

Here's one part that I find "smelly":

In general, when I see Class.forName(), I already start trying to look for something bad brewing. Most of the time, the smell helps reveal poor design choices. Very seldom does it reveal that the problem is just a stinky one to start with and you can't do anything else but use Class.forName(). I suspect it might be a little of both for what you're trying to do.

There are a number of things I would do if I were in your shoes but I don't know if you'd be willing to start over, which is what my first step would be.


Junilu - [How to Ask Questions] [How to Answer Questions]
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 435



This is the result these calls are:

System.out.println(Class.forName("rule.editor.TaskConditionEditor").newInstance());
---> rule.editor.TaskConditionEditor@1a679b7



System.out.println(Class.forName("rule.model.Rule").newInstance());
--->rule.model.Rule@fd54d6
System.out.println(Class.forName("rule.model.FieldCondition").newInstance());
---> NOTHING DISPLAYED
System.out.println(Class.forName("com.workstream.tornado.beanmodel.rule.model.EnableFieldsAction").newInstance());
----> nullnull
System.out.println(Class.forName("com.workstream.tornado.beanmodel.rule.model.UserCondition").newInstance());
---> -1 (This I had set in the class as a int field value)

however in the smae code that I am using for printing these, I added last of lines as
ConditionBean c = (ConditionBean) Class.forName("rule.model.FieldCondition").newInstance();
System.out.println(c.getName());

And I have an abstract method in ConnectionBean by that name (ie getName()) and it's implemention in FieldCondition class is ....
public String getName() {
return "Field Condition is here";
}

So adding above line's I got this in the output:
----> com.workstream.tornado.beanmodel.rule.model.Rule@16672d6
Field Condition is here
nullnull
-1
Field Condition is here

Maki Jav


Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4444
    
    5

Maki Jav wrote:
This is the result these calls are:

System.out.println(Class.forName("rule.editor.TaskConditionEditor").newInstance());
---> rule.editor.TaskConditionEditor@1a679b7

Ok. This looks like the output from the default implementation of toString(), inherited from Object. So it looks like an object was instantiated. Did you have a specific question about this?
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 435

please read above post

only that part was posted by twist of faith to which you have replied...

Thanks,

Maki Jav
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4444
    
    5

I can't get past your use of Class.forName(); I'm sorry but I don't think I'll be able to provide further assistance or advice besides what I already gave above. Maybe someone else will come along and offer to help. And please check your private messages, I just sent you an important note. Good luck.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

You have to show actual real code that we can copy and past and run that shows the error: an SSCCE . You are leaving something out that is preventing anyone from understanding your problem. There could be a number of things - for example your error handling, or an overriden toString() method. We won't know until you show an example of what does work + an example of what doesn't work in as short as possible but compileable example


Steve
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Maki Jav wrote: System.out.println(Class.forName("rule.model.FieldCondition").newInstance());
---> NOTHING DISPLAYED

That only means that class FieldCondition has an overridden toString() method that returns an empty string.

It does not mean that you can't instantiate class FieldCondition. The newInstance() method is still returning a FieldCondition object.

Did you expect that if you print the object with System.out.println(), it would call the getName() method on your FieldCondition object and print what that returns? Well, it doesn't. It calls toString(), not getName().
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 435
You are very right Jesper de Jong

The class is available for further use but my subclass must have something that is making it behave that way.

ConditionBean a = Class.forName("rule.model.FiledCondition").newInstance();
System.out.println(a.getName()+" "+a.getClass().equals(FieldCondition.class));

This is returning true

Thank you all,

Maki Jav

Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 435
Moderators please remove com.workstream from my messages from this post.

Thank you,

Maki Jav
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37985
    
  22
After two weeks it will be too late to remove anything, since it will already be logged in archiving websites.
 
Consider Paul's rocket mass heater.
 
subject: Why am I not able to get Instance of this subclasses?
 
Similar Threads
Beans binding with local variable
Panels do not get repainted
GWT - DataObject was not serializable and has no concrete serializable subtypes
Stripes errors duplicated in JSP page
Struts 2 and authentication