未提供时,不显示作者值

时间:2017-03-31 15:50:37

标签: java

我正在尝试创建一个java程序,其中我有一个超类ReadingMaterial和许多不同的子类,即Newspaper,Manga,Novel和TextBook。现在,Readingmaterial有3个数据成员,每个其他类每个都有2个不同的数据成员。

我正在尝试显示我正在创建的字符串,例如:

allMat.add(new Newspaper("NewYork_Times"));

我想要“NewYork_Times”和日期,作者要显示 谁能告诉我我做错了什么?

ps:我是JAVA编程新手

报纸课

public class Newspaper
        extends ReadingMaterial {

    private String title = "BHCC_Newspaper";

    //public Newspaper(){}
    public Newspaper(String title) {
        this.title = title;
    }

    public Newspaper(String title, String author, boolean readingMaterial) {
        super(author, readingMaterial);
        this.title = title;
    }

    //getter
    public String getTitle() {
        return title;
    }

    // setter
    public void setTitle(String title) {
        this.title = title;
    }

    public void displayReadingMaterials() {
        System.out.println(getTitle() + "\n Microsoft recruited 15 students from a community college.\n");
    }
}

这是超类

public class ReadingMaterial implements Comparable {

    private String author = "Lydie";
    private boolean readingMaterial;
    private java.util.Date dateCreated;

    // no-arg constructor
    public ReadingMaterial() {
        dateCreated = new java.util.Date();
    }
    // arg constructor

    public ReadingMaterial(String author, boolean readingMaterial) {
        dateCreated = new java.util.Date();
        this.author = author;
        this.readingMaterial = readingMaterial;
    }
    // getters

    public String getAuthor() {
        return author;
    }

    public java.util.Date getDateCreated() {
        return dateCreated;
    }

    public boolean getReadingMaterial() {
        return readingMaterial;
    }
    // setters

    public void setAuthor(String author) {
        this.author = author;
    }

    public void setReadingMaterial(boolean readingMaterial) {
        this.readingMaterial = readingMaterial;
    }

    public void displayReadingMaterials() {
        System.out.println("Written on " + getDateCreated() + " by " + getAuthor()
                + " on all ReadingMaterial: " + getReadingMaterial());
    }

//Comparator for sorting the list
     public static  Comparator <ReadingMaterial> testCompare = new Comparator <ReadingMaterial>(){

             public int compare(ReadingMaterial readMat1, ReadingMaterial readMat2){


             return (int) (readMat1.getAuthor().compareTo(readMat2.getAuthor()));
             }
     };
    @Override
    public String toString() {
        return "Written on " + dateCreated + " by " + author
                + " on all ReadingMaterial: " + readingMaterial;
    }

    @Override
    public int compareTo(Object t) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }



}

这是主要的

package readingmaterial;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.Collections;


public class ReadingFactory {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        // create and initialize a newspaper, a manga, a textbook and a novel

        List<ReadingMaterial> allMat = new ArrayList<ReadingMaterial>();
        allMat.add(new Newspaper("NewYork_Times"));
        allMat.add(new Manga("Naruto"));
        allMat.add(new TextBook("Bible"));
        allMat.add(new Novel("Argonauts"));
        //Iterator<ReadingMaterial> readMat = all.iterator();

        System.out.println("ArrayList elements after sorting in ascending order : ");

       Collections.sort(allMat, ReadingMaterial.testCompare);

       for(ReadingMaterial str: allMat){
            System.out.println(str);

        //ReadingMaterial[] allMat={newspaper,manga,textbook,novel};
        //for(int i=0;i<4;i++){
          //   allMat[i].displayReadingMaterials();
        // }
    }
    // this method show the polymorphic call and display any reading material

}

1 个答案:

答案 0 :(得分:2)

问题在于,当您执行new Newspaper("NewYork_Times")时,您使用的是默认的超级构造函数,没有args,这意味着author未设置。
因此,您在比较器上获得空指针(getAuthor返回null):

readMat1.getAuthor().compareTo(readMat2.getAuthor())

你应该:
1.将空检查添加到比较器中 2.如果您指望作者进行比较,则不能允许它为空。没有它的构造函数,或者设置默认值(就像你使用日期一样)