私はjavaで作業しており、挿入および更新はJtable
です。何らかのエラーが発生し、エラーを特定できなくなりました。は、Javaプログラムのエラーを特定できません
エラー:
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)
と、これは私のコードです:
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);
}
}
まずはhttp://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-itをご覧ください。 – ajb
テーブルのプロパティを設定する前にテーブルにメモリを割り当てます。 –