Search code examples
javabubble-sort

Why won't my bubble sort sort my array of objects?


I've created a program with an object called CarlysCatering. I'm trying to sort the CarlysCatering objects by number of guests.

I've tried using a bubble sort but I get an error message.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        CarlysCatering[] event = new CarlysCatering[100];

        event[0] = new CarlysCatering(10, "A547", "6874714145", 0);
        event[1] = new CarlysCatering(100, "B527", "6874874945", 2);
        event[2] = new CarlysCatering(50, "C546", "6874785145", 3);
        event[3] = new CarlysCatering(40, "L577", "6874321485", 1);
        event[4] = new CarlysCatering(70, "A111", "6874714145", 4);
        event[5] = new CarlysCatering(90, "K222", "6874974855", 2);
        event[6] = new CarlysCatering(11, "F798", "6875555555", 3);
        event[7] = new CarlysCatering(17, "T696", "6474763898", 0);

        //SORT
        int selection = 0;
        do {
            System.out.println("1 - sort by eventID. 2 - sort by number of guests. 3 - sort by event type. 4 - quit");
            selection = input.nextInt();
            input.nextLine();

            if(selection == 1) {

            }

            if(selection == 2) {
                int n = event.length;
                for (int i = 0; i < n - 1; i++) {
                    for (int j = 0; j < n - i - 1; j++) {
                        if (event[j].getGuests() > event[j + 1].getGuests()) {
                            // swap arr[j+1] and arr[i]
                            CarlysCatering temp = event[j];
                            event[j] = event[j + 1];
                            event[j + 1] = temp;
                        }
                    }
                }
            }
        } while (selection != 4);

        //Print totals
        event[0].getTotals(); event[1].getTotals(); event[2].getTotals(); event[3].getTotals(); event[4].getTotals(); event[5].getTotals(); event[6].getTotals(); event[7].getTotals();
    }



////////////////////////////////////////////////////// STATIC METHODS //////////////////////////////////////////////////////////////////////

}

public class CarlysCatering {
    public static final int PRICE_PER_GUEST_HIGH = 35;
    public static final int PRICE_PER_GUEST_LOW = 32;
    public static final int CUTOFF_VALUE_LARGE = 49;
    private int guests;
    private int totalPrice;
    private String eventID;
    private String phoneNumber;
    private String eventType;
    private boolean largeEvent;

/////////////////////////////////////////////////////  CONSTRUCTORS  //////////////////////////////////////////////////////////////////
    CarlysCatering() {
        this.guests = 0;
        this.eventID = "A000";
        this.phoneNumber = "0000000000";
    }

    CarlysCatering(int guests, String eventID, String phoneNumber, int eventType) {
        this.guests = guests;
        this.eventID = eventID;
        //Phone Number formatting
        String phoneNumber2 = "";
        int count = 0;
        for(int i = 0; i < phoneNumber.length(); i++) {
            if (Character.isDigit(phoneNumber.charAt(i))) {
                phoneNumber2 += phoneNumber.charAt(i);
                count += 1;
            }
        }
        if (count != 10) {
            this.phoneNumber = "0000000000";
        } else {
            String phoneNumber3 = "(" + phoneNumber2.substring(0,3) + ") " + phoneNumber2.substring(3,6) + "-" + phoneNumber2.substring(6,10);
            this.phoneNumber = phoneNumber3;
        }
        //Event type formatting
        final String[] eventString = new String[5];
        eventString[0] = "wedding"; eventString[1] = "baptism"; eventString[2] = "birthday"; eventString[3] = "corporate"; eventString[4] = "other";

        if(eventType > -1 && eventType < 5) {
            this.eventType = eventString[eventType];
        } else {
            this.eventType = eventString[4];
        }
    }

/////////////////////////////////////////////////////////   SETTERS AND GETTERS   /////////////////////////////////////////////////

    //Setters
    public void setEventID(String eventID) {
        this.eventID = eventID;
    }
    public void setGuests(int guests) {
        this.guests = guests;
    }
    public void setPhoneNumber(String phoneNumber) {
        String phoneNumber2 = "";
        int count = 0;
        for (int i = 0; i < phoneNumber.length(); i++) {
            if (Character.isDigit(phoneNumber.charAt(i))) {
                phoneNumber2 += phoneNumber.charAt(i);
                count += 1;
            }
        }
        if (count != 10) {
            this.phoneNumber = "0000000000";
        } else {
            String phoneNumber3 = "(" + phoneNumber2.substring(0, 3) + ") " + phoneNumber2.substring(3, 6) + "-" + phoneNumber2.substring(6, 10);
            this.phoneNumber = phoneNumber3;
        }
    }
    public void setEventType(String eventType) {
        this.eventType = eventType;
    }
    //Getters
    public int getTotalPrice() {
        return totalPrice;
    }
    public int getGuests() {
        return guests;
    }
    public String getEventID() {
        return eventID;
    }
    public String getPhoneNumber() {
        return phoneNumber;
    }
    public String getEventType() {
        return eventType;
    }


    ///////////////////////////////////////////////////////  ADDITIONAL METHODS   ///////////////////////////////////////////////////////////////////
    public void isLargeEvent() {
        if (this.guests > CUTOFF_VALUE_LARGE) {
            largeEvent = true;
            System.out.println("Yes this is a large event.");
        } else {
            largeEvent = false;
            System.out.println("This is not a large event");
        }
    }

    public void getTotals() {
        boolean largeEvent = false;
        if(this.guests > CUTOFF_VALUE_LARGE) {
            largeEvent = true;
            this.totalPrice = this.guests * PRICE_PER_GUEST_HIGH;
        } else {
            largeEvent = false;
            this.totalPrice = this.guests * PRICE_PER_GUEST_LOW;
        }
        System.out.println("The number of guests attending event " + this.eventID + " " + this.eventType + " is: " + this.guests + ". The total price is $" + this.totalPrice);
        System.out.println("Large event: " + largeEvent);
        System.out.println("The phone number on file is " + this.phoneNumber);
    }
    // Static methods
    public static void showMotto() {
        System.out.println("*****Carly's makes the food that makes it a party.*****");
    }

}

The error message I get when I try to sort by guests is Exception in thread "main" java.lang.NullPointerException and then error code exit -1. The line that's causing the error is:

if (event[j].getGuests() > event[j + 1].getGuests()) {

Solution

  • You create an Array with the size 100.

    After that, you fill it from index 0 to 7.

    Every other place of the array remains null but the length is 100.

    Then, you try to sort the array.

    This throws a NullPointerException when you try to dereference (access) the 8. element:

    event[j+1].getGuests()
    

    I think you should use a smaller array(size 8) or a List.