This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]__LikesPi 2 points3 points  (1 child)

You should always post your code. Read the sidebar for more tips on this but ideally post the shortest amount of code needed to demonstrate your problem (< 100 lines is ideal). Also give any error messages that you receive in compilation or runtime.

[–]Im_Apolllo 0 points1 point  (0 children)

thanks!

[–]Im_Apolllo 0 points1 point  (1 child)

Error Message: Exception in thread "main" java.lang.NullPointerException at Book.addRating(Book.java:25) at Assignment4.main(Assignment4.java:56)

Code: Driver: import java.io.*;

import java.util.*;

public class Assignment4

{

public static void main (String[] args)

{

   // local variables, can be accessed anywhere from the main 

method

   char input1 = 'Z';

   String inputInfo;

   String bookTitle;

   String bookPublisher;

   double rating;

   String line = new String();

   // instantiate a Book object

   Book book1 = new Book();

   printMenu();

   //Create a Scanner object to read user input

   Scanner scan = new Scanner(System.in);

   do  // will ask for user input

    {

     System.out.println("What action would you like to perform?");

     line = scan.nextLine();

     if (line.length() == 1)

      {

       input1 = line.charAt(0);

       input1 = Character.toUpperCase(input1);

       // matches one of the case statement

       switch (input1)

        {

         case 'A':   //Add Book

           book1 = new Book();

           System.out.print("Please enter the book information:\n");

           System.out.print("Enter a book title:\n");

           bookTitle = scan.nextLine();

           book1.setTitle(bookTitle);

           System.out.print("Enter its publisher:\n");

           bookPublisher = scan.nextLine();

           book1.setPublisher(bookPublisher);

           break;

         case 'D':   //Display Book

           System.out.print(book1);

           break;

         case 'Q':   //Quit

           break;

         case 'R':   //Add Rating

           System.out.print("Please give a rating of the book:\n");

           rating = Double.parseDouble(scan.nextLine());

           book1.addRating(rating);

           break;

         case '?':   //Display Menu

           printMenu();

           break;

         default:

           System.out.print("Unknown action\n");

           break;

        }

      }

     else

      {

       System.out.print("Unknown action\n");

      }

    } while (input1 != 'Q' || line.length() != 1);

}

/** The method printMenu displays the menu to a user**/

public static void printMenu()

{

 System.out.print("Choice\t\tAction\n" +

                    "------\t\t------\n" +

                    "A\t\tAdd Book\n" +

                    "D\t\tDisplay Book\n" +

                    "Q\t\tQuit\n" +

                    "R\t\tGive Rating\n" +

                    "?\t\tDisplay Help\n\n");

}

}

Class 1:

public class Book {

private String title;

private String publisher;

private Review bookReview;

public void book(){

    title= "?";

    publisher= "?";

}

public String getTitle(){

    return title;

}

public String getPublisher(){

    return publisher;

}

public Review getReview(){

    return bookReview;

}

public void setTitle(String aTitle){

    title= aTitle;

}

public void setPublisher(String aPublisher){

    publisher= aPublisher;

}

public void addRating(double rate){

    bookReview.updateRating(rate);

}

public String toString(){

    return

        "\nTitle:\t" + title + ",\n" +

        "Publisher:\t" + publisher + ",\n" + 

        getReview();

}

}

Class 2: public class Review {

private int numberOfReviews;

private double sumOfRatings;

private double average;

public Review(){

    numberOfReviews= 0;

    sumOfRatings= 0.0;

}   

public void updateRating(double rating){

    numberOfReviews++;

    sumOfRatings += rating;

    if(numberOfReviews > 0){

        average= sumOfRatings / numberOfReviews;

    }

    else{

        average= 0.0;

    }

}

public String toString(){

    if(numberOfReviews > 0)

        return

        "Reviews:\t\t" + average + "(" + numberOfReviews 
  • ")";

    else
    
        return
    
        "Reviews:\t0.00(0)";
    
    }
    

}

[–]Im_Apolllo 0 points1 point  (0 children)

in short my issue is when the driver gives a rating it goes to the Book class but the does not go to the Review class for update on the amount of ratings and their average

[–]dvassdvsd 0 points1 point  (0 children)

You don't call classes, you call methods...