我在运行这个应用程序时遇到错误..例外并且没有在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)
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);
}
}
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)
{
}
答案 0 :(得分:4)
无效:
JLabel txtName = null;
JTextField field = null;
txtName.setText(field.getText());
您正在txtName
使用null
。这就是你的NPE的原因。
顺便说一句field
也是空的!
将其更改为此并运行:
JLabel txtName = new JLabel();
txtName.setText(this.field.getText());
和
CustomFocusListener(JTextField txtNo)
{
this.field = txtNo;
}