无法识别java程序中的错误

时间:2016-05-11 05:01:17

标签: java swing

我在java中工作并在Jtable中插入和更新。发生了一些错误,我无法识别错误,请帮忙。

错误:

  

显示java.lang.NullPointerException       在jtabledemo.Accounts。(Accounts.java:57)       在jtabledemo.Accounts $ 1.run(Accounts.java:33)       at java.awt.event.InvocationEvent.dispatch(Unknown Source)       at java.awt.EventQueue.dispatchEventImpl(Unknown Source)       在java.awt.EventQueue.access $ 400(未知来源)       在java.awt.EventQueue $ 3.run(未知来源)       在java.awt.EventQueue $ 3.run(未知来源)       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)       在java.awt.EventDispatchThread.run(未知来源)

这是我的代码:

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

3 个答案:

答案 0 :(得分:2)

根据异常错误在第57行,即table.setModel(null);

这是因为你正在第62行初始化表[table = new JTable();]并在第57行之前使用它。

答案 1 :(得分:0)

当你在对象中有空值时,这是你经常遇到的一个基本例外。

检查程序中的任何空对象。

你班上可能排在第57位。

java.lang.NullPointerException at jtabledemo.Accounts.(Accounts.java:57) at 

答案 2 :(得分:0)

在使用表格对象之前添加table=new JTable(model);

你声明实例private JTable table;但没有初始化。(实例对象的默认值为null)。那就是为什么你得到nullpointerexception

此外,您还需要使final DefaultTableModel model=new DefaultTableModel();final Object[] row=new Object[2];成为最终版,因为局部变量模型和行是从内部类中访问的;需要宣布为最终