Hi I have a small problem with with my bubble sort. Everything else in my program works fine if I remove my bubble sort.

This is the part of code for the bubble sort.

//bubble sort based on animal breed
            if (numberofanimals > 1)//making sure there is more than 1 animal to sort
            {
                for (int x = 0 ; x < numberofanimals ; x++)
                {

                    for (y = 0 ; y < numberofanimals - x ; y++)
                    {
                        if (statistics [y] [1].compareToIgnoreCase (statistics [y + 1] [1]) > 0)
                        {
                            temp = statistics [y] [0];
                            statistics [y] [0] = statistics [y + 1] [0];
                            statistics [y + 1] [0] = temp;

                            temp = statistics [y] [1];
                            statistics [y] [1] = statistics [y + 1] [1];
                            statistics [y + 1] [1] = temp;

                            temp = statistics [y] [2];
                            statistics [y] [2] = statistics [y + 1] [2];
                            statistics [y + 1] [2] = temp;

                            temp = statistics [y] [3];
                            statistics [y] [3] = statistics [y + 1] [3];
                            statistics [y + 1] [3] = temp;

                            temp = statistics [y] [4];
                            statistics [y] [4] = statistics [y + 1] [4];
                            statistics [y + 1] [4] = temp;

                        }

                    }

                }
            }

And these are all the errors that I get

java.lang.NullPointerException
at java.lang.String$CaseInsensitiveComparator.compare(Unknown Source)
at java.lang.String.compareToIgnoreCase(Unknown Source)
at AnimalProfitRatioCalc.sort(AnimalProfitRatioCalc.java:314)
at AnimalProfitRatioCalc.DisplayStatistics(AnimalProfitRatioCalc.java:223)
at AnimalProfitRatioCalc.actionPerformed(AnimalProfitRatioCalc.java:120)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Here is my full program code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;



/*
*Created by Jonathan Schep
*on Jan 4
*to Calculate the profit ratio of animals and display them in sorted order
*/

public class AnimalProfitRatioCalc extends JFrame implements ActionListener
{

    static Container contentPane;
    static JPanel infoPane;
    static JTextField J1, J2, J3, J4;
    static int numberofanimals = 0;

    static String statistics[] [] = new String [100] [5];




    public AnimalProfitRatioCalc ()
    {

        JMenuBar MenuBar;
        JMenu menu;
        JMenuItem MenuItem;
        MenuBar = new JMenuBar ();
        setJMenuBar (MenuBar);

        //adding menu
        menu = new JMenu ("File");
        MenuBar.add (menu);
        MenuItem = new JMenuItem ("Quit");
        MenuItem.addActionListener (this);
        menu.add (MenuItem);
        menu = new JMenu ("Options");
        MenuItem.addActionListener (this);
        MenuBar.add (menu);
        MenuItem = new JMenuItem ("Main Menu");
        MenuItem.setToolTipText ("Go to main menu"); //tip
        MenuItem.addActionListener (this);
        menu.add (MenuItem);
        MenuItem = new JMenuItem ("Enter Animal Statistics");
        MenuItem.setToolTipText ("Animal category, Breed, Acquisition price, selling price "); //tip
        MenuItem.addActionListener (this);
        menu.add (MenuItem);
        MenuItem = new JMenuItem ("Display Animals with profit ratio");
        MenuItem.setToolTipText ("Display Animals with preferred profit ratio in sorted order"); //tip
        MenuItem.addActionListener (this);
        menu.add (MenuItem);
        MainMenu ();
    }


