-1

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();
kajl16
  • 27
  • 5

1 Answers1

2

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) {
    // ...
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
  • Yeah I goofed up on that one but now I get a new 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.(Publication.java:14) at com.library.entity.publication.Book.(Book.java:12) at com.library.runner.BookstoreRunner.main(BookstoreRunner.java:9)` – kajl16 Oct 18 '20 at 11:40
  • @kajl16 this is nother problem. you have to look at `Publication` source – Oleg Cherednik Oct 18 '20 at 11:42
  • I updated the post with the publication class, I hope you can help spot my error. – kajl16 Oct 18 '20 at 11:45
  • @kajl16 Please ask a separate question for this. Editing the question to include another question can invalidate existing answers, and maybe even make this question open for closure by "Needs more focus". – shreyasm-dev Oct 18 '20 at 11:48
  • @GalaxyCat105 how do I open this for closure? – kajl16 Oct 18 '20 at 11:56