0

I'm trying to display JList without DefaultListModel. I use my own method add to add elements to JList, but it gives a lot of errors, such as

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

How it can be fixed?

Creating JList

    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);      
            }
        }

Method add in List.java

    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++;
        }
    }

Main function (setting data)

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);

    }

ACrafts.java

public class ACrafts implements Comparable<ACrafts> {
    public String name;
    public String production;
    public int flightRange;
    public int weight;
    public int flightHeight;
    public int speed;

    public ACrafts(String n, String pr, int fR, int w, int fH, int s){
        this.name=n;
        this.production=pr;
        this.flightRange=fR;
        this.weight=w;
        this.flightHeight=fH;
        this.speed=s;
    }
}
rokky
  • 13
  • 4
  • *"I'm trying to display `JList` without `DefaultListModel`."* .. Why without `DefaultListModel`? For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson May 23 '15 at 09:31
  • 1
    `panelShow.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 23 '15 at 09:32
  • @AndrewThompson I think it's his own.List.`public class List implements ListImpl` – Sergiy Medvynskyy May 23 '15 at 09:38
  • 1
    @AndrewThompson It's my own List. "why without `DefaultListModel`" - because I want implement my own method `add`, and using `DefaultListModel` I can only use `addElement`. – rokky May 23 '15 at 10:04
  • 1
    *""why without `DefaultListModel`" - because I want implement my own method add, and using `DefaultListModel` I can only use `addElement`."* Well, there is the possibility to `extends DefaultListModel implements ListImpl` then define an `add(..)` method that just **calls** the `addElement(..)` method! This is OO, folks! OTOH I suspect this is some sort of learning exercise about implementing a list and that your instructor would not want their students to veer off into other matters when they are trying to teach list implementations. Where is the MCVE? When can we expect to see it? – Andrew Thompson May 23 '15 at 10:09
  • 1
    Also consider extending `AbstractListModel` to get the event plumbing, _required_ by `JList`, without the `Vector` baggage. – trashgod May 23 '15 at 10:55

0 Answers0