• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Maki Jav
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();?
 
Maki Jav
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7291
44
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maki Jav
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Pie
Posts: 7291
44
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

please read above post

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

Thanks,

Maki Jav
 
Junilu Lacar
Bartender
Pie
Posts: 7291
44
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moderators please remove com.workstream from my messages from this post.

Thank you,

Maki Jav
 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After two weeks it will be too late to remove anything, since it will already be logged in archiving websites.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic