Hi.

I'm developing a software for my degree project.

I have some problems in sorting the probability item from linkedList object. Here are the snippets of my code (I have almost 400 lines, so if the below is not enough, please let me know)

CLASS LINKEDLIST

class LinkedList implements Comparable
{
    private char symbol;
    private float probability;
    private String codeword;
    private LinkedList next;
    private int flag;

    public int compareTo(Object other) 
    {
        if (this.probability == ((LinkedList) other).probability)
            return 0;
        else if ((this.probability) > ((LinkedList) other).probability)
            return 1;
        else
            return -1;
    }
    
     
    public LinkedList(char s, float p, String c, int f)
    {
        symbol = s;
        probability = p;
        codeword = c;
        flag = f;
    }

    public void setProbability(float p)
    {
        probability = p;
    }

    public void setCodeword(String c)
    {
        codeword = c;
    }

    public void setFlag(int f)
    {
        flag = f;
    }

    public float getProbability()
    {
        return probability;
    }
    public int getFlag()
    {
        return flag;
    }

    public char getSymbol()
    {
        return symbol;
    }
}

SOMEWHERE INSIDE CLASS TEST

//sort window
   public static JPanel getSortWindow(LinkedList linkedList[], int symbolNum)
   {
        Test sortWindow = new Test();

        sortWindow.setVisible(true);
        sortWindow.setSize(500,500);

        //JPanel newSortPanel = new JPanel(new GridLayout(symbolNum,2));

        for(l = 0; l < symbolNum; l++)
        {
            System.out.println("Symbol: " + linkedList[l].getSymbol() + " Prob: " + linkedList[l].getProbability());
        }

        Collections.sort(linkedList); //the problem is here

        //after sorting
        for(l = 0; l < symbolNum; l++)
        {
            System.out.println("Symbol: " + linkedList[l].getSymbol() + " Prob: " + linkedList[l].getProbability());
        }

        sortWindow.add(newSortPanel);
        
        return newSortPanel;
    }

the error at --> Collections.sort(linkedList);

it says "Cannot find symbol"

Easier to write

class LinkedList implements Comparable<LinkedList> {

   public int compareTo(LinkedList other) {     // no cast required
      return probability - other.probability;
   }
}

but as probability is a float better to

class LinkedList implements Comparable<LinkedList> {

   public int compareTo(LinkedList other) {
      return Float.compare(probability, other.probability);
   }
}

And I will used Arrays.sort() rather than Collections.sort()
It is an array that you sort

I already changed the class LinkedList to

and used the Arrays.sort(linkedList)

However, I get this error instead:


Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at LinkedList.compareTo(Test.java:38)
at LinkedList.compareTo(Test.java:28)
at java.util.Arrays.mergeSort(Arrays.java:1144)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.sort(Arrays.java:1079)
at Test.getSortWindow(Test.java:460)
at Test$8.actionPerformed(Test.java:376)
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:6041)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5806)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4413)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2440)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

It is because your array of LinkedList is not full
I guess it contains symbolNum elements but its length is > then that so it has null elements at the end of it.

You will have to make a copy of it into an array of LinkedList[symbolNum] before sending it to sort()

i already make a copy of the linkedList,

using the constructor

//constructor 1
    public LinkedList(char s, float p, String c, int f)
    {
        symbol = s;
        probability = p;
        codeword = c;
        flag = f;
    }

    //constructor for copyList
    public LinkedList(LinkedList copyLinkedList)
    {
        this(copyLinkedList.getSymbol(), copyLinkedList.getProbability(), copyLinkedList.getCode(), copyLinkedList.getFlag());
    }

and this

LinkedList copyList[] = new LinkedList[symbolNum];

        for(l = 0; l < symbolNum; l++)
        {
            copyList[i] = new LinkedList(linkedList[i]);
            System.out.println("Symbol: " + copyList[l].getSymbol() + " Prob: " + copyList[l].getProbability());
        }

but i still get the same error


Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
Symbol: A Prob: 0.3 <-- the first item is printed
at Test.getSortWindow(Test.java:452)
at Test$8.actionPerformed(Test.java:388)
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:6041)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5806)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4413)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2440)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)


can anyone help me with this?

i think the problem has been solved and i finally managed to sort the object. thank you very much!

here's the working code

LinkedList copyList[] = new LinkedList[symbolNum];

         //copy object
        for(int j = 0; j < symbolNum; j++)
        {
            copyList[j] = new LinkedList(linkedList[j]);
            System.out.println("Linked List:" + j + " " + copyList[j].getSymbol() + " " + copyList[j].getProbability());

        }

        Arrays.sort(copyList);

        //after sorting once (increasing)
        for(int j = 0; j < symbolNum; j++)
        {
            System.out.println("Linked List:" + j + " " + copyList[j].getSymbol() + " " + copyList[j].getProbability());

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