    public void MainMenu ()
    {

        contentPane = getContentPane ();
        if (infoPane != null)
        {
            contentPane.remove (infoPane);
        }
        infoPane = new JPanel ();
        infoPane.setLayout (null); //setting layout to null so i can use absolute positioning with coordinates


        //all the JLabels
        JLabel L1;

        L1 = new JLabel ("Animal Profit Ratio Calculator");
        L1.setFont (new Font ("Arial", Font.BOLD, 24));
        L1.setBounds (232, 10, 342, 20);
        infoPane.add (L1);


        //all JButtons
        JButton eas, da;

        eas = new JButton ("Enter Animal Statistics");
        eas.setToolTipText ("Animal category, Breed, Acquisition price, selling price "); //tip
        eas.setFont (new Font ("Arial", Font.BOLD, 18));
        eas.addActionListener (this);
        eas.setBounds (275, 75, 256, 50);
        infoPane.add (eas);

        da = new JButton ("Display Animals with profit ratio");
        da.setToolTipText ("Display Animals with preferred profit ratio in sorted order"); //tip
        da.setFont (new Font ("Arial", Font.BOLD, 18));
        da.addActionListener (this);
        da.setBounds (246, 175, 312, 50);
        infoPane.add (da);

        contentPane.add (infoPane);
        validate ();
    }


    public void actionPerformed (ActionEvent e)
    {
        String event = e.getActionCommand ();
        if (event.equals ("Quit"))
        {
            hide ();
            System.exit (0);
        }

        if (event.equals ("Enter Animal Statistics"))
        {
            AddStatistics ();
        }

        if (event.equals ("Display Animals with profit ratio"))
        {
            DisplayStatistics ();
        }

        if (event.equals ("Add Animal"))
        {
            AddAnimal ();
        }

        if (event.equals ("Main Menu"))
        {
            MainMenu ();
        }
    }


    public void AddStatistics ()
    {
        contentPane = getContentPane ();
        if (infoPane != null)
        {
            contentPane.remove (infoPane);
        }
        infoPane = new JPanel ();
        infoPane.setLayout (null); //setting layout to null so i can use absolute positioning with


        //all the  JLabels
        JLabel L1, L2, L3, L4, L5;

        L1 = new JLabel ("Enter Animal Statistics");
        L1.setFont (new Font ("Arial", Font.BOLD, 24));
        L1.setBounds (272, 10, 261, 20);
        infoPane.add (L1);

        L2 = new JLabel ("Animal Catagory");
        L2.setFont (new Font ("Arial", Font.PLAIN, 18));
        L2.setBounds (125, 100, 261, 20);
        infoPane.add (L2);

        L3 = new JLabel ("Animal Breed");
        L3.setFont (new Font ("Arial", Font.PLAIN, 18));
        L3.setBounds (125, 150, 261, 20);
        infoPane.add (L3);

        L4 = new JLabel ("Acquisition Price");
        L4.setFont (new Font ("Arial", Font.PLAIN, 18));
        L4.setBounds (125, 200, 261, 20);
        infoPane.add (L4);

        L5 = new JLabel ("Selling Price");
        L5.setFont (new Font ("Arial", Font.PLAIN, 18));
        L5.setBounds (125, 250, 261, 20);
        infoPane.add (L5);


        //all JTextFields

        J1 = new JTextField (20);
        J1.setFont (new Font ("Arial", Font.PLAIN, 14));
        J1.setToolTipText ("Catagory"); //tip
        J1.setBounds (350, 100, 120, 20);
        infoPane.add (J1);

        J2 = new JTextField (20);
        J2.setFont (new Font ("Arial", Font.PLAIN, 14));
        J2.setToolTipText ("Breed"); //tip
        J2.setBounds (350, 150, 120, 20);
        infoPane.add (J2);

        J3 = new JTextField (20);
        J3.setFont (new Font ("Arial", Font.PLAIN, 14));
        J3.setToolTipText ("Acquisition Price"); //tip
        J3.setBounds (350, 200, 120, 20);
        infoPane.add (J3);

        J4 = new JTextField (20);
        J4.setFont (new Font ("Arial", Font.PLAIN, 14));
        J4.setToolTipText ("Selling Price"); //tip
        J4.setBounds (350, 250, 120, 20);
        infoPane.add (J4);


        //all JButtons
        JButton B1, B2;

        B1 = new JButton ("Add Animal");
        B1.setToolTipText ("Add animal to data base"); //tip
        B1.setFont (new Font ("Arial", Font.PLAIN, 18));
        B1.addActionListener (this);
        B1.setBounds (550, 100, 130, 170);
        infoPane.add (B1);

        B2 = new JButton ("Main Menu");
        B2.addActionListener (this);
        B2.setBounds (10, 515, 100, 20);
        infoPane.add (B2);
        contentPane.add (infoPane);
        validate ();
    }


    public void DisplayStatistics ()
    {
        sort (); //sorting the animals ect
        contentPane = getContentPane ();
        if (infoPane != null)
        {
            contentPane.remove (infoPane);
        }
        infoPane = new JPanel ();
        infoPane.setLayout (null); //setting layout to null so i can use absolute positioning with



        JLabel L1;
        L1 = new JLabel ("Animals profit ratio table");
        L1.setFont (new Font ("Arial", Font.BOLD, 24));
        L1.setBounds (260, 10, 284, 30); //was 261
        infoPane.add (L1);

        JButton B1;
        B1 = new JButton ("Main Menu");
        B1.addActionListener (this);
        B1.setBounds (10, 515, 100, 20);
        infoPane.add (B1);

        JTable table;
        String[] columnNames = {"Catagory", "Breed", "Acquisition Price", "Selling Price", "Profit Ratio"};
        table = new JTable (statistics, columnNames);

        JScrollPane scrollpane = new JScrollPane (table);
        scrollpane.setBounds (75, 50, 650, 400);
        infoPane.add (scrollpane);


        contentPane.add (infoPane);
        validate ();

    }


    public void AddAnimal ()  //adding an animal and info to the statisitics array
    {
        statistics [numberofanimals] [0] = J1.getText ();
        statistics [numberofanimals] [1] = J2.getText ();
        statistics [numberofanimals] [2] = J3.getText ();
        statistics [numberofanimals] [3] = J4.getText ();
        numberofanimals = numberofanimals + 1; //adding 1 to number of animals so program can keep track of how many animals there are
        //removing user inputer text from JTextfields so user can input another animal easily if the user wants
        J1.setText ("");
        J2.setText ("");
        J3.setText ("");
        J4.setText ("");
    }


    public void sort ()  //deleting array indexes with ratio less than 1.2, and sorting by breed.
    {
        double buy, sell, percent, ratio1, ratio;
        String ratiostring, temp;

        for (int x = 0 ; x < numberofanimals ; x++) //calcculating the ratio
        {
            buy = Double.parseDouble (statistics [x] [2]);
            sell = Double.parseDouble (statistics [x] [3]);
            ratio = sell / buy;
            ratiostring = Double.toString (ratio);
            statistics [x] [4] = ratiostring;
        }

        for (int y = 0 ; y < numberofanimals ; y++)
        {
            ratio1 = Double.parseDouble (statistics [y] [4]);
            if (ratio1 < 1.2) //seeing if ratio if less than the required 1.2
            {
                for (int z = y ; z < numberofanimals + 1 ; z++) //moving array indexes 1 down
                {
                    statistics [z] [0] = statistics [z + 1] [0];
                    statistics [z] [1] = statistics [z + 1] [1];
                    statistics [z] [2] = statistics [z + 1] [2];
                    statistics [z] [3] = statistics [z + 1] [3];
                    statistics [z] [4] = statistics [z + 1] [4];
                }
                numberofanimals = numberofanimals - 1; //removing 1 from number of animals so program knows how many animals are in the array
            }

            //bubble sort based on animal breed
            if (numberofanimals > 1)//making sure there is more than 1 animal to sort
            {
                for (int x = 0 ; x < numberofanimals ; x++)
                {

                    for (y = 0 ; y < numberofanimals - x ; y++)
                    {
                        if (statistics [y] [1].compareToIgnoreCase (statistics [y + 1] [1]) > 0)
                        {
                            temp = statistics [y] [0];
                            statistics [y] [0] = statistics [y + 1] [0];
                            statistics [y + 1] [0] = temp;

                            temp = statistics [y] [1];
                            statistics [y] [1] = statistics [y + 1] [1];
                            statistics [y + 1] [1] = temp;

                            temp = statistics [y] [2];
                            statistics [y] [2] = statistics [y + 1] [2];
                            statistics [y + 1] [2] = temp;

                            temp = statistics [y] [3];
                            statistics [y] [3] = statistics [y + 1] [3];
                            statistics [y + 1] [3] = temp;

                            temp = statistics [y] [4];
                            statistics [y] [4] = statistics [y + 1] [4];
                            statistics [y + 1] [4] = temp;
                        }

                    }

                }
            }
        }
    }

