【发布时间】:2016-01-10 17:56:51
【问题描述】:
我创建了一个名为“Book”的 Java 类。使用此类,我愿意更新有关新对象“book1”的信息。我还想将作者信息添加到对象“book1”中。因此,我使用名为“Author[]”的类数组动态分配内存。我的意思是有一个单独的代码,我在其中创建了一个名为“Author”的类,它有自己的一组实例变量。我现在不参与。但是,当我使用另一个名为“TestBook”的类测试“Book”类时,没有编译错误,但是当我运行代码时,我在控制台窗口中收到以下消息:
Exception in thread "main" java.lang.NullPointerException
at Book.addAuthors(Book.java:34)
at TestBook.main(TestBook.java:12)
“书”的代码如下所示:
public class Book {
private String name;
private Author[] A = new Author[];
private int numAuthors = 0;
private double price;
private int qtyInStock;
public Book(String n, Author[] authors, double p) {
name = n;
A = authors;
price = p;
}
public Book(String n, Author[] authors, double p, int qIS) {
name = n;
A = authors;
price = p;
qtyInStock = qIS;
}
public Book(String n, double p, int qIS) {
name = n;
price = p;
qtyInStock = qIS;
}
public String getName() {
return name;
}
/*
public Author getAuthors() {
return A;
}
*/
public void addAuthors(Author newAuthor) {
A[numAuthors] = newAuthor; // THIS LINE IS WHERE THE ERROR POINTS TO
++numAuthors;
}
public void printAuthors() {
/*
for (int i = 0; i < A.length; i++) {
System.out.println(A[i]);
}
*/
for (int i = 0; i < numAuthors; i++) {
System.out.println(A[i]);
}
}
public void setPrice(double p) {
price = p;
}
public double getPrice() {
return price;
}
public void setqtyInStock(int qIS) {
qtyInStock = qIS;
}
public int getqtyInStock() {
return qtyInStock;
}
/*
public String getAuthorName() {
return A.getName();
}
public String getAuthorEmail() {
return A.getEmail();
}
public char getAuthorGender() {
return A.getGender();
}
*/
public String toString() {
/*
return getName() + " " + getAuthor() + " Book price: " + getPrice() +
" Qty left in stock: " + getqtyInStock();
*/
//return getName() + " is written by " + A.length + " authors.";
return getName() + " is written by " + numAuthors + " authors.";
}
}
'TestBook'的代码如下所示:
public class TestBook {
public static void main(String args[]) {
//Author[] authors = new Author[2];
//authors[0] = new Author("Tapasvi Dumdam Thapashki", "tapasvi@thapashki.com", 'M');
//authors[1] = new Author("Paul Rand", "paulie@aol.com", 'M');
Book book1 = new Book("The Quickie Man", 69.00, 5);
//System.out.println(book1.toString());
//book1.setqtyInStock(5);
//System.out.println(book1.toString());
//System.out.println(book1.getAuthorName() + " " + book1.getAuthorEmail());
//book1.printAuthors();
book1.addAuthors(new Author("Linda Lee", "lindalee@grinchtown.com", 'F'));
book1.addAuthors(new Author("Joseph Caputo", "caputo@lfp.com", 'M'));
System.out.println(book1.toString());
book1.printAuthors();
}
}
“作者”的代码如下所示:
public class Author {
private String name;
private String email;
private char gender;
public Author(String n, String e, char g) {
name = n;
email = e;
gender = g;
}
public String getName() {
return name;
}
public void setEmail(String e) {
email = e;
}
public String getEmail() {
return email;
}
public char getGender() {
return gender;
}
public String toString() {
return getName() + " [" + getGender() + "] " + getEmail();
}
}
我需要一些帮助。
【问题讨论】:
-
private Author[] A = new Author[];是什么意思???? -
我建议您将
Author[] A替换为List<Author> authors,这样您就无需调整大小或跟踪作者的数量。