0

I am a complete newbie when it comes to programming and my first post so please forgive all my errors in advance. I am trying to figure out why when I println information from an array containing variables from a superclass and subclass I get null and other jargon. I know this specifically has to do with the Ebook subclass I have added, but just seem to be completely stuck the last several days. I hope someone here doesn't mind taking a look at this. Thanks in advance!

import java.util.Arrays;
import java.text.NumberFormat;
import java.util.Locale;
import java.text.DecimalFormat;

public class Bookstore2_3
{
public static void main(String args[])
{

    NumberFormat usCurrency = NumberFormat.getCurrencyInstance(Locale.US);
    DecimalFormat simpleformat = new DecimalFormat("#,###.#");

    String[] titles;
    titles = new String [5];

    titles [0] = new String ("David Goes to School");
    titles [1] = new String ("No David!");
    titles [2] = new String ("Simple Abundance");
    titles [3] = new String ("The very hungry caterpillar");
    titles [4] = new String ("We are going on a bear hunt");        

    Book sortedBooks[] = new Book[5];
    sortedBooks [0] = new EBook (0075260012l, "David goes to School", "David Shannon", 2010, "Shannon Rock", 12, "www.anything.com", 1);
    sortedBooks [1] = new Book (7423540089l, "No David!", "David Shannon", 2009, "Shannon Rock", 13);
    sortedBooks [2] = new Book (0743200616l, "Simple Abundance", "Sarah Breathnach", 2009, "Scribner", 15);
    sortedBooks [3] = new EBook (78137521819l, "The very hungry caterpillar", "Eric Carle", 2005, "Philomel Books", 14, "http://www.tinyurl.fguopt8u90", 1);
    sortedBooks [4] = new Book (9781416987116l, "We are going on a bear hunt", "Michael Rosen", 2009, "McElderry", 16);

    System.out.println(Arrays.toString(sortedBooks));

}
} // end class Bookstore

// Begin class Book
class Book
{
public float isbn;
public String title;
public String authorName;
public float yearPublished;
public String publisherName;
public float price;

public Book ()
{
    isbn = 0;
    title = "";
    authorName = "";
    yearPublished = 0;
    publisherName = "";
    price = 0;
}

public Book (float bookisbn, String bookTitle, String bookauthorName, float bookyearPublished, String bookpublisherName, float bookPrice)
{
    isbn = bookisbn;
    title = bookTitle;
    authorName = bookauthorName;
    yearPublished = bookyearPublished;
    publisherName = bookpublisherName;
    price = bookPrice;
}


///////////////////////////////////////////////
public void setISBN (float ISBN) //set ISBN
{
    this.isbn = ISBN;
}
public float getISBN () //get ISBN
{
    return isbn;
}
//////////////////////////////////////////////
public void setTitle (String Title) //set Title
{
    this.title = Title;
}
public String getTitle () //get Title
{
    return title;
}
///////////////////////////////////////////////
public void setAuthorName (String AuthorName) //set AuthorName
{ 
    this.authorName = AuthorName;
}
public String getAuthorName () //get AuthorName
{
    return authorName;
}
///////////////////////////////////////////////
public void setYearPublished (float YearPublished)//set YearPublished
{
    this.yearPublished = YearPublished;
}
public float getYearPublished () //get YearPublished
{
    return yearPublished;
}
///////////////////////////////////////////////
public void setPublisherName (String PublisherName)
{
    this.publisherName = PublisherName;
}
public String getPublisherName ()
{
    return publisherName;
}
///////////////////////////////////////////////
public void setPrice (float Price)
{
    this.price = Price;
}
public float getPrice ()
{
    return price;
}

} // end class Book

//Begin class EBook
class EBook extends Book
{
public String webSite;
public float discountRate;
public float discountOff;

public EBook (float isbn, String title, String authorName, float yearPublished, String publisherName, float price, String webSite, float discountRate)
{
    super(isbn, title, authorName, yearPublished, publisherName, price);
    webSite = "";
    discountRate = 2;
}
public EBook (String EBookWebSite, float EBookDiscount)
{
    webSite = EBookWebSite;
    discountRate = EBookDiscount;
}   


public void setWebSite (String WebSite)
{
    this.webSite = WebSite;
}
public String getWebSite ()
{
    return webSite;
}
public float discountOff (float discountRate, float price) //method to calculate discount off
{
    float discountOff = discountRate * price;
    return discountRate * price;
}
public String toString ()
{
    return "ISBN: " + isbn + "\n" + "Title: " + title + "\n" + "Author's Name: " + authorName + "\n" + "Year Published: " + yearPublished + "\n" + "Publisher's Name: " + publisherName + "\n" + "Price: " + price + "\n" + "Website: " + webSite + "\n" + "Discount: " + discountRate + "\n";
}

}
4
  • Please be more specific with the error than "null and other jargon". Commented Jan 21, 2014 at 1:26
  • Don't forget to override the toString() method in your Book class. Commented Jan 21, 2014 at 1:27
  • Wow! Hi everyone and thanks for the really quick feedback already! What I am trying to do is display all of the information within the array. It does compile properly, but when actually running it, anything with a website responds Null and the Discount says Zero. Website and Discount are members specific to the subclass of Ebook so I think it has to do with that, I am just stuck as to how to resolve this. Commented Jan 21, 2014 at 1:39
  • There are many other issues in this program all together, but because this is for my own learning experience I am only trying to get past the part I am currently stuck on and hopefully resolving the rest myself. The other jargon that appears when running the class is "Book@1d7e8c5b, Book@5f30b97d" Commented Jan 21, 2014 at 1:40

1 Answer 1

1

In your constructor

webSite = "";

should be

this.webSite = ""; 

or

this.webSite = webSite;

Previously you were referencing the constructor parameter named webSite, not the instance field. As such, the instance field remained null, which is the default value for an instance variable of a reference type.

Sign up to request clarification or add additional context in comments.

4 Comments

Wow, I can't believe that's all it was thank you so much for this!
@CoShark The Book@1d7e8c5b is the result of Object#toString() method which all classes inherit. You need to override it.
Thank you for pointing that out, I am just trying to figure out the Override concept now. Where would this get placed? If you do not mind showing me.
@CoShark See here. Notice your EBook class has a method public String toString(). Try adding the @Override annotation to it. Then try adding the @Override annotation to any other method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.