I DefaultListModel trying to output a JList without a DefaultListModel , using my own List and add method to add to the JList . I get a list of errors like

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.JList $ 4.getSize (Unknown Source)

The task is exactly in the output without DefaultListModel .

In the MyForm() constructor

  import laba2.ACrafts; import laba2.List; import javax.swing.*; public class MyForm extends JFrame{ JFrame formShow; private JList<ACrafts> list; public List li; private JButton btnAdd; public MyForm() { li = new List(); JPanel panelShow = new JPanel(); formShow = new JFrame("Aircrafts"); formShow.setBounds(100, 100, 700, 550); formShow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); formShow.add(panelShow); panelShow.setLayout(null); list = new JList<ACrafts>(); list.setLayoutOrientation(JList.VERTICAL); list.setBounds(20, 20, 640, 150); ACrafts[] arr; arr = li.toArray(); list.setListData(arr); panelShow.add(list); public void setData(ACrafts a) { li.add(a); } } 

Methods add in the List class

 class Element{ Element left; ACrafts data; Element right; } public class List implements ListImpl{ private Element first; private Element last; private int size; public List() { first = null; last = null; size = 0; } public void add(ACrafts e){ Element elem = new Element(); elem.data = e; if (size == 0) first = last = elem; else last.right = elem; elem.left = last; last = elem; size++; } } 

In Maine:

 public static void main(String[] args) { ACrafts[] aircraft = new ACrafts[5]; aircraft[0] = new ACrafts("Boeing 767-300F","США",7130,54000,13100,850); aircraft[1] = new ACrafts("Airbus 310-300F","Франция",39000,9600,12200,850); aircraft[2] = new ACrafts("Boeing 757-200F","США",7100,39780,12500,935); aircraft[3] = new ACrafts("IL-86","Украина",3300,15000,11000,950); aircraft[4] = new ACrafts("Avro RJ-100","Великобритания",2255,9500,10600,890); MyForm form = new MyForm(); form.setData(aircraft[0]); form.setData(aircraft[1]); form.setData(aircraft[2]); form.setData(aircraft[3]); form.setData(aircraft[4]); form.formShow.setVisible(true); } 

And if you make a list by pressing a button, then everything is displayed. And the previous version is not.

 btnAdd = new JButton("Просмотреть"); btnAdd.setBounds(30, 250, 100, 50); panelShow.add(btnAdd); btnAdd.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent event) { ACraft[] petsArr; petsArr = li.toArray(); z1.setListData(petsArr);} }); z1 = new JList(); z1.setLayoutOrientation(JList.VERTICAL); z1.setBounds(20, 20, 640, 150); panelShow.add(z1); 
  • That is, the problem is simply that the list is not displayed ? That is, the removal is not to blame? - VladD
  • @VladD, the task is still in the distance, but the list is not even displayed. I previously managed to display the list using the DefaulfListModel, but at the same time I couldn’t remove the element using my remove2 method. The question is how to implement the removal? - Identin
  • Okay, but then let's divide the problems. (1) You cannot delete an item from DefaulfListModel (2) You cannot display items in any other way. I think it’s worth asking separately. And you asked a question that does not reflect any of the two immediate problems. It seems to me that the main task need only be mentioned in the question. (I’m not an awt person at all, but it seems everyone’s asleep already.) - VladD

0