0
我正在嘗試製作一個簡單的地址簿GUI,它最終能夠將所有字段(如姓名編號)添加到數據庫中。目前,我想補充的方法來改變,當按下一個按鈕會發生什麼,但我不斷收到這個錯誤...Java swing簡單地址簿事件錯誤
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at AddressBook.actionPerformed(AddressBook.java:122)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
.
.
. theres some more but you get the point
我在想,如果有人可以幫助我瞭解爲什麼我不斷收到這個錯誤甚至無論如何改善這個程序的結構。謝謝!
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;
public class AddressBook implements ActionListener{
JFrame frame;
JPanel Master, centerInfo, buttonInfo;
GridBagConstraints constraints;
GridBagConstraints topconstraint;
JLabel jlb_firstName, jlb_lastName, jlb_phoneNum, jlb_searchInfo;
JTextField jtf_firstName, jtf_lastName, jtf_phoneNum;
JTextArea jta_searchInfo;
JButton jb_add, jb_search, jb_share;
Container pane;
String firstName, lastName, phoneNum;
//address book designed in GridBagLayout via swing
public static void main(String[] args)
{
AddressBook a = new AddressBook();
System.out.println("hello world");
a.start_gui();
}
public void start_gui()
{
JFrame frame = new JFrame("Address Book");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = new Container();
pane = frame.getContentPane();
layComponents(pane);
//frame.setSize(400, 400);
frame.setSize(400,400);
frame.setVisible(true);
}
private void layComponents(Container contentPane)
{
//PANELS,CONTENTPANE,CONSTRAINTS
JPanel Master = new JPanel();
JPanel centerInfo = new JPanel(new GridBagLayout());
JPanel buttonInfo = new JPanel(new FlowLayout(FlowLayout.LEADING,10,5));
Master.add(centerInfo, BorderLayout.CENTER);
Master.add(buttonInfo, BorderLayout.SOUTH);
contentPane.add(Master); //set style of layout for GUI
GridBagConstraints constraints = new GridBagConstraints(); //layout constraints
GridBagConstraints topconstraint = new GridBagConstraints(); //constraint against top of GUI
constraints.insets = new Insets(5,5,10,10);
//components: firstName, lastName, phoneNum, info, Add, Search(need to add oracle sql db)
//LABELS
JLabel jlb_firstName = new JLabel("First Name");
topconstraint.gridx =1;
topconstraint.gridy =0;
topconstraint.insets = new Insets(20,5,10,10); //Insets(top,left,right,down)
centerInfo.add(jlb_firstName, topconstraint);
JLabel jlb_lastName = new JLabel("Last Name");
constraints.gridx =1;
constraints.gridy =2;
centerInfo.add(jlb_lastName,constraints);
JLabel jlb_phoneNum = new JLabel("Phone Number");
constraints.gridx =1;
constraints.gridy =4;
centerInfo.add(jlb_phoneNum,constraints);
JLabel jlb_searchInfo = new JLabel("Search Info");
constraints.gridx =1;
constraints.gridy =6;
centerInfo.add(jlb_searchInfo, constraints);
//TEXTFIELDS
JTextField jtf_firstName = new JTextField("Enter New First Name"); //to clear textfield when clicked need actionlisteners
topconstraint.gridx =3;
topconstraint.gridy =0;
centerInfo.add(jtf_firstName,topconstraint);
JTextField jtf_lastName = new JTextField("Enter New Last Name");
constraints.gridx =3;
constraints.gridy =2;
centerInfo.add(jtf_lastName, constraints);
JTextField jtf_phoneNum = new JTextField("Enter New Phone Number");
constraints.gridx =3;
constraints.gridy =4;
centerInfo.add(jtf_phoneNum, constraints);
//TEXTAREA
Border loweredetched;
loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
JTextArea jta_searchInfo = new JTextArea(10,20);
jta_searchInfo.append("click me");
jta_searchInfo.selectAll();
jta_searchInfo.requestFocus();
jta_searchInfo.setBorder(loweredetched);
constraints.gridx =3;
constraints.gridy =6;
centerInfo.add(jta_searchInfo, constraints);
//buttons will use flowlayout
//BUTTONS
JButton jb_add = new JButton("Add");
buttonInfo.add(jb_add);
JButton jb_search = new JButton("Search");
buttonInfo.add(jb_search);
JButton jb_share = new JButton("Share");
buttonInfo.add(jb_share); //share info with ppl. need a new share gui
//Listen for any component actions performed
jb_add.addActionListener(this);
//jb_search.addActionListener(this);
//jb_share.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
firstName = jtf_firstName.getText();
System.out.println(firstName);
}
}
您確實需要學習調試代碼,嘗試在代碼中將對象初始化並使用的位置處放置斷點,然後比較差異並通過逐步執行代碼或添加新斷點來縮小搜索範圍 – MadProgrammer 2015-01-20 23:40:10
開該代碼的第122行,您正在調用一個對象爲null的方法。 (錯誤消息的第一行是'java.lang.NullPointerException'。) – tbodt 2015-01-20 23:46:58