Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: BlueJ Auction Project calling external methods, using ArrayLists, and for-each loops

  1. #1
    Member
    Join Date
    Jul 2021
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default BlueJ Auction Project calling external methods, using ArrayLists, and for-each loops

    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;
        }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: BlueJ Auction Project calling external methods, using ArrayLists, and for-each loops

    What variable has the null value when line 134 is executed? Why doesn't that variable have a valid value?

    Note: The posted code does not compile for testing because of missing statements.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2021
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: BlueJ Auction Project calling external methods, using ArrayLists, and for-each loops

    The only method I couldn't get to compile was the saleClose() method. That's where I was having trouble getting calls to other methods in order to retrieve values from them. Try, try, and try more and sometimes you succeed, that's what happened to me today. Here's the new code for that method only, which should make the rest work.
        public void saleClose()
        {
            for(Lot lot : lots) {
               Bid bid = lot.getHighestBid();
               if(lot.getHighestBid() == null){
                   System.out.println("Sorry, lot number " + lot.getNumber() + " had no bids. ");
               }
              else {
                   System.out.println("Congratulations, " + bid.getBidder().getName() + " your bid of " + bid.getValue() + " won the item.");
               }
            }
        }
    I saw that my problem was in naming the method calls, I was doing it wrong. Of course there's still more work to do on this auction, so I'm sure I'll be back.

Similar Threads

  1. Bluej fish project
    By charliengatai4381 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 28th, 2014, 07:29 PM
  2. Need help calling methods in a GUI
    By phunnydoode in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 11th, 2014, 07:28 PM
  3. Calling upon methods
    By jonathanfox in forum Java Theory & Questions
    Replies: 4
    Last Post: August 3rd, 2012, 11:58 AM
  4. BlueJ trouble or program trouble (Combining Arraylists)
    By star12345645 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 12:15 PM
  5. Calling for methods
    By soccer_kid_6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2010, 12:13 AM