Guys im stuck once again.

I have a JList (unsorted) and when you press 'SORT' the list sorts.
The thing is i have to use bubble sort.

My jlist is set to defaultListModel.

My problem is i keep getting this error:

xception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to assignment2009.studentClass

I tried sorting the data by adding it to an arraylist then sorting but i was guided by my teacher by sorting direct from JList.

Any help on this one would be great, im a newbee to this, but 6 days trying to sort a list is crazy :)

Below is my sort method

and student class:


note: private DefaultListModel tranList = new DefaultListModel();

public void bubbleSort() {

        studentClass tmp;
        if (tranList.getSize() == 1) {
            return;
        }

        for (int i = 0; i < tranList.getSize(); i++) {
            for (int j = i + 1; j < tranList.getSize(); j++) {
                if (((studentClass) tranList.get(i)).getStudentID() > ((studentClass) tranList.get(j)).getStudentID()) {
                    tmp = (studentClass) tranList.get(i);
                    tranList.set(i, tranList.get(j));
                    tranList.set(j, tmp);
                    
                }
            }
        }
    }
package assignment2009;


public class studentClass
{

    String studentName ;
    int studentID;
    String transType;


public int getStudentID()
{
return studentID;
}

public void setStudentID(int newStudentID)
{
this.studentID = newStudentID;
}
//--------------------------------------------------

public String getStudentName()
{
return studentName;
}

public void setStudentName(String newStudentName)
{
this.studentName = newStudentName;
}
//----------------------------------------------------
public String getTransType()
{
return transType;
}

public void setTransType(String newTransType)
{
this.transType = newTransType;
}
}

Thanks for trying

What line gives the error? Highlight it in red or point it out some other way. Also, please give us something we can compile and run. In other words, the class that contains the Bubble Sort function, a main function, something that creates the JList and studentClass objects, stuff like that, not just the Bubble Sort function by itself.

Hi VernonDozier.

Not sure how much you want so here is all of it.

package idcsjavaassignemnt2009;

import javax.swing.*;

public class MainApplication extends javax.swing.JFrame
{

      public MainApplication() {
        initComponents();
      }

