public class Star{
public static ArrayList initdata(String pattern) {
ArrayList data = new ArrayList();
if (pattern != "") {
ModelCollection mc = Star.find(pattern, 0);
Iterator dataIterator = mc.iterator();
while (dataIterator.hasNext()) {
Star star = (Star) dataIterator.next();
data.add(star.getName());
Debug.trace("StarName" + star.getName());
}
}
Collections.sort(data);
return data;
}
}
I want to invoke method initdata using reflection, I tried to write something like this , but it does not work:
Class c = Class.forName("com.cubiware.fyretv.application.model.Star");
par[0] = String.class;
Method mthd = c.getMethod("initdata", par);
ArrayList output = (ArrayList) mthd.invoke(null, null);
First, Your check seems weird to me: try
if (pattern != null)
instead ofif (pattern != "")
.Why don't you pass the
par
array, you have illegal argument exception I think. try passing arguments array.try
It's not good idea to pass null, when method expects Object...
May be this will help
Calling Java varargs method with single null argument?
Obviously, your invoke call is similiar to
Now, inside
initdata
you do not filter the case wherepattern == null
which leads us to a callWe do not know the implementation of this method - if we're lucky, we get an empty collection. Otherwise, I expect a
NullPointerException
either inStar.find
or later atmc.iterator()