英文:
How to use the toString method in my runner?
问题
我正在尝试学习Java,现在我创建了这个Book类,我在Eclipse中使用了Generate toString()选项,现在我想在我的运行程序中调用toString方法。如何调用?
我尝试使用这个作为运行程序,但是它给了我一个错误:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.library.entity.Author.setFirstName(String)" because "this.author" is null
at com.library.entity.Publication.<init>(Publication.java:14)
at com.library.entity.publication.Book.<init>(Book.java:12)
at com.library.runner.BookstoreRunner.main(BookstoreRunner.java:9)
这是运行程序:
package com.library.runner;
import java.time.LocalDateTime;
import com.library.entity.publication.*;
public class BookstoreRunner {
public static void main(String args[]) {
Book book1 = new Book("Generic Title", "K", "L", "K Publishing", "Earth", LocalDateTime.now());
String bookdetails = book1.toString();
System.out.println(bookdetails);
}
}
这是我的Publication类,希望有所帮助。我真的很新到Java。
package com.library.entity;
import java.time.LocalDateTime;
public class Publication {
private String title;
private Author author;
private Publisher publisher;
private LocalDateTime datePublished;
public Publication(String title, String firstName, String lastName, String publisherName, String address, LocalDateTime datePublished) {
this.title = title;
author.setFirstName(firstName);
author.setLastName(lastName);
publisher.setPublisherName(publisherName);
publisher.setAddress(address);
this.datePublished = datePublished;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public LocalDateTime getDatePublished() {
return datePublished;
}
public void setDatePublished(LocalDateTime datePublished) {
this.datePublished = datePublished;
}
}
编辑:它现在工作了!我的错误是使用了:
private Author author;
private Publisher publisher;
而不是
private final Author author = new Author();
private final Publisher publisher = new Publisher();
英文:
I'm trying to learn Java and now I created this Book class, I used the Generate toString() option in Eclipse and now I want to call the toString method in my runner. How do I call it?
import java.time.LocalDateTime;
import com.library.entity.Publication;
public class Book extends Publication {
public Book(String title, String firstName, String lastName, String publisherName, String address,
LocalDateTime datePublished) {
super(title, firstName, lastName, publisherName, address, datePublished);
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Book [getTitle()=" + getTitle() + ", getDatePublished()=" + getDatePublished() + ", getClass()="
+ getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]";
}
}
I tried using this as a runner but it gives me an error:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.library.entity.Author.setFirstName(String)" because "this.author" is null
at com.library.entity.Publication.<init>(Publication.java:14)
at com.library.entity.publication.Book.<init>(Book.java:12)
at com.library.runner.BookstoreRunner.main(BookstoreRunner.java:9)
Here is the runner:
package com.library.runner;
import java.time.LocalDateTime;
import com.library.entity.publication.*;
public class BookstoreRunner{
public static void main(String args[]) {
Book book1 = new Book("Generic Title", "K", "L", "K Publishing", "Earth", LocalDateTime.now());
String bookdetails = book1.toString();
System.out.println(bookdetails);
}
}
Here is my Publication class, I hope it helps. I'm really new to java.
package com.library.entity;
import java.time.LocalDateTime;
public class Publication {
private String title;
private Author author;
private Publisher publisher;
private LocalDateTime datePublished;
public Publication(String title, String firstName, String lastName, String publisherName, String address, LocalDateTime datePublished) {
this.title = title;
author.setFirstName(firstName);
author.setLastName(lastName);
publisher.setPublisherName(publisherName);
publisher.setAddress(address);
this.datePublished = datePublished;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public LocalDateTime getDatePublished() {
return datePublished;
}
public void setDatePublished(LocalDateTime datePublished) {
this.datePublished = datePublished;
}
}
edit: It now works! My error was using:
private Author author;
private Publisher publisher
instead of
private final Author author = new Author();
private final Publisher publisher = new Publisher();
答案1
得分: 2
你已经快要完成了!你应该将你的代码放在一个方法运行器内部:
public class BookstoreRunner{
public static void main(String... args) {
Book book1 = new Book("通用标题", "K", "L", "K 出版社", "地球",
LocalDateTime.now());
String bookdetails = book1.toString();
System.out.println(bookdetails);
}
}
附注:当你调用System.out.println(new Book())
时,book.toString()
方法会隐式地被调用,无需手动调用。
附注1:Publication
存在一个问题。在构造函数中访问author
变量之前,应先进行初始化:
private final Author author = new Author();
private final Publisher publisher = new Publisher();
public Publication(String title, String firstName, String lastName, String publisherName, String address, LocalDateTime datePublished) {
// ...
}
英文:
You're almost there! You shoul wrap your code inside runner with some method:
public class BookstoreRunner{
public static void main(String... args) {
Book book1 = new Book("Generic Title", "K", "L", "K Publishing", "Earth",
LocalDateTime.now());
String bookdetails = book1.toString();
System.out.println(bookdetails);
}
}
P.S. Whe you call System.out.println(new Book())
then book.toString()
method is called implicitly and no need to invoke it manually.
P.S.1 Publication
has a problem. author
variable should be initialized before it will be accessed in the constructor:
private final Author author = new Author();
private final Publisher publisher = new Publisher();
public Publication(String title, String firstName, String lastName, String publisherName, String address, LocalDateTime datePublished) {
// ...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论