-3

I am working in java and inserting and updating in Jtable. Some error occurs and i am unable to identify the error please help.

Error:

java.lang.NullPointerException at jtabledemo.Accounts.(Accounts.java:57) at jtabledemo.Accounts$1.run(Accounts.java:33) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$400(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.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)

and this is my code:

package jtabledemo;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Accounts extends JFrame {

private JPanel contentPane;
private JTable table;
private JTextField textField;
private JTextField textField_1;
private JButton btnUpdate;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Accounts frame = new Accounts();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Accounts() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 609, 387);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);


    Object [] columns={"Debit","Credit"};
    DefaultTableModel model=new DefaultTableModel();
    model.setColumnIdentifiers(columns);
    table.setModel(null);
    //table.setModel(model);
    table.setBackground(Color.blue);
    table.setForeground(Color.white);

    table = new JTable();
    table.setBounds(63, 117, 383, 202);
    contentPane.add(table);

    JLabel lblCredit = new JLabel("Credit");
    lblCredit.setBounds(84, 11, 46, 14);
    contentPane.add(lblCredit);

    JLabel lblDebit = new JLabel("Debit");
    lblDebit.setBounds(84, 49, 46, 14);
    contentPane.add(lblDebit);

    textField = new JTextField();
    textField.setBounds(131, 8, 86, 20);
    contentPane.add(textField);
    textField.setColumns(10);

    textField_1 = new JTextField();
    textField_1.setBounds(131, 46, 86, 20);
    contentPane.add(textField_1);
    textField_1.setColumns(10);

    JButton btnAdd = new JButton("Add");
    Object[] row=new Object[2];
    btnAdd.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            row[0]=textField.getText();
            row[1]=textField_1.getText();
            model.addRow(row);
        }
    });
    btnAdd.setBounds(248, 45, 89, 23);
    contentPane.add(btnAdd);

    btnUpdate = new JButton("Update");
    btnUpdate.setBounds(248, 7, 89, 23);
    contentPane.add(btnUpdate);
}
}
ketan
  • 19,129
  • 42
  • 60
  • 98
SmartF
  • 99
  • 2
  • 12

3 Answers3

2

As per exception error is at line 57 i.e. table.setModel(null);

And this is because you are initializing table at line 62 [table = new JTable();] and using it before that at line 57.

Prashant Thakkar
  • 1,383
  • 1
  • 12
  • 15
0

It is a basic exception you face many time when you have null values in objects.

Check for any null objects in your program.

maybe 57th line in your class.

java.lang.NullPointerException at jtabledemo.Accounts.(Accounts.java:57) at 
Harish Pathak
  • 1,567
  • 1
  • 18
  • 32
  • i have checked line number 57 which was "table.setModel(null);" i replaced null with "table.setModel(model);" but it gave me the same error. – SmartF May 11 '16 at 05:07
  • The `null` in `table.setModel(null)` won't give you an exception on that line. Your problem is that `table` is null. – ajb May 11 '16 at 05:08
  • yup done with my error. actually i was using jtable before initializing it. – SmartF May 11 '16 at 05:11
0

add table=new JTable(model); before using table object.

you declare instance private JTable table; but not initialize.(default value of instance object is null).So that's why you are getting nullpointerexception

And also you need to make final DefaultTableModel model=new DefaultTableModel(); and final Object[] row=new Object[2]; final because local variable model and row is accessed from within inner class; needs to be declared final

Khan Abdulrehman
  • 816
  • 2
  • 10
  • 22