Results 1 to 7 of 7
  1. #1
    Decarle is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Unhappy java.lang.NullPointerException !! Pls help me with the calling other class.

    Hi, I am new to Java, and I got an Assignment of store date entry. The main class is "RentalAgreement", the others are "RentalCustomer","RentalDate" and "MusicalInstrument". I am confusing about the calling other class. Pls help.

    Here is the main class.

    /**
    * Write a description of class RentalAgreement here.
    * @version (2012.11.3)
    */
    public class RentalAgreement
    {
    private RentalCustomer customer;
    private RentalDate rentalDate;
    private MusicalInstrument instrumentRented;
    private double rentalFeePerDay;
    private int numberOfDaysRented;
    private double totalRentalFee;

    /**
    * Constructor for objects of class RentalAgreement
    */

    public RentalAgreement()
    {}

    public RentalAgreement(RentalCustomer renter,
    RentalDate dateRented,
    MusicalInstrument instrumentRented)
    {
    customer = new RentalCustomer(); //I am confused here
    customer = renter;
    rentalDate = new RentalDate();
    rentalDate = dateRented;
    instrumentRented = new MusicalInstrument();
    instrumentRented = instrumentRented;
    }


    /**
    * get customer
    * return customer
    */
    public RentalCustomer getCustomer()
    {
    return customer;
    }

    /**
    * get rentalDate
    * return rentalDate
    */
    public RentalDate getRentalDate()
    {
    return rentalDate;
    }

    /**
    * get instrumentRented
    * return instrumentRented
    */
    public MusicalInstrument getInstrumentRented()
    {
    return instrumentRented;
    }

    /**
    * get rentalFeePerDay
    * return rentalFeePerDay
    */
    public double getRentalFeePerDay()
    {
    return rentalFeePerDay;
    }

    /**
    * get numberOfDaysRented
    * return numberOfDaysRented
    */
    public int getNumberOfDaysRented()
    {
    return numberOfDaysRented;
    }

    /**
    * get totalRentalFee
    * return totalRentalFee
    */
    public double getTotalRentalFeed()
    {
    return totalRentalFee;
    }

    /**
    * set rentalFeePerDay
    * @param newRentalFeePerDay
    */
    public void setRentalFeePerDay(double newRentalFeePerDay)
    {

    if ( newRentalFeePerDay > 0)
    { rentalFeePerDay = newRentalFeePerDay; }
    else { System.out.println("Invalue Number Input!");}
    }

    /**
    * set numberOfDaysRented
    * @param newNumberOfDaysRented
    */
    public void setNumberOfDaysRented(int newNumberOfDaysRented)
    {
    if ( newNumberOfDaysRented >= 1 && newNumberOfDaysRented <= 31)
    { numberOfDaysRented = newNumberOfDaysRented; }
    else { System.out.println("Invalue Number Input!");}
    }

    /**
    * set totalRentalFee
    * @param newTotalRentalFee
    */
    public void setTotalRentalFee()
    {
    totalRentalFee = rentalFeePerDay * numberOfDaysRented;
    }

    /**
    * display rental agreement
    */
    public void displayRentalAgreement()
    {
    System.out.println("Customer: " + customer.getFullName());
    System.out.println("Rental date: " + rentalDate.displayFullDate());
    System.out.println("Instrument rented: " + instrumentRented.getDescription());
    System.out.println("Fee per day: $" + rentalFeePerDay);
    System.out.println("Number of days rented: " + numberOfDaysRented);
    System.out.println("Total rental fee: $" + totalRentalFee);
    }
    }

    Second class

    /**
    * Write a description of class RentalCustomer here.
    *
    * @author (decarle Yang)
    * @version (2012-10-28)
    */
    public class RentalCustomer
    {
    private String first;
    private String last;
    private String creditCardNumber;
    private String address;
    private String phoneNumber;

    public RentalCustomer()
    {}

    /**
    * Constructor for objects of class RentalCustomer
    */
    public RentalCustomer(
    String newFirst,
    String newlast,
    String newCreditCardNumber,
    String newAddress,
    String newPhoneNumber)
    {
    setFirstName(newFirst);
    setLastName(newlast);
    setCreditCardNumber(newCreditCardNumber);
    setAddress(newAddress);
    setPhoneNumber(newPhoneNumber);
    }

    /**
    * get firstName
    * @return first
    */
    public String getFirstName()
    {
    return first;
    }

    /**
    * get lastName
    * @return last
    */
    public String getLastName()
    {
    return last;
    }

    /**
    * get creditCardNumber
    * @return creditCardNumber
    */
    public String getCreditCardNumber()
    {
    return creditCardNumber;
    }

    /**
    * get address
    * @return address
    */
    public String getAddress()
    {
    return address;
    }

    /**
    * get fullName
    * @return fullName
    */
    public String getFullName()
    {
    String fullName = first + " " + last;
    return fullName;
    }

    /**
    * get phoneNumber
    * @return phoneNumber
    */
    public String getPhoneNumber()
    {
    return phoneNumber;
    }

    /**
    * set firstName
    * The first letter of each name component is uppercase and the rest of the letters is lowercase.
    * @param newFirst
    */
    public void setFirstName(String newFirst)
    {
    if ( newFirst.length() > 0)
    {
    first = newFirst.substring(0, 1).toUpperCase() + newFirst.substring(1).toLowerCase();
    }
    else
    {
    System.out.println("It's not a correct input!");
    }
    }

    /**
    * set lastName
    * The first letter of each name component is uppercase and the rest of the letters is lowercase.
    * @param newlast
    */
    public void setLastName(String newlast)
    {
    if ( newlast.length() > 0)
    {
    last = newlast.substring(0, 1).toUpperCase() + newlast.substring(1).toLowerCase();
    }
    else
    {
    System.out.println("It's not a correct input!");
    }
    }

    /**
    * set creditCardNumber
    * @param newCreditCardNumber
    */
    public void setCreditCardNumber(String newCreditCardNumber)
    {
    creditCardNumber = newCreditCardNumber;
    }

    /**
    * set address
    * @param newAddress
    */
    public void setAddress(String newAddress)
    {
    if ( newAddress.length() > 0)
    {
    address = newAddress;
    }
    else
    {
    System.out.println("It's not a correct input!");
    }
    }

    /**
    * set phoneNumber
    * @param newPhoneNumber
    */
    public void setPhoneNumber(String newPhoneNumber)
    {
    phoneNumber = newPhoneNumber;
    }
    }


    Third class

    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    /**
    * Write a description of class RentalDate here.
    *
    * @author (Decarle Yang)
    * @version (2012-10-28)
    */
    public class RentalDate
    {
    private static final int CURRENT_YEAR = 2012;
    private int year;
    private int month;
    private int day;

    public RentalDate()
    {}

    /**
    * Constructor for objects of class RentalDate
    */
    public RentalDate(int theYear, int theMonth, int theDay)
    {
    setYear(theYear);
    setMonth(theMonth);
    setDay(theDay);
    }

    /**
    * get year
    * return year
    */
    public int getYear()
    {
    return year;
    }

    /**
    * get month
    * return month
    */
    public int getMonth()
    {
    return month;
    }

    /**
    * get day
    * return day
    */
    public int getDay()
    {
    return day;
    }

    /**
    * set year
    * @param theYear
    */
    public void setYear(int theYear)
    {
    if ( theYear <= CURRENT_YEAR && theYear > 0)
    { year = theYear; }
    else { year = CURRENT_YEAR;
    System.out.println("Invalue Number Input!");}
    }

    /**
    * set month
    * @param theMonth
    */
    public void setMonth(int theMonth)
    {
    if ( theMonth >= 1 && theMonth <= 12)
    {month = theMonth;}
    else { month = 1;
    System.out.println("Invalue Number Input!");}
    }

    /**
    * set day
    * @param theDay
    */
    public void setDay(int theDay)
    {
    if ( theDay >=1 && theDay <= 31)
    {day = theDay;}
    else { day = 1;
    System.out.println("Invalue Number Input!");}
    }

    /**
    * return the full date
    */
    public String displayFullDate()
    {
    String fullDate;
    String strMonth;
    String strDay;
    if ( month < 10)
    { strMonth = "0" + month;}
    else { strMonth = "" + month;}
    if ( day < 10)
    { strDay = "0" + day;}
    else { strDay = "" + day;}
    fullDate = year + "-" + strMonth + "-" + strDay;
    return fullDate;
    }
    }

    Last class


    /**
    * Class type which holds information about a type of musical instrument.
    */
    public class MusicalInstrument {

    private String description;
    private String stockCode;
    private int quantityInStock;
    private int quantitySold;
    private double purchasePrice;
    private double sellingPrice;
    private int numberRented;

    public MusicalInstrument()
    {}

    /**
    * @param description the instrument
    * @param stock code the instrument
    * @param purchase cost of the instrument
    * @param selling price of the instrument
    */
    public MusicalInstrument(String instrumentName, String instrumentCode,
    double unitCost, double unitPrice) {

    description = instrumentName;
    stockCode = instrumentCode;

    setPurchasePrice(unitCost);
    setSellingPrice(unitPrice);

    quantityInStock = 0;
    quantitySold = 0;
    numberRented = 0;
    }

    /**
    * @return the description
    */
    public String getDescription() {
    return description;
    }

    /**
    * @param description the description to set
    */
    public void setDescription(String instrumentDescription) {
    description = instrumentDescription;
    }

    /**
    * @return the stockCode
    */
    public String getStockCode() {
    return stockCode;
    }

    /**
    * @param stockCode the stockCode to set
    */
    public void setStockCode(String instrumentStockCode) {
    stockCode = instrumentStockCode;
    }

    /**
    * @return the quantityInStock
    */
    public int getQuantityInStock() {
    return quantityInStock;
    }

    /**
    * @param quantityInStock the quantityInStock to set
    */
    public void setQuantityInStock(int numberInstrumentsInStock) {
    quantityInStock = numberInstrumentsInStock;
    }

    /**
    * @return the quantitySold
    */
    public int getQuantitySold() {
    return quantitySold;
    }

    /**
    * @param quantitySold the quantitySold to set
    */
    public void setQuantitySold(int numberInstrumentsSold) {
    quantitySold = numberInstrumentsSold;
    }

    /**
    * @return the purchasePrice
    */
    public double getPurchasePrice() {
    return purchasePrice;
    }

    /**
    * @param purchasePrice the purchasePrice to set
    */
    public void setPurchasePrice(double unitCost) {

    if (unitCost < 1.0) {
    purchasePrice = 1.0;
    } else {
    purchasePrice = unitCost;
    }
    }

    /**
    * @return the sellingPrice
    */
    public double getSellingPrice() {
    return sellingPrice;
    }

    /**
    * @param sellingPrice the sellingPrice to set
    */
    public void setSellingPrice(double unitPrice) {

    if (unitPrice < 0.0 || unitPrice < (purchasePrice * 1.1)) {
    sellingPrice = purchasePrice * 1.1;
    } else {
    sellingPrice = unitPrice;
    }

    }

    /**
    * @return the numberRented
    */
    public int getNumberRented() {
    return numberRented;
    }

    /**
    * @param numberRented the numberRented to set
    */
    public void setNumberRented(int quantityRented) {

    if(quantityRented > quantityInStock / 2){
    System.out.println("You cannot rent " + quantityRented + " instruments.");
    }
    else{
    numberRented = quantityRented;
    }
    }

    /**
    * @param quantity the quantity added to existing stock
    */
    public void restock(int quantity){

    quantityInStock += quantity;
    }

    /**
    * @param quantity the quantity sold
    */
    public void sell(int quantity){

    if(quantity > 0 && quantity <= quantityInStock){

    quantityInStock -= quantity;
    quantitySold += quantity;
    }
    else{
    System.out.println("Input quantity is either negative or two high a value.");
    }
    }

    /**
    * @return the profit expected on one instrument at current selling price
    */
    public double calculateProfit(){

    return sellingPrice - purchasePrice;
    }

    /**
    * @return the profit expected on all instruments at current selling price
    */
    public double calculateTotalProfit(){

    return calculateProfit() * quantitySold;
    }

    /**
    * @return the value of all the instruments in inventory based on purchase price
    */
    public double calculateInventoryValue(){

    return purchasePrice * quantityInStock;
    }

    /**
    * display the information for this instrument object
    */
    public void printDetails(){

    System.out.println("Descritpion: " + description);
    System.out.println("Stock code: " + stockCode);
    System.out.println("Puchase price: $" + purchasePrice);
    System.out.println("Selling price: $" + sellingPrice);
    System.out.println("Quantity in stock: " + quantityInStock);
    System.out.println("Quantity sold: " + quantitySold);
    System.out.println("Inventory value: $" + calculateInventoryValue());
    System.out.println("Profit from sales: $" + calculateTotalProfit());
    System.out.println("Number of instruments rented: " + numberRented);
    }

    }

  2. #2
    sibernewf is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default Re: java.lang.NullPointerException !! Pls help me with the calling other class.

    Wow that is a lot of code to go through.
    You said you had a NullPointerException, but you didnt specify where.
    So somewhere you are try ing to access a variable that has not been initialised.

    Run this through a debugger and see if you can spot where the problem is occuring.
    Also if you want us to look into this a bit, please post the full error message you are getting.

  3. #3
    Decarle is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: java.lang.NullPointerException !! Pls help me with the calling other class.

    Hi Sibernewf,
    Thanks for your reply. I have tried to use the debugger, but nothing pop our. I have marked out the unsure code with red color. Please take a look at there because I don't know what I have written there. Thank you a lot.

    Decarle

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: java.lang.NullPointerException !! Pls help me with the calling other class.

    The NullPointerException message will tell you which line is throwing the exception. Why make us guess? Please show us the exact line. Also, please surround your posted code with code tags. Have a look at the link in my signature below to see how.

  5. #5
    Decarle is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: java.lang.NullPointerException !! Pls help me with the calling other class.

    Hi Fubarable,
    I understand that it's confuse, but I am just a beginner for Java. Therefore, I don't know well about debugger or other tools. The following is the error line. Please advise me.Thanks

    System.out.println("Customer: " + customer.getFullName());

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,254
    Rep Power
    19

    Default Re: java.lang.NullPointerException !! Pls help me with the calling other class.

    Read the whole response. Fubarable asked you to use code tags, and you're still posting unformatted code.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: java.lang.NullPointerException !! Pls help me with the calling other class.

    At the point that line is executed the customer variable is pointing to nothing (ie it is null).
    Stick some debugging into your code (ie println() statements) around the places where you use that customer object and where you set it.
    Do not come back and say "I have done that and have seen nothing". You will need to show the output from a test run (that fails), and the code with the debugging statements in it.

    I can't follow that code, so can't comment on anything that might be obvious if it were in [code] tags [/code]. So any new code you post must be in tags.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 15
    Last Post: 01-13-2011, 06:28 PM
  2. java.lang.NullPointerException
    By peterhabe in forum New To Java
    Replies: 5
    Last Post: 08-27-2010, 08:43 PM
  3. java.lang.NullPointerException
    By nitinverma in forum CLDC and MIDP
    Replies: 4
    Last Post: 06-15-2010, 03:56 PM
  4. java.lang.NullPointerException
    By Pombi in forum New To Java
    Replies: 6
    Last Post: 05-15-2010, 03:12 PM
  5. java.lang.NullPointerException
    By Travis in forum AWT / Swing
    Replies: 7
    Last Post: 09-18-2009, 05:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •