Aimad Majdou
Aimad Majdou

Reputation: 583

Populate a jTable from an ArrayList

I'm trying to populate a JTable from an ArrayList, The ArrayList is filled with data from my database.

this is the code I tried :

DefaultTableModel model = new DefaultTableModel();

        model.setColumnIdentifiers(new String[]{"Numéro d'ordre", "Article", "Quantité", "Remarque"});
        for (gestionstock.LigneBonInterne o : listLigneBonInterne) {
            model.addRow(new String[]{o.getNumOrder().toString(), o.getdesgArt(), o.getQte().toString(), o.getRemarque()});
            System.out.println(o.toString());
        }

        jTable1.setModel(model);

But I get this error message :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at magasinier.BonInterneDetails.(BonInterneDetails.java:63)

the ligne 63 is : jTable1.setModel(model);

I did a test to see if the ArrayList is filled, and I found that the ArrayList is filled with records which means that there is no problem with filling the ArrayList

How can I solve this problem ?

EDIT :

I tried to create the JTable using code and assign it to ScrollPane :

JTable jTable1 = new JTable(model);
        jTable1.setModel(model);
         jScrollPane1.setViewportView(jTable1);

But I still get the same error this time is the line : jScrollPane1.setViewportView(jTable1);

Upvotes: 1

Views: 6335

Answers (2)

rimero
rimero

Reputation: 2383

In Netbeans palette you can specify some custom post-initialization code or use a custom constructor.

Upvotes: 0

Reimeus
Reimeus

Reputation: 159754

Initialize the JTable jTable1 prior to setting the TableModel

jTable1 = new JTable(model);
jTable1.setModel(model);

Upvotes: 3

Related Questions