所以我有一個類 「的TestClass」Groovy的方法返回null
class TestClass {
public abstract class Test {
private String _string
public Test(String string) {
_string = string
}
private TreeSet<List<String>> _rows = new TreeSet<List<String>>()
public Collection<List<String>> getRows() {
return _rows
}
public abstract void processObject(XmlObject object)
}
private List<Test> list = [
new getData() //long list of classes
]
//this method is supposed to to through the list and return "_rows" which doesnt seem to work
public List<Test> returnRows() {
list.each { Test test ->
return test.getRows()
}
}
public getData extends Test {
public getData() {
super('test')
}
@Override
public void processObject(XmlObject object) {
getRows().add(['test'])
}
}
public TestClass() {
//constructor
}
}
我在這裏的另一個文件調用的TestClass:
class AnotherClass {
public void run() {
def testClass
testClass = getClass().classLoader.parseClass(new File(classpath)).newInstance()
testClass.returnRows().each { //it is failing here saying that testClass.returnRows() is null
}
}
但testClass.returnRows()爲null
我不知道,如果該方法returnRows()不返回任何東西,或者如果我沒有正確實例化類
看起來你需要學習使用調試器。請幫助你一些補充性的調試技術。 –
第一個問題是你應該調整'list.each {...}'而不是'list.each(){...}'。 –
'每個(){}'是過於冗長,但沒有問題 – cfrick