我制作了一个Mainmenu JFrame,它可以生成4个不同的新帧代表每个菜单。 Mainmenu框架工作正常。但我发现它无法从新的框架文本字段中获取文本。这是错误报告和源代码。请让我知道如何解决它。
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
import javax.swing.JTable;
public class PhoneBook extends JFrame implements ActionListener
{
private String[] colName;
JPanel panel_main;
JPanel panel_phbook;
JPanel panel_numb;
JPanel panel_grp;
JButton btn_main_phb;
JButton btn_main_name;
JButton btn_main_numb;
JButton btn_main_grp;
JTextArea ta_name_serch;
TextField tf_name_input;
JButton btn_name_search;
JButton btn_name_cancel;
JButton btn_name_modify;
JButton btn_name_del;
JButton btn_name_send;
JTextArea ta_numb_serch;
TextField tf_numb_input;
JButton btn_numb_search;
JButton btn_numb_cancel;
JButton btn_numb_modify;
JButton btn_numb_del;
JButton btn_numb_send;
JButton btn_grp_search;
JButton btn_grp_cancel;
JButton btn_grp_modify;
JButton btn_grp_del;
JButton btn_grp_send;
private JTable table;
public static void main(String[] args)
{
PhoneBook frame = new PhoneBook();
}
public PhoneBook()
{
colName = new String[3];
panel_main = new JPanel();
panel_main.setLayout(new GridLayout());
btn_main_phb = new JButton("phonbook");
btn_main_name = new JButton("name");
btn_main_numb = new JButton("number");
btn_main_grp = new JButton("grp");
btn_main_phb.addActionListener(this);
btn_main_name.addActionListener(this);
btn_main_numb.addActionListener(this);
btn_main_grp.addActionListener(this);
panel_main.add(btn_main_phb);
panel_main.add(btn_main_name);
panel_main.add(btn_main_numb);
panel_main.add(btn_main_grp);
// table = new JTable();
// table.setColumnSelectionAllowed(true);
// panel_main.add(table);
// table.set
panel_phbook = new JPanel();
panel_phbook.setLayout(new GridLayout());
// panel_phbook.add(btn_main_phb);
// panel_phbook.add(btn_main_name);
// panel_phbook.add(btn_main_numb);
// panel_phbook.add(btn_main_grp);
btn_name_search = new JButton("Serch");
btn_name_cancel = new JButton("Serch Cancel");
btn_name_modify = new JButton("Modi");
btn_name_del = new JButton("del");
btn_name_send = new JButton("sms send");
TextField tf_name_input = new TextField();
panel_phbook.add(tf_name_input);
panel_phbook.add(btn_name_search);
btn_name_search.addActionListener(this);
//
panel_numb = new JPanel();
panel_grp = new JPanel();
//getContentPane().setLayout(BorderLayout());
getContentPane().setLayout(new BorderLayout());
setLocation(500, 400);
getContentPane().add(panel_main);
setSize(650,150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
@Override public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource().equals(btn_main_phb))
{
JFrame frame2 = new JFrame();
frame2.getContentPane().add(panel_phbook);
frame2.setLocation(400, 300);
frame2.setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
frame2.setVisible(true);
}
else if(e.getSource().equals(btn_main_name))
{
}
else if(e.getSource().equals(btn_main_numb))
{
}
else if(e.getSource().equals(btn_main_grp))
{
}
else if(e.getSource().equals(btn_name_search))//this is the problem
{
System.out.println(tf_name_input.getText());
}
}
线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException 在PhoneBook.actionPerformed(PhoneBook.java:166)at javax.swing.AbstractButton.fireActionPerformed(未知来源)at javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source)at javax.swing.DefaultButtonModel.fireActionPerformed(未知来源)at javax.swing.DefaultButtonModel.setPressed(未知来源)at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知 来自)java.awt.Component.processMouseEvent(未知来源)at javax.swing.JComponent.processMouseEvent(未知来源)at java.awt.Component.processEvent(未知来源)at java.awt.Container.processEvent(未知来源)at java.awt.Component.dispatchEventImpl(未知来源)at java.awt.Container.dispatchEventImpl(未知来源)at java.awt.Component.dispatchEvent(未知来源)at java.awt.LightweightDispatcher.retargetMouseEvent(未知来源)at java.awt.LightweightDispatcher.processMouseEvent(未知来源)at java.awt.LightweightDispatcher.dispatchEvent(未知来源)at java.awt.Container.dispatchEventImpl(未知来源)at java.awt.Window.dispatchEventImpl(未知来源)at java.awt.Component.dispatchEvent(未知来源)at java.awt.EventQueue.dispatchEventImpl(未知来源)at java.awt.EventQueue.access $ 200(未知来源)at java.awt.EventQueue $ 3.run(未知来源)at java.awt.EventQueue $ 3.run(未知来源)at java.security.AccessController.doPrivileged(Native Method)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知 来源)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知 来自)java.awt.EventQueue $ 4.run(未知来源)at java.awt.EventQueue $ 4.run(未知来源)at java.security.AccessController.doPrivileged(Native Method)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知 来自)java.awt.EventQueue.dispatchEvent(未知来源)at java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知 来自java.awt.EventDispatchThread.pumpEvents(未知来源) at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
答案 0 :(得分:3)
您遇到此问题的原因是您创建了一个与您的一个类成员变量同名的新局部变量:
TextField tf_name_input = new TextField();
而不是:
tf_name_input = new TextField();