10 Replies Latest reply on Mar 4, 2003 11:08 PM by 807541

    dateFormat

    807541
      I created this in class call rental.java.

      public String getFormattedDatetimeOfSale() {     
           
                DateFormat dateFormat = DateFormat.getDateTimeInstance();
                return dateFormat.format(timeOfSale);
           }



           public String displayRental() {
                String classDescription = "Time Of Sale";
                classDescription += getFormattedDatetimeOfSale();
                classDescription += "\tNumber Of Items Rented ";
                classDescription += TotalAmountRented();
                classDescription += "\tTotal Of Sale ";
                classDescription += getFormattedRentalCost();
                classDescription += "\nThank You For Renting";
                return classDescription;
           }

      It shows fine but then when I call it on different class rentlapplication.java i got this error message

      java.lang.NullPointerException
           at java.util.Calendar.setTime(Calendar.java:902)
           at java.text.SimpleDateFormat.format(SimpleDateFormat.java:780)
           at java.text.SimpleDateFormat.format(SimpleDateFormat.java:773)
           at java.text.DateFormat.format(DateFormat.java:314)
           at Rental.getFormattedDatetimeOfSale(Rental.java:73)
           at Rental.displayRental(Rental.java:122)
           at RentalApplication.main(RentalApplication.java:60)

      the problem is at

      classDescription += getFormattedDatetimeOfSale();

      I thought I can call that like

      getFormattedRentalCost();

      but obviously not. Any Idea to fix this? I also stated the import java.

      import java.text.DateFormat;
        • 1. Re: dateFormat
          807541
          return dateFormat.format(timeOfSale);
          Here, timeOfSale must be a reference to a java.util.Date object.
          • 2. Re: dateFormat
            807541
            I import the java.util.Data on both classes but when I create

            public String displayRental() {
                      String classDescription = "Time Of Sale";
                      classDescription += getFormatteddatetimeOfSale();
                      classDescription += "\tNumber Of Items Rented ";
                      classDescription += TotalAmountRented();
                      classDescription += "\tTotal Of Sale ";
                      classDescription += getFormattedRentalCost();
                      classDescription += "\nThank You For Renting";
                      return classDescription;
                 }
            in rental.java and I call it in rentalapplication.java but creating this line

            System.out.println(theRental.displayRental());

            I got this error

            java.lang.NullPointerException
                 at java.util.Calendar.setTime(Calendar.java:902)
                 at java.text.SimpleDateFormat.format(SimpleDateFormat.java:780)
                 at java.text.SimpleDateFormat.format(SimpleDateFormat.java:773)
                 at java.text.DateFormat.format(DateFormat.java:314)
                 at Rental.getFormatteddatetimeOfSale(Rental.java:43)
                 at Rental.displayRental(Rental.java:104)
                 at RentalApplication.main(RentalApplication.java:48)
            • 3. Re: dateFormat
              807541
              what does this method look like



              at Rental.getFormatteddatetimeOfSale(Rental.java:43)

              • 4. Re: dateFormat
                807541
                NullPointerExceptions occur when you try to call a method using a variable that has not been initialized.

                The call to dateFormat.format() passes timeOfSale - timeOfSale must have been initialized with a Date object or it will evaluate to null.

                When the DateFormat class tries to use the passed null value (when it expects a valid Date), a NullPointerException is produced.

                • 5. Re: dateFormat
                  807541
                  whreturn dateFormat.format(timeOfSale);

                  which I intialized the time of sale by

                  private Date timeOfSale;

                  help I am really a newbie on this. what do I need to do?
                  • 6. Re: dateFormat
                    DrClap
                    which I intialized the time of sale by

                    private Date timeOfSale;
                    When you declare a variable of some object type (e.g. Date), but don't declare an initial value, the compiler does indeed initialize the variable -- to null. And then you try to use it, and get a NullPointerException. You need to create a Date object and assign it to that variable.

                    I would suggest you spend some time with the tutorials here:

                    http://java.sun.com/docs/books/tutorial/
                    • 7. Re: dateFormat
                      807541
                      Hi, I get this example from my instructor. I compare mind with his he is working fine but mind is not. So I think it's only a simple answer
                      • 8. Re: dateFormat
                        807541
                        Try this...

                        Debug your code and examine the value of timeOfSale before it is passed the format method. If you find that the value of timeOfSale is actually null, then you will need to determine the apropriate place to initialize timeOfSale with a Date. If timeOfSale contains a valid Date then post again with more information...
                        public String getFormattedDatetimeOfSale() {

                        DateFormat dateFormat = DateFormat.getDateTimeInstance();
                        return dateFormat.format(timeOfSale);
                        }
                        • 9. Re: dateFormat
                          807541
                          hi this my whole rental.java class

                          public class Rental {
                               //Declare variables
                               public static final double COST_OF_NEW_RELEASES = 2.25;
                               public static final double COST_OF_REGULAR_VIDEO = 1.00;
                               public static final double COST_OF_DVDS = 2.98;
                               public static final double COST_OF_GAMES = 4.50;
                               private Date timeOfSale;
                               private int NewReleases;
                               private int RegularVideo;
                               private int Dvds;
                               private int Games;
                               private String rentalReport;
                               private int customerNumber;
                               
                               
                               //Constructor method
                               
                               public Rental(
                               int theNumberOfNewReleases,
                               int theNumberOfRegularVideo,
                               int theNumberOfDvds,
                               int theNumberOfGames) {
                               NewReleases = theNumberOfNewReleases;
                               RegularVideo = theNumberOfRegularVideo;
                               Dvds = theNumberOfDvds;
                               Games = theNumberOfGames;
                               timeOfSale = new Date();
                          }

                               //Getters
                               
                                         
                               public int getNumberOfNewReleases() {
                                    return NewReleases;
                               }

                               public String getFormatteddatetimeOfSale() {
                                    DateFormat dateFormat = DateFormat.getDateTimeInstance();
                                    return dateFormat.format(timeOfSale);
                               }
                               
                               public String getRentalReport() {
                                    return rentalReport;
                               }
                               public int getNumberOfRegularVideo() {
                                    return RegularVideo;
                               }
                               public int getNumberOfDvds() {
                                    return Dvds;
                               }
                               public int getNumberOfGames() {
                                    return Games;
                               }

                               //Other Methods
                               public double rentalCost() {
                                    /**
                                    * Rental Cost()
                                    * creation date ("2/5/03" 1:30PM)
                                    */
                                    return NewReleases * COST_OF_NEW_RELEASES
                                         + RegularVideo * COST_OF_REGULAR_VIDEO
                                         + Dvds * COST_OF_DVDS
                                         + Games * COST_OF_GAMES;
                               }

                               public double TotalAmountRented() {
                                    return NewReleases + RegularVideo + Dvds + Games;

                               }

                               public String getFormattedRentalCost() {
                                    /**
                                    * rentalCost()
                                    * creation date ("1/30/03" 1:30PM)
                                    */
                                    NumberFormat dollarFormat = NumberFormat.getCurrencyInstance();
                                    return dollarFormat.format(rentalCost());
                               }

                               //     Instance Methods
                               public void addItem(RentalItems theItem) {

                                    if (theItem.getType().equals(RentalItems.NEW_TITLE)) {
                                         NewReleases += 1;
                                    }
                                    if (theItem.getType().equals(RentalItems.REGULAR_VIDEO)) {
                                         RegularVideo += 1;
                                    }
                                    if (theItem.getType().equals(RentalItems.DVDS)) {
                                         Dvds += 1;
                                    }
                                    if (theItem.getType().equals(RentalItems.GAMES)) {
                                         Games += 1;
                                    }
                               }

                               public String toString() {
                                    String classDescription = "Time of Sale = ";
                                    classDescription += getFormatteddatetimeOfSale();
                                    return classDescription;
                               }

                               public String displayRental() {
                                    String classDescription = "\tNumber Of Items Rented ";
                                    classDescription += TotalAmountRented();
                                    classDescription += getFormatteddatetimeOfSale();
                                    classDescription += "\tTotal Of Sale ";
                                    classDescription += getFormattedRentalCost();
                                    classDescription += "\nThank You For Renting";
                                    return classDescription;
                               }

                               /**
                               *
                               * Constructor for rental
                               */
                               public Rental() {
                                    super();
                               }

                               public static void main(String[] args) {
                                    Rental theRental;
                                    theRental = new Rental(3, 1, 3, 1);
                                    System.out.println(theRental.toString());
                               }
                               /**
                               * Method findRentalItems.
                               * @param thevideoId
                               * @return RentalItems
                               */
                               public static RentalItems findRentalItems(String thevideoId) {
                                    return null;
                               }
                               /**
                               * Method addRental.
                               * @param theRental
                               */
                               public void addRental(Rental theRental) {
                               }
                          }

                          then this is my rental application class


                          public class RentalApplication {

                               public static void main(String[] args) {
                                    //          declarations
                                    RentalItems theRentalItems;
                                    Rental theRental = new Rental();
                                    VideoStore theVideoStore = new VideoStore();
                                    NumberFormat numberFormat = NumberFormat.getCurrencyInstance();
                                    DateFormat dateFormat = DateFormat.getDateTimeInstance();
                                    int customerNumber = 0;
                                    String thevideoId;
                                    boolean newCustomer = true;
                                    boolean moreLookUps = true;
                                    customerNumber++;
                                    System.out.println("Customer Number:" + customerNumber);
                                    while (newCustomer) {
                                         customerNumber++;
                                         thevideoId = JOptionPane.showInputDialog("Enter Video Id's");
                                         theRentalItems = RentalItems.findRentalItems(thevideoId);
                                         theRental.addItem(theRentalItems);
                                         System.out.println(theRentalItems.displayRental());
                                         moreLookUps = getOption("Another Videos?");

                                         while (moreLookUps) {

                                              thevideoId = JOptionPane.showInputDialog("Enter Video Id's");
                                              theRentalItems = RentalItems.findRentalItems(thevideoId);
                                              theRental.addItem(theRentalItems);
                                              System.out.println(theRentalItems.displayRental());
                                              moreLookUps = getOption("Another Videos?");

                                         }
                                         theVideoStore.addRental(theRental);
                                         System.out.println(theRental.displayRental());
                                         System.out.println("\nCustomer Number:" + customerNumber);
                                         moreLookUps = getOption("Another Customer");

                                    }

                                    System.exit(0);
                               }

                               public static int readInt(String prompt) {
                                    return Integer.parseInt(JOptionPane.showInputDialog(prompt));
                               }

                               public static boolean getOption(String prompt) {
                                    int theNum;
                                    theNum = JOptionPane.showConfirmDialog(null, prompt);
                                    return (theNum == 0);

                               }
                          }


                          Can you tell me what did I do wrong?? Please help
                          • 10. Re: dateFormat
                            807541
                            Ok...there are two main methods, one in Rental and one in RentalApplication. I'm going to assume you are running the RentalApplcation version.

                            Your Rental class has two constructors - The default constructor, Rental(), and a version that takes 4 ints, Rental(int, int, int, int).

                            The Rental() constructor does not perform any sort of initialization of the Rental class's instance variables. The Rental(int, int, int, int) version does initialize the class's instance variables.

                            The main method in RentalApplication creates a Rental object by calling the Rental() version of the constructor. This means that the timeOfSale variable will never be initialized.

                            You either need to initialize timeOfSale from the Rental() version of the constructor, or you need to use the Rental(int, int, int, int) version of the constructor.