我想反映MyRow的一个对象然后获取A MyTable对象,我该怎么办?
这是我的java代码:
import java.util.*;
import java.text.*;
import java.util.concurrent.*;
import java.lang.reflect.*;
class MyTable {
private MyRows rows;
private List columns;
public List getColumns(){return this.columns;}
}
class MyRows extends ArrayList<MyRow> {//...
}
class MyRow {
protected MyTable table;
private List<Object> data;
private MyRow(MyTable p_table) {
this.table = p_table;
this.data = Collections.synchronizedList(new ArrayList(this.table.getColumns().size()));
}
}
public class Test2 {
public static void main(String args[]){
try{
MyTable myTable = new MyTable();
MyRows myRows = new MyRows();
Constructor ctor = MyRow.class.getDeclaredConstructor(MyTable.class);
ctor.setAccessible(true);
MyRow myRow = (MyRow)ctor.newInstance(myTable); // Error: ava.lang.reflect.InvocationTargetException ??? how should I do here
System.out.println(myRow);
myRows.add(myRow);
// myTable.rows = myRows;
}catch(Exception e){ e.printStackTrace();}
}
}
我不知道为什么得到这个例外,谁能帮助我,请:(
F:\ java&gt; javac Test2.java ...
F:\ java&gt; java Test2 java.lang.reflect.InvocationTargetException at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
RCE) at java.lang.reflect.Constructor.newInstance(Unknown Source) 在Test2.main(Test2.java:34)引起:java.lang.NullPointerException 在MyRow。(Test2.java:22) ......还有5个
F:\ java的
答案 0 :(得分:6)
MyTable
未初始化rows
变量,因此当您尝试在其上调用方法时,您会得到InvocationTargetException
。如果你看近底部,你会发现它是由NullPointerException
引起的。