1

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

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Aimad Majdou
  • 563
  • 4
  • 13
  • 22

2 Answers2

3

Initialize the JTable jTable1 prior to setting the TableModel

jTable1 = new JTable(model);
jTable1.setModel(model);
Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

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

rimero
  • 2,383
  • 1
  • 14
  • 8