Hi everyone!

In my code when I click on the "Show Results" button it shows me the result is a table in another interface other than the main interface, untel now everything is fine but when I changed the method : I just want to show this table in the main interface, I created a new panel and all, the problem is a problem of showing this table in this panel so here: the code of the panel:

JPanel panel_4 = new JPanel();
 panel_4.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
 panel_4.setBounds(115, 510, 450, 130);
 getContentPane().add(panel_4);
 panel_4.setLayout(null);
 JLabel lblNewLabel = new JLabel("Resource Aware Planning Results" );
 lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD | Font.ITALIC, 14));
 lblNewLabel.setBounds(125, 11, 236, 14);
 panel_4.add(lblNewLabel);
 JScrollPane scrollPane1 = new JScrollPane();
 scrollPane1.setBounds(66, 55, 312, 49);
 panel_4.add(scrollPane1);
 table = new JTable();
 scrollPane1.setViewportView(table);
 DefaultTableModel modelres = new DefaultTableModel();
 modelres.setRowCount(1);
 DefaultTableModel modelres1 = new DefaultTableModel();
 modelres1.setRowCount(1);

and here is the Show Results button code:

if ( radio3.isSelected())
 {
 for(int i=0; i<n;i++)
    {
    //int k=i+1;
    String object = "T"+i;
    modelres.addColumn(object);// modelres est globale ainsi que modelres1
    int index=plan.NodeIndex(i, res, m);
    object="N"+index;
    modelres.setValueAt(object, 0, i);
    }
  table.setModel(modelres);
 //resultsFrame.setVisible(true);
 }
 else if ( radio4.isSelected())
 {
    for(int i=0; i<n;i++)
    {
   //int k=i+1;
   String object = "T"+i;
   modelres1.addColumn(object);
   int index=plan.NodeIndex(i, res, m);
   object="N"+index;
   modelres1.setValueAt(object, 0, i);
    }
   table.setModel(modelres1);
    //resultsFrame.setVisible(true);
     }

and it shows me this error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at projetPFE.GUI2.btnShowResults(GUI2.java:978)
at projetPFE.GUI2$5.actionPerformed(GUI2.java:363)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

I honestly do not know how to fix this error
I'll be very grateful if you can help me
Thank you in advance

Dani AI

Generated

Quick diagnosis: a NullPointerException thrown at the call that adds a column means the table-model reference is null when the button handler runs. As noted, that is the immediate cause. The usual reasons are (a) the model was never assigned to the field that the handler uses, or (b) you accidentally redeclared a local model variable that hides the class field (shadowing), so the field remains null when the button code executes.

Practical fixes:

  • Declare the model as a class field and initialize it during GUI construction (before any action listeners run). Do not redeclare the same name inside a constructor or init method — assign to the existing field instead.
  • Check your action listener uses that same field (not a different/local variable). Use your IDE’s “Find usages” for the model name to locate duplicate declarations.

Example pattern (use different names than in the thread to avoid shadowing):

// class field
private DefaultTableModel resultsModel;

// during initialization
resultsModel = new DefaultTableModel();
resultsModel.setRowCount(1);
resultsTable.setModel(resultsModel);

Troubleshooting checklist:

  • Put a breakpoint or a quick log at the start of the button handler to verify the model reference (and the table reference) is not null.
  • If you see a local declaration like DefaultTableModel model = ... inside a method, remove the type so you assign to the field instead.
  • Ensure you create at least one row (or add rows) before calling setValueAt, otherwise you’ll get index errors even if the model is non-null.

Notes: , once the model is the same shared field that the listener touches, addColumn and setValueAt will work as expected. Also consider building the model fully first (columns + rows) and then calling table.setModel(...) for simpler, less error-prone updates.

Recommended Answers

All 3 Replies

The exception message identifies the exact line where the NPE happened, but we don't have the original file to interpret the line number. What is the code on line 978?

ok the ligne is :

 modelres.addColumn(object);

That looks like modelres is null (not initialised). I see the code where it is defined and initialised, so maybe there is a duplicate definition somewhere?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.