    @SuppressWarnings("unchecked")

    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        top1Panel = new javax.swing.JPanel();
        clearTFButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tfJList = new javax.swing.JList();
        sortTranButton = new javax.swing.JButton();
        top2Panel = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        newMList = new javax.swing.JList();
        statusLbl = new javax.swing.JLabel();
        top3Panel = new javax.swing.JPanel();
        clearMFButton = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        mfJList = new javax.swing.JList();
        sortMasterButton = new javax.swing.JButton();
        top4Panel = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        errorLog = new javax.swing.JTextArea();
        elLabel = new javax.swing.JLabel();
        botton1Panel = new javax.swing.JPanel();
        etButton = new javax.swing.JLabel();
        blank = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        nameSurFeild = new javax.swing.JTextField();
        idLabel = new javax.swing.JLabel();
        iDFeild = new javax.swing.JTextField();
        tLabel = new javax.swing.JLabel();
        tranTypeFeild = new javax.swing.JTextField();
        deleteButton = new javax.swing.JButton();
        addButton = new javax.swing.JButton();
        modifyButton = new javax.swing.JButton();
        defaultRecordsButton = new javax.swing.JButton();
        bottom2Panel = new javax.swing.JPanel();
        soLabel = new javax.swing.JLabel();
        asLabel = new javax.swing.JLabel();
        demoSpeedSlider = new javax.swing.JSlider();
        sliderLabel = new javax.swing.JLabel();
        mLabel = new javax.swing.JLabel();
        singleShotCB = new javax.swing.JCheckBox();
        bottom3Panel = new javax.swing.JPanel();
        dcLabel = new javax.swing.JLabel();
        sDemoButton = new javax.swing.JButton();
        pDemoButton = new javax.swing.JButton();
        jToggleButton1 = new javax.swing.JToggleButton();
        bottom4Panel = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        helpTArea = new javax.swing.JTextArea();
        helpButton = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridLayout(2, 4));

        top1Panel.setLayout(new java.awt.BorderLayout());

        clearTFButton.setText("Clear Transaction Record");
        clearTFButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearTFButtonActionPerformed(evt);
            }
        });
        top1Panel.add(clearTFButton, java.awt.BorderLayout.PAGE_END);

        tfJList.setBackground(new java.awt.Color(0, 204, 255));
        tfJList.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2), "Transaction File", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Impact", 0, 13), new java.awt.Color(0, 0, 0))); // NOI18N
        tfJList.setModel(tranList);
        tfJList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(tfJList);

        top1Panel.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        sortTranButton.setText("Sort");
        sortTranButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sortTranButtonActionPerformed(evt);
            }
        });
        top1Panel.add(sortTranButton, java.awt.BorderLayout.PAGE_START);

        getContentPane().add(top1Panel);

        top2Panel.setLayout(new java.awt.BorderLayout());

        newMList.setBackground(new java.awt.Color(255, 51, 51));
        newMList.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2), "New Master File", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Impact", 0, 13), new java.awt.Color(0, 0, 0))); // NOI18N
        newMList.setModel(newMasterList);
        newMList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(newMList);

        top2Panel.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        statusLbl.setBackground(new java.awt.Color(102, 255, 51));
        statusLbl.setFont(new java.awt.Font("Tahoma", 2, 14)); // NOI18N
        statusLbl.setForeground(new java.awt.Color(0, 51, 255));
        statusLbl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        statusLbl.setText("Info Bar");
        statusLbl.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 3, true));
        top2Panel.add(statusLbl, java.awt.BorderLayout.PAGE_END);

        getContentPane().add(top2Panel);

        top3Panel.setLayout(new java.awt.BorderLayout());

        clearMFButton.setText("Clear Master Record");
        clearMFButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearMFButtonActionPerformed(evt);
            }
        });
        top3Panel.add(clearMFButton, java.awt.BorderLayout.PAGE_END);

        mfJList.setBackground(new java.awt.Color(102, 255, 102));
        mfJList.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2), "Master File", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Impact", 0, 13), new java.awt.Color(0, 0, 0))); // NOI18N
        mfJList.setModel(masterList);
        mfJList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane3.setViewportView(mfJList);

        top3Panel.add(jScrollPane3, java.awt.BorderLayout.CENTER);

        sortMasterButton.setText("Sort");
        sortMasterButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sortMasterButtonActionPerformed(evt);
            }
        });
        top3Panel.add(sortMasterButton, java.awt.BorderLayout.PAGE_START);

        getContentPane().add(top3Panel);

        top4Panel.setLayout(new java.awt.BorderLayout());

        errorLog.setBackground(new java.awt.Color(0, 102, 204));
        errorLog.setColumns(20);
        errorLog.setRows(5);
        jScrollPane4.setViewportView(errorLog);

        top4Panel.add(jScrollPane4, java.awt.BorderLayout.CENTER);

        elLabel.setFont(new java.awt.Font("Impact", 0, 13)); // NOI18N
        elLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        elLabel.setText("Error Log");
        top4Panel.add(elLabel, java.awt.BorderLayout.PAGE_START);

        getContentPane().add(top4Panel);

        botton1Panel.setLayout(new java.awt.GridLayout(7, 2, 10, 10));

        etButton.setFont(new java.awt.Font("Impact", 0, 13)); // NOI18N
        etButton.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        etButton.setText("Records");
        botton1Panel.add(etButton);
        botton1Panel.add(blank);

        nameLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        nameLabel.setText("Name / Surname:");
        botton1Panel.add(nameLabel);

        nameSurFeild.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        nameSurFeild.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameSurFeildActionPerformed(evt);
            }
        });
        botton1Panel.add(nameSurFeild);

        idLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        idLabel.setText("ID Number:");
        botton1Panel.add(idLabel);

        iDFeild.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                iDFeildActionPerformed(evt);
            }
        });
        botton1Panel.add(iDFeild);

        tLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        tLabel.setText("Transaction Type:");
        botton1Panel.add(tLabel);

        tranTypeFeild.setText("");
        tranTypeFeild.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tranTypeFeildActionPerformed(evt);
            }
        });
        botton1Panel.add(tranTypeFeild);

        deleteButton.setText("Delete Record");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });
        botton1Panel.add(deleteButton);

        addButton.setText("Add Record");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });
        botton1Panel.add(addButton);

        modifyButton.setText("Modify Record");
        modifyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modifyButtonActionPerformed(evt);
            }
        });
        botton1Panel.add(modifyButton);

        defaultRecordsButton.setText("Load Default Records");
        defaultRecordsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                defaultRecordsButtonActionPerformed(evt);
            }
        });
        botton1Panel.add(defaultRecordsButton);

        getContentPane().add(botton1Panel);

        bottom2Panel.setLayout(new java.awt.GridLayout(7, 1, 10, 10));

        soLabel.setFont(new java.awt.Font("Impact", 0, 13)); // NOI18N
        soLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        soLabel.setText("Simulation Options");
        bottom2Panel.add(soLabel);

        asLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        asLabel.setText("--- Automatic Simulation ---");
        bottom2Panel.add(asLabel);

        demoSpeedSlider.setMaximum(10);
        demoSpeedSlider.setMinimum(1);
        demoSpeedSlider.setMinorTickSpacing(1);
        demoSpeedSlider.setPaintLabels(true);
        demoSpeedSlider.setPaintTicks(true);
        demoSpeedSlider.setSnapToTicks(true);
        demoSpeedSlider.setValue(5);
        bottom2Panel.add(demoSpeedSlider);

        sliderLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        sliderLabel.setText("Slow                 Speed                      Fast");
        sliderLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bottom2Panel.add(sliderLabel);

        mLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        mLabel.setText("--- Manual Simulation ---");
        bottom2Panel.add(mLabel);

        singleShotCB.setText("Single Shot");
        singleShotCB.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        bottom2Panel.add(singleShotCB);

        getContentPane().add(bottom2Panel);

        bottom3Panel.setLayout(new java.awt.GridLayout(7, 1, 10, 10));

        dcLabel.setFont(new java.awt.Font("Impact", 0, 13));
        dcLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        dcLabel.setText("Demonstration Controls");
        bottom3Panel.add(dcLabel);

        sDemoButton.setText("Start Demonstration");
        sDemoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sDemoButtonActionPerformed(evt);
            }
        });
        bottom3Panel.add(sDemoButton);

        pDemoButton.setText("Pause");
        pDemoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pDemoButtonActionPerformed(evt);
            }
        });
        bottom3Panel.add(pDemoButton);

        jToggleButton1.setText("Next Step >");
        bottom3Panel.add(jToggleButton1);

        getContentPane().add(bottom3Panel);

        bottom4Panel.setLayout(new java.awt.BorderLayout());

        helpTArea.setBackground(new java.awt.Color(255, 255, 51));
        helpTArea.setColumns(20);
        helpTArea.setEditable(false);
        helpTArea.setFont(new java.awt.Font("Monospaced", 0, 12));
        helpTArea.setLineWrap(true);
        helpTArea.setRows(5);
        helpTArea.setText("Step 1\nAdd records to transaction file.\n\nStep 2\nSelect speed of demonstration or select single shot to process data in an instant.\n\nStep 3\nSelect Start Demonstation!");
        helpTArea.setWrapStyleWord(true);
        jScrollPane5.setViewportView(helpTArea);

        bottom4Panel.add(jScrollPane5, java.awt.BorderLayout.CENTER);

        helpButton.setFont(new java.awt.Font("Impact", 0, 13));
        helpButton.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        helpButton.setText("Help ");
        bottom4Panel.add(helpButton, java.awt.BorderLayout.PAGE_START);

        getContentPane().add(bottom4Panel);

        pack();
    }// </editor-fold>
    private void clearTFButtonActionPerformed(java.awt.event.ActionEvent evt) {
        tranList.clear();
        statusLbl.setText("Transaction File Cleared");
}

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
    addRecord();
}

    private void modifyButtonActionPerformed(java.awt.event.ActionEvent evt) {
    modify();
}

    private void defaultRecordsButtonActionPerformed(java.awt.event.ActionEvent evt) {
    addTransactionDefaultData();
    addMasterDefaultData();
}

    private void sDemoButtonActionPerformed(java.awt.event.ActionEvent evt) {

//processRecords();
}

    private void pDemoButtonActionPerformed(java.awt.event.ActionEvent evt) {

}

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {
    delete();
}

    private void iDFeildActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
}

    private void clearMFButtonActionPerformed(java.awt.event.ActionEvent evt) {
    masterList.clear();
    statusLbl.setText("Master File Cleared");
}

    private void nameSurFeildActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
}

    private void tranTypeFeildActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
}

    private void sortMasterButtonActionPerformed(java.awt.event.ActionEvent evt) {

}

    private void sortTranButtonActionPerformed(java.awt.event.ActionEvent evt) {
    bubbleSort();
    }


    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainApplication().setVisible(true);


            }
        });
    }

private DefaultListModel tranList = new DefaultListModel();
private DefaultListModel masterList = new DefaultListModel();
private DefaultListModel newMasterList = new DefaultListModel();

int index=0;
int recordNumber = 0;
int recordCount = 0;

    // Variables declaration - do not modify
    private javax.swing.JButton addButton;
    private javax.swing.JLabel asLabel;
    private javax.swing.JLabel blank;
    private javax.swing.JPanel bottom2Panel;
    private javax.swing.JPanel bottom3Panel;
    private javax.swing.JPanel bottom4Panel;
    private javax.swing.JPanel botton1Panel;
    private javax.swing.JButton clearMFButton;
    private javax.swing.JButton clearTFButton;
    private javax.swing.JLabel dcLabel;
    private javax.swing.JButton defaultRecordsButton;
    private javax.swing.JButton deleteButton;
    private javax.swing.JSlider demoSpeedSlider;
    private javax.swing.JLabel elLabel;
    private javax.swing.JTextArea errorLog;
    private javax.swing.JLabel etButton;
    private javax.swing.JLabel helpButton;
    private javax.swing.JTextArea helpTArea;
    private javax.swing.JTextField iDFeild;
    private javax.swing.JLabel idLabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JLabel mLabel;
    private javax.swing.JList mfJList;
    private javax.swing.JButton modifyButton;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameSurFeild;
    private javax.swing.JList newMList;
    private javax.swing.JButton pDemoButton;
    private javax.swing.JButton sDemoButton;
    private javax.swing.JCheckBox singleShotCB;
    private javax.swing.JLabel sliderLabel;
    private javax.swing.JLabel soLabel;
    private javax.swing.JButton sortMasterButton;
    private javax.swing.JButton sortTranButton;
    private javax.swing.JLabel statusLbl;
    private javax.swing.JLabel tLabel;
    private javax.swing.JList tfJList;
    private javax.swing.JPanel top1Panel;
    private javax.swing.JPanel top2Panel;
    private javax.swing.JPanel top3Panel;
    private javax.swing.JPanel top4Panel;
    private javax.swing.JTextField tranTypeFeild;
    // End of variables declaration
//-----------------------------------------------------------------
    public void addRecord()
    {
         StudentClass Nstudent = new StudentClass();

         Nstudent.setStudentName(nameSurFeild.getText());

         int IdtoInt = Integer.parseInt(iDFeild.getText());
         Nstudent.setStudentID(IdtoInt);

         Nstudent.setTransType(tranTypeFeild.getText());

         tranList.addElement(Nstudent.studentName+"     "+Nstudent.studentID+"     "+Nstudent.transType);

         clear();
         statusLbl.setText("Record Added To Transaction File!");

    }
//------------------------------------------------------------------
    public void clear()
    {
        nameSurFeild.setText("");
        iDFeild.setText("");
        tranTypeFeild.setText("");

    }
//------------------------------------------------------------------
    public void addTransactionDefaultData()
    {
        String [] defaultNames = {"Alvin Du","Ryan Gosden","Michelle Dowling","Toni Dowling"};
        int [] defaultID = {153679,417950,234512,762903};
        String []defaultTType = {"A", "A", "D","A"};

        for (int i = 0; i < defaultNames.length; i++)
        {
          recordCount = recordCount + 1;


            StudentClass Nstudent = new StudentClass();

            Nstudent.setStudentName(defaultNames[i]);
            Nstudent.setStudentID(defaultID[i]);
            Nstudent.setTransType(defaultTType[i]);

            tranList.addElement(defaultNames[i]+"     "+defaultID[i]+"     "+defaultTType[i]); //adds Object Array to JList


         statusLbl.setText("Default Records Loaded!");

        }
    }
//------------------------------------------------------------------

      public void addMasterDefaultData()
    {
        String [] defaultNames = {"Peter Smith","Paul Johnson","Amanda Fenech","Claire Borg"};
        int [] defaultID = {999999,876543,765789,654327};
        String []defaultTType = {"A", "A", "D","A"};

        for (int i = 0; i < defaultNames.length; i++)
        {
          recordCount = recordCount + 1;


             StudentClass Nstudent = new StudentClass();

            Nstudent.setStudentName(defaultNames[i]);
            Nstudent.setStudentID(defaultID[i]);

          masterList.addElement(defaultNames[i]+"     "+defaultID[i]+"     "+defaultTType[i]); //adds Object Array to JList


         statusLbl.setText("Default Records Loaded!");

        }
    }
//--------------------------------------------------------------------------------
    public void delete()
    {
       tranTypeFeild.setText("D");
       statusLbl.setText("Delete Transaction Type Selected");
   
    }
//------------------------------------------------------------------------------------

    public void modify()
    {
        tranTypeFeild.setText("M");
        statusLbl.setText("Modify Transaction Type Selected");
    }
//------------------------------------------------------------------

     public void bubbleSort() {

        StudentClass tmp;
        if (tranList.getSize() == 1) {
            return;
        }

        for (int i = 0; i < tranList.getSize(); i++) {
            for (int j = i + 1; j < tranList.getSize(); j++) {
                if (((StudentClass) tranList.get(i)).getStudentID() > ((StudentClass) tranList.get(j)).getStudentID()) {
                    tmp = (StudentClass) tranList.get(i);
                    tranList.set(i, tranList.get(j));
                    tranList.set(j, tmp);

                }
            }
        }
    }

}

StudentClass

package idcsjavaassignemnt2009;

public class StudentClass
{

    String studentName ;
    int studentID;
    String transType;


public int getStudentID()
{
return studentID;
}

public void setStudentID(int newStudentID)
{
this.studentID = newStudentID;
}
//--------------------------------------------------

public String getStudentName()
{
return studentName;
}

public void setStudentName(String newStudentName)
{
this.studentName = newStudentName;
}
//----------------------------------------------------
public String getTransType()
{
return transType;
}

public void setTransType(String newTransType)
{
this.transType = newTransType;
}
}

Error Msg:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to idcsjavaassignemnt2009.StudentClass
at idcsjavaassignemnt2009.MainApplication.bubbleSort(MainApplication.java:548)
at idcsjavaassignemnt2009.MainApplication.sortTranButtonActionPerformed(MainApplication.java:376)
at idcsjavaassignemnt2009.MainApplication.access$100(MainApplication.java:5)
at idcsjavaassignemnt2009.MainApplication$2.actionPerformed(MainApplication.java:89)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6216)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5981)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4583)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

I thought the error was a type mismatch, but StudentId is set to int...

Note your i range and j range they're in pairs! i is going one too many though the inner loop j is falling through so no problem but its not efficient!

//        for (int i = 0; i < tranList.getSize(); i++) {
        for (int i = 0; i < tranList.getSize() - 1; i++) {
            for (int j = i + 1; j < tranList.getSize(); j++) {

Not sure about your crash problem but your bubble sort is flawed!
How does a low number get all the way to the left!
Note that its good for pushing the highest number all the way to the right, but your low numbers are only bubbling back!

10   5  3  8 0
 |
5    3  8  0  10
      |
5    3  0  8  10
          |
5    3  0   8  10
5    3  0   |
5    3  0   8   10

In your case you''re moving the low limit by one each loop but its not done yet!

Since your highest number is moved immediately to its position, instead move the high range limit down by one since you know the highest number is already in position. The low numbers will bubble into their respective positions.
You can also use a do loop on the outer loop and clear a flag between the loops, and set the flag if a swap occurs. And on the outer do-while, it has to loop while the flag is set! So if everything was checked before both loops created, and nothing was swapped, then no need to waste time doing all the loops again as its already done!

That's too much code. Don't have time to debug it beyond what I did, but I did discover this: tranList.get (i) appears to not be returning an object of type StudentClass, so you can't execute getStudentId () on it.

That's this error:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to idcsjavaassignemnt2009.StudentClass

Therefore the problem is before the call to getStudentId () . This isn't the mismatch:

I thought the error was a type mismatch, but StudentId is set to int...

The mismatch is here:

if (((studentClass) tranList.get(i)).getStudentID() > ((studentClass) tranList.get(j)).getStudentID())

Look at tranList, make sure it's storing what you want.

What i thought was tranList is storing 3 strings and an int.
StudentID refering to the int.

int IdtoInt = Integer.parseInt(iDFeild.getText());
Nstudent.setStudentID(IdtoInt);

damm im lost.
My aim was to call the first item in the jList, retrieve its ID and compare to the next item in jList...

slowly giving up :(

If sorting is the only issue them fixed it with java.util.Collections.sort() method.

It's no longer just a sorting issue, it's a storage/access issue which must be solved before the sorting issue is touched. Put this program aside and start a much smaller program. The one you have now is too big to isolate the problem. I think I used JList once, years ago, and have forgotten all of it, so I'm of no use in debugging it. Clearly you are somewhere using it incorrectly. Decide whether you want to use a JList and if you do and if you've never used one before and don't know how, take some tutorials. Otherwise, go back to your old idea of ArrayList. Regardless, you can't expect to SORT using a JList or ArrayList until you actually know how to USE them and access the elements. If you don't, put the larger program and the sorting issues aside and simply learn how to use JList or ArrayList. First things first.

commented: Good suggestion. +10

Thanks VernonDozier again.
Ill back track to JLists again and try to understand it workings.

ill let you know the outcome, hopefully a positive one :)

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.18.1

<i>15.18: ...If the type of either operand of a + operator is String, then the operation is string concatenation....If only one operand expression is of type String, then string conversion is performed on the other operand to produce a string at run time. The result is a reference to a String object (newly created, unless the expression is a compile-time constant expression (ยง15.28))that is the concatenation of the two operand strings.
</i>
Also look at section 15.18.1.3

Additionally see:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

-----------------------------------------------
How does this apply?
In "addTransactionDefaultData" you have the following statement:

tranList.addElement(defaultNames+" "+defaultID+" "+defaultTType); //adds Object Array to JList


Since you use the "+" (string concatenation) operator, it results in a String.

Then in "bubbleSort" you do the following:
if (((StudentClass) tranList.get(i)).getStudentID() > ((StudentClass) tranList.get(j)).getStudentID())

-----------------------------------------------
"(StudentClass)" is a casting operation. It "casts" the following statement to that type.
The rest of the statement would probably thow a compiler error too, because you stored a String in tranList and "getStudentID" isn't a String method--it is a method in your StudentClass. I believe the compiler only reports the first error it finds on any given line.

So, to eliminate the compile error in your "bubblesort" change the following line from:

if (((StudentClass) tranList.get(i)).getStudentID() > ((StudentClass) tranList.get(j)).getStudentID())

TO:

if (tranList.get(i).compareToIgnoreCase(tranList.get(j)) )

This should eliminate the compile error. Will it make your program work? Probably not.

You probably should have done the following (or something similar):
In "addTransactionDefaultData":

tranList.addElement(Nstudent); //adds Object Array to JList

Then in "bubbleSort":
if (tranList.get(i).getStudentID() > tranList.get(j).getStudentID())

Why? Because according to your definition in StudentClass, getStudentID returns an int. And since you are now adding "Nstudent" to tranList--which is of type StudentClass--you can now use the method getStudentID.

Here's an example of printing to a jList:

private void displayData(ArrayList<StudentClass> myArrayList){
        String userData = "";

        //needed for jList
        Vector studentData = new Vector();

        //clear the jList box
        jList1.clearSelection();


        //add student data to vector
        for (int i=0; i<myArrayList.size();i++){
            studentData.add(myArrayList.get(i).getStudentID() + " " + myArrayList.get(i).getStudentName());
        }


        //if data exists add to jList, otherwise just leave it empty
        if (myArrayList.size() >0){
            jList1.setListData(studentData);
        }
        
    }
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.