-3

运行此应用程序时出现错误..get 异常并且没有在 jtextfield 上显示任何内容..所以给我那个片段..问题出在哪里?我在写入 jtextfield 时什么都得不到。。不从 mysql 数据库中获取记录。

跑:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.mylogin.CustomFocusListener.focusLost(CustomFocusListener.java:39)
at java.awt.AWTEventMulticaster.focusLost(AWTEventMulticaster.java:230)
at java.awt.Component.processFocusEvent(Component.java:6281)
at java.awt.Component.processEvent(Component.java:6145)
at java.awt.Container.processEvent(Container.java:2045)
at java.awt.Component.dispatchEventImpl(Component.java:4750)
at java.awt.Container.dispatchEventImpl(Container.java:2103)
at java.awt.Component.dispatchEvent(Component.java:4576)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1895)
at                                                              java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:96        5)
at   java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:627)
at java.awt.Component.dispatchEventImpl(Component.java:4620)
at java.awt.Container.dispatchEventImpl(Container.java:2103)
at java.awt.Component.dispatchEvent(Component.java:4576)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:672)
at java.awt.EventQueue.access$400(EventQueue.java:96)
at java.awt.EventQueue$2.run(EventQueue.java:631)
at java.awt.EventQueue$2.run(EventQueue.java:629)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116)
at java.awt.EventQueue$3.run(EventQueue.java:645)
at java.awt.EventQueue$3.run(EventQueue.java:643)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:642)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

Example.java

    package com.mylogin;


    import com.mylogin.CustomFocusListener;
    import java.awt.event.FocusListener;


import java.awt.event.*;
import javax.swing.*;

public class Example extends Box
{

JLabel txtName = new JLabel("Nothing Entered");

public Example(){
    super(BoxLayout.Y_AXIS);
    // Add FocusListener to first field
    final JTextField txtNo = new JTextField(20);
    txtNo.addFocusListener(new CustomFocusListener(txtNo));
    add(txtNo);

    // Add TextListener to first field
    final JTextField txtNo2 = new JTextField(20);
    txtNo2.addFocusListener(new CustomFocusListener(txtNo2));
    add(txtNo2);

    // Add TextListener to first field
    final JTextField txtNo3 = new JTextField(20);
    txtNo3.addFocusListener(new CustomFocusListener(txtNo3));
    add(txtNo3);

    add(new JButton("Do Something"));

    add(txtName);

}


public static void main(String[] args){
    final JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new Example());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}
}

CustomFocusListener.java

package com.mylogin;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.awt.Component;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class CustomFocusListener extends FocusAdapter
{

 JTextField field;
// private final Component add;



CustomFocusListener(JTextField txtNo)
{
     this.field = field;
}




public void focusLost(FocusEvent e)
{

    JLabel txtName = new JLabel();
   // JTextField field = new JTextField();
    txtName.setText(this.field.getText());       
    try 
    {

        Class.forName("com.mysql.jdbc.Driver");
        java.sql.Connection con = (java.sql.Connection)                                                                                   DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "ubuntu123");
        Statement st = (Statement) con.createStatement();
        String load = "Select * from supplierinfo where companyname = '" +            field.getText() + "'";
        ResultSet rs = st.executeQuery(load);
        while (rs.next()) 
        {
            txtName.setText(rs.getString("companyname"));

        }

                        con.close();

    }
    catch (Exception ae) 
    {
    }
4

1 回答 1

4

那行不通:

JLabel txtName = null;
JTextField field = null;
txtName.setText(field.getText());

你正在使用txtNamewith null。这就是你的 NPE 的原因。顺便说一句field也为空!

将其更改为此,它将运行:

JLabel txtName = new JLabel();
txtName.setText(this.field.getText());

CustomFocusListener(JTextField txtNo)
{
     this.field = txtNo;
}
于 2013-04-02T12:00:25.767 回答