[英]Creating an array of inherited objects
i'm trying to create a method that adds a certain object into an array of inherited objects. 我正在尝试创建一种将特定对象添加到继承对象数组中的方法。
public class Biblio {
Biblio[] Tab; static int i=0;
Biblio();
void insert(Biblio O){Tab[i]=O;i++;}}
in the main class, i created 3 objects of classes that extend from each others: means Document extends from Biblio, Article extends from Document, Book extends from Article. 在主类中,我创建了3个相互延伸的类对象:意味着Document从Biblio扩展,Article从Document扩展,Book从Article扩展。
public class TestBiblio {
public static void main(String[] args) {
Document A= new Document();
Article B= new Article();
Book C= new Book();
Biblio D= new Biblio();
D.insert(A);
D.insert(B);
D.insert(C);}}
Once i run the code, i get Exception in thread "main" java.lang.NullPointerException error. 一旦运行代码,我就在线程“ main” java.lang.NullPointerException错误中得到异常。 i'm a beginner in java, i couldn't find out hat went wrong..
我是Java的初学者,我找不到帽子出了错。
You never initialized the array that you used to insert. 您从未初始化过用于插入的数组。 When you do Tab[i], you are dereferencing a null pointer.
当您执行Tab [i]时,您将取消引用空指针。 Have something like
有类似的东西
public class Biblio {
Biblio[] Tab;
static int i=0;
public Biblio() {
Tab = new Biblio[5];
}
void insert(Biblio O){
Tab[i]=O;i++;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.