如何在我的运行器中使用toString方法?

huangapple go评论74阅读模式
英文:

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 &quot;Book [getTitle()=&quot; + getTitle() + &quot;, getDatePublished()=&quot; + getDatePublished() + &quot;, getClass()=&quot;
+ getClass() + &quot;, hashCode()=&quot; + hashCode() + &quot;, toString()=&quot; + super.toString() + &quot;]&quot;;
}
}

I tried using this as a runner but it gives me an error:

Exception in thread &quot;main&quot; java.lang.NullPointerException: Cannot invoke &quot;com.library.entity.Author.setFirstName(String)&quot; because &quot;this.author&quot; is null
at com.library.entity.Publication.&lt;init&gt;(Publication.java:14)
at com.library.entity.publication.Book.&lt;init&gt;(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(&quot;Generic Title&quot;, &quot;K&quot;, &quot;L&quot;, &quot;K Publishing&quot;, &quot;Earth&quot;, 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()方法会隐式地被调用,无需手动调用。

附注1Publication存在一个问题。在构造函数中访问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(&quot;Generic Title&quot;, &quot;K&quot;, &quot;L&quot;, &quot;K Publishing&quot;, &quot;Earth&quot;, 
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) {
// ...
}

huangapple
  • 本文由 发表于 2020年10月18日 19:25:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64412737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定