Java JTextfield gives me only null data

I made a Mainmenu JFrame which can generate 4 different new frame represents each menu. Mainmenu frame was works fine. but I found that it could't get text from new frame textfield. here is error report and source code. please let me know how to fix it.

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

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at PhoneBook.actionPerformed(PhoneBook.java:166) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.Lightwe ightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(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.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.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)


The reason you are getting this problem is because you have created a new local variable with the same name as one of your class member variables:

TextField tf_name_input = new TextField();

instead of:

tf_name_input = new TextField();
链接地址: http://www.djcxy.com/p/73440.html

上一篇: Java.swing.ComboBox不能转换为Button classException

下一篇: Java JTextfield只给我空数据