There are four classes in this project Auction, Lot, Bid, Person. I will include the code to the methods in the other classes, but overall we're only working within the Auction Class. I'm having problems calling the methods in the last method in this class. I get my code to compile and then when I run it I get these error messages. java.lang.NullPointerException java.lang.NullPointerException
at Auction.saleClose(Auction.java:134) at Auction.saleClose(Auction.java:135) WHY?! I really thought my code was good in this assignment, obviously not.
import java.util.ArrayList; public class Auction { // The list of Lots in this auction. private ArrayList<Lot> lots; // The number that will be given to the next lot entered // into this auction. private int nextLotNumber; /** * Create a new auction. */ public Auction() { lots = new ArrayList<>(); nextLotNumber = 1; } /** * Enter a new lot into the auction. * @param description A description of the lot. */ public void enterLot(String description) //anonymous object { lots.add(new Lot(nextLotNumber, description)); nextLotNumber++; } /** * Show the full list of lots in this auction. */ public void showLots() { for(Lot lot : lots) { System.out.println(lot.toString()); } } /** * Return a list of the unsold lots */ public ArrayList<Lot> getUnsold() { ArrayList<Lot> unsold = new ArrayList<Lot>(); for(Lot lot : lots) { Bid bid = lot.getHighestBid(); if(bid == null){ unsold.add(lot); } } return unsold; } /** * Make a bid for a lot. * A message is printed indicating whether the bid is * successful or not. * * @param lotNumber The lot being bid for. * @param bidder The person bidding for the lot. * @param value The value of the bid. */ public void makeABid(int lotNumber, Person bidder, long value) { Lot selectedLot = getLot(lotNumber); if(selectedLot != null) { Bid bid = new Bid(bidder, value); boolean successful = selectedLot.bidFor(bid); if(successful) { System.out.println("The bid for lot number " + lotNumber + " was successful."); } else { // Report which bid is higher. Bid highestBid = selectedLot.getHighestBid(); System.out.println("Lot number: " + lotNumber + " already has a bid of: " + highestBid.getValue()); } } } /** * Return the lot with the given number. Return null * if a lot with this number does not exist. * @param lotNumber The number of the lot to return. */ public Lot getLot(int lotNumber) { if((lotNumber >= 1) && (lotNumber < nextLotNumber)) { // The number seems to be reasonable. Lot selectedLot = lots.get(lotNumber - 1); // Include a confidence check to be sure we have the // right lot. if(selectedLot.getNumber() != lotNumber) { System.out.println("Internal error: Lot number " + selectedLot.getNumber() + " was returned instead of " + lotNumber); // Don't return an invalid lot. selectedLot = null; } return selectedLot; } else { System.out.println("Lot number: " + lotNumber + " does not exist."); return null; } } /** * Return details of all the lots ***** METHOD IN QUESTION ***** * lots with bids are considered sold * Lot objects whose highest bid is not null */ public void saleClose() { for(Lot lot : lots) { String details = lot.getNumber() + ": " + lot.getDescription(); Bid bid = lot.getHighestBid(); if(lot.getHighestBid() == null){ System.out.println("Unfortunately, " + bid.getBidder() + " lot " + details + " did not sell. "); } else { System.out.println("Congratulations, " + bid.getBidder() + " your bid of " + lot.getHighestBid().getValue() + " was enough for " + lot.getDescription(). ""); } } } } //Here's the Bid Class code public Bid(Person bidder, long value) { this.bidder = bidder; this.value = value; } /** * @return The bidder. */ public Person getBidder() { return bidder; } Lot Class code public String toString() { String details = number + ": " + description; if(highestBid != null) { details += " Bid: " + highestBid.getValue(); } else { details += " (No bid)"; } return details; } /** * @return The lot's number. */ public int getNumber() { return number; } /** * @return The lot's description. */ public String getDescription() { return description; } /** * @return The highest bid for this lot. * This could be null if there is * no current bid. */ public Bid getHighestBid() { return highestBid; } //Person Class code public class Person { // The name of this person. private final String name; /** * Create a new person with the given name. * @param name The person's name. */ public Person(String name) { this.name = name; } /** * @return The person's name. */ public String getName() { return name; }