    public static void main (String[] args)  //main
    {
        AnimalProfitRatioCalc window = new AnimalProfitRatioCalc ();
        window.setTitle ("Animal Profit Ratio Calculator");
        window.setSize (800, 600);
        window.setVisible (true);
    }
}

Help would be immensely appreciated

Have you found the null variable and determined why it is null?
Add some printlns to print out the values of the variables on the line where the exception occurred.

Well I know that it always happens on the last pass going through the entire bubble sort. but I also know that both the statistics [y] [1] and statistics [y+1] [1] are not null, so dont know what the heck is going on.

by last pass I mean the last pass of
for (int x = 0 ; x < numberofanimals ; x++)
so the final pass of the entire thing

Are there any null elements in the statistics array?

Print out the last couple of rows of the array to see.

the statistics array is a 2d array, and here is this makes it easier


static String statistics[] [] = new String [100] [5];

whenever the user adds an animal which includes Catagory, breed, price, buying price and selling price, this program will calculate the profit ratio which will be assigned statistics[x] [5], then the program deletes all the animals which have less than a 20% profit ration, and moves all the animals 1 up in the array.

everytime an animal is added, a variable numberofanimals gets a value higher of one,
and same when they program takes an animal off becouse it doesant have atleast 20% profit ratio, but then a value of 1 gets subtracted from number of animals.

so the bubble sort only sorts the array for numberofanimals see below

//bubble sort based on animal breed
            if (numberofanimals > 1)//making sure there is more than 1 animal to sort
            {
                for (int x = 0 ; x < numberofanimals ; x++)
                {
 
                    for (y = 0 ; y < numberofanimals - x ; y++)
                    {
                        if (statistics [y] [1].compareToIgnoreCase (statistics [y + 1] [1]) > 0)
                        {
                            temp = statistics [y] [0];
                            statistics [y] [0] = statistics [y + 1] [0];
                            statistics [y + 1] [0] = temp;
 
                            temp = statistics [y] [1];
                            statistics [y] [1] = statistics [y + 1] [1];
                            statistics [y + 1] [1] = temp;
 
                            temp = statistics [y] [2];
                            statistics [y] [2] = statistics [y + 1] [2];
                            statistics [y + 1] [2] = temp;
 
                            temp = statistics [y] [3];
                            statistics [y] [3] = statistics [y + 1] [3];
                            statistics [y + 1] [3] = temp;
 
                            temp = statistics [y] [4];
                            statistics [y] [4] = statistics [y + 1] [4];
                            statistics [y + 1] [4] = temp;
                        }
 
                    }
 
                }
            }

Are there any null elements in the array?

What variable is null and causing the exception?

yes there are null elements in the array, but they arent touched there, so they cant be the problem. I think your trying to get at something, but im just not catching it, could you clarify?

I know for 100% sure that it happens on the final go through the sort

You get a NullPointerException from null variables. Your code must be using a variable with a null value.
A way to find that variable is to print out the variables that are being used.
This is the third or fourth time I've suggested this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.