java无法获取JTextField用户输入以搜索文本文件
似乎无法让扫描仪将用户输入从JTextField匹配到文本文件。例如:搜索员工编号1121,看看是谁。数字及;来自文本文件的名称
更新:完整代码和以下更改
public class custSearch extends JFrame{
private static final long serialVersionUID = 1L;
public JTextField userInput;
// Declaring Window Name, Size & Functions
public custSearch(){
super ("Customer Records");
setSize(630,250);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//NumberFormat integerFieldFormatter = NumberFormat.getIntegerInstance();
//integerFieldFormatter.setGroupingUsed(false);
// Declaring JPanels & New FlowLayouts
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout(-2));
// Declaring JLabels
JLabel cs = new JLabel("Search Phone Number:");
// Declaring JButtons
JButton sb1 = new JButton("Search");
// Declaring JFormattedTextFields
//JFormattedTextField cNumber = new JFormattedTextField(integerFieldFormatter);
//cNumber.setColumns(7);
JTextField cNumber = new JTextField();
cNumber.setColumns(7);
// Declaring JPanel Layout
p1.add(cs);
p1.add(cNumber);
p1.add(sb1);
add(p1, BorderLayout.NORTH);
//Create File & Save Details
//sb.addActionListener(new ActionListener() {
//@Override
// public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// File file = new File("Customers.txt");
// try{
// if(file.exists()==false){
// file.createNewFile();
// }
// PrintWriter pw = new PrintWriter(new FileWriter(file, true));
// pw.println(cNumber.getText());
// pw.close();
// }catch(IOException e1){
// JOptionPane.showMessageDialog(null,"<html>Customer File Could Not Be Created<p>Make Sure Program Has File Write Perssions");
// }
//}
//});
sb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try(Scanner sc = new Scanner(new File("Customers.txt"))) {
while(sc.hasNextInt()) {
int cNumber = sc.nextInt();
if(cNumber == Integer.parseInt(userInput.getText()))
JOptionPane.showMessageDialog(null, "Customer Found" + cNumber);
break;
}
}
catch(IOException e1) {
JOptionPane.showMessageDialog(null,"Error");
}
}
});
}
}
得到这个错误
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at custSearch$1.actionPerformed(custSearch.java:83)
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.LightweightDispatcher.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$500(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)
at java.awt.EventDispatchThread.run(Unknown Source)
# 1 楼答案
你提供的代码很难解决问题。你能提供整个课程以及你遇到的错误吗?此外,您似乎还在if语句中使用赋值运算符:
if语句的正确形式为:
或者:
您似乎将Scanner赋值给int变量。cNumber包含你的下一个标记,所以我想你要做的是
万一你有
它必须对actionPerformed()可见,然后可以执行以下操作:
所以试着写下这样的话:
更新尝试创建类似的代码:很抱歉,您的代码很难理解,因为我没有其他组件,比如它继承的类,所以我创建的是您可能需要的简单版本,很抱歉,我有点赶时间,所以如果您有任何问题,我无法很好地进行注释!基本上它能满足你的需求,只要确保你有顾客。txt与src文件夹位于同一文件夹中
这就是: