2

Is there any way to create and return an object based on a passed string in Java? I want to create objects that are subclasses of a Package class, and do this by calling a method that passes the class name. I could accomplish this with a switch statement easily enough, but if I do it this way, when I add more subclasses of Packages I won't have to adjust the switch statement.

Here's what I have for code:

Package Variable:

private Package testPackage;

Method Call:

createPackage(testPackage, "TestPackage");

And the method:

Object createPackage(Object curPackage, String packageName)
{   
    Object object = null;

    try
    {
        Class<?> aClass = Class.forName("a.b.c.packages." +packageName);
        Constructor<?> constructor = aClass.getConstructor(new Class[] { String.class });
        object = constructor.newInstance(new Object[] { packageName });
    } catch (Exception e)
    {
        e.printStackTrace();
    }

    return object;
}

The class is found, but after I run this method and try to call

Log.v(TAG, testPackage.getName());

I get a null pointer exception. I am writing Android code, I don't think that makes a difference in this situation, though.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Ben Kane
  • 9,331
  • 6
  • 36
  • 58
  • "I want to create objects that are subclasses of a Package class, and do this by calling a method that passes the class name" *sniff* *sniff* looks like code smell... What do you want to do exactly? – fge Jun 06 '13 at 18:39
  • http://stackoverflow.com/questions/6094575/creating-an-instance-using-the-class-name-and-calling-constructor you can find better reference here. – Triode Jun 06 '13 at 18:44
  • I've already been to that page. That's what I modeled my code after. – Ben Kane Jun 06 '13 at 18:54
  • And it's not a code smell, it will save a lot of code if I can implement this short method. – Ben Kane Jun 06 '13 at 19:02

2 Answers2

1

You have a NullPointerException because you don't initialize testPackage.

And you have to cast the result :

 testPackage = (Package)createPackage("TestPackage")

(and you can remove the first parameter, which is not used)

Thierry
  • 5,133
  • 3
  • 25
  • 30
  • When I use `testPackage = createPackage(testPackage, "TestPackage");` I get a "Type mismatch: cannot convert from Object to Package" – Ben Kane Jun 06 '13 at 19:13
  • Can you post the stacktrace of the NullPointerException? – Thierry Jun 06 '13 at 19:15
  • Could I do this with generics? I'm not exactly sure what I'd need to do to set that up. I have used generics before but definitely not in this situation. – Ben Kane Jun 06 '13 at 19:20
  • now I get a ClassCastException. I did try this solution earlier as well. – Ben Kane Jun 06 '13 at 19:23
  • If you have a ClassCastException, check that a.b.c.packages.TestPackage is a subclass of Package – Thierry Jun 06 '13 at 19:26
  • Thanks for your help everyone. I for some reason didn't import a.b.c.packages.* so I was having scope issues. I've fixed the problem by Casting to (Package) as I had tried earlier and Thierry and @tkeE2036 suggested. Glad to have finally figured out this simple problem! Thanks again everyone. I'm posting this as a comment since I don't have high enough reputation to answer my own question yet.. – Ben Kane Jun 06 '13 at 19:34
  • Your answer wasn't the answer to my problem though...maybe I will just to be nice. I don't want to mislead people though – Ben Kane Jun 06 '13 at 21:06
0

So I believe your problem is that you never assign your variable curPackage in your method (at least from what I can tell with the code you've written). You need to assign your testPackage, ie:

private Package testPackage = (Package) createPackage("TestPackage");

At this point you can just remove your Object parameter since its never used in the method.

Ian Dallas
  • 12,451
  • 19
  • 58
  • 82