Note that there are some explanatory texts on larger screens.

plurals
  1. POUse TreeSet to sort JList
    primarykey
    data
    text
    <p>I'm doing an assignment where I'm making a program that generates 20 random numbers and puts them in an arraylist, a hashset and a treeset. It is supposed to show those numbers in JList1. Then, I am going to use HashSet to display unique numbers only, and TreeSet to display the numbers sorted. I made three JLists, and made one function for each of them to update the GUI (there's probably an easier way to do this, but thats all I could come up with due to me being quite new to java). So, for example my updateGUI function used to display the arraylist in my JList looks like this:</p> <pre><code>public void updateGUI(JList someList, ArrayList&lt;Integer&gt; anyarraylist){ DefaultListModel m = new DefaultListModel(); for (Object s : anyarraylist) { m.addElement(s); } someList.setModel(m); } </code></pre> <p>I then call that function when I press a button by using ActionEvent:</p> <pre><code>@Override public void actionPerformed(ActionEvent arg0) { generateNumbers(); this.updateGUI(this.numbers1, numberlist); </code></pre> <p>Where generateNumbers is my function to generate 20 random numbers, numbers1 is my first JList and numberlist is the name of my arraylist.</p> <p>I did pretty much the exact same thing for HashSet to display a JList with unique numbers, and it worked. However, I then tried to do the same for TreeSet, which gave me a "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"-error. The function looks like this:</p> <pre><code> public void oppdaterGUI3(JList someList, TreeSet&lt;Integer&gt; anysortedlist){ DefaultListModel m = new DefaultListModel(); for (Object s : anysortedlist) { m.addElement(s); } someList.setModel(m); } </code></pre> <p>Why doesn't this work? I thought TreeSet would work the same way as HashSet. Any help is appreciated. I can post the full error and code if necessary.</p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload