gpt4 book ai didi

Java,JTextField .equals

转载 作者:行者123 更新时间:2023-11-30 07:01:32 25 4
gpt4 key购买 nike

我正在尝试比较 JTextField 中的值作为验证部分,以确保如果所有 TextField 都没有值,用户无法保存进度。

这是我的文本字段方法。

public void DrawBookAttributes() {
lblBookName = new JLabel("Book Name :");
lblBookName.setFont(getLblFont());
lblBookName.setSize(140, 20);
lblBookName.setLocation(280, 150);
menuFrame.add(lblBookName);

tfBookName = new JTextField();
tfBookName.setSize(140, 20);
tfBookName.setLocation(420, 150);
menuFrame.add(tfBookName);

lblBookID = new JLabel("Book ID :");
lblBookID.setFont(getLblFont());
lblBookID.setSize(140, 20);
lblBookID.setLocation(280, 50);
menuFrame.add(lblBookID);

tfBookID = new JTextField();
tfBookID.setSize(140, 20);
tfBookID.setLocation(420, 50);
tfBookID.setText("hello");
tfBookID.setEditable(false);
menuFrame.add(tfBookID);

JLabel lblBookCost = new JLabel("Cost (£) :");
lblBookCost.setFont(getLblFont());
lblBookCost.setSize(140, 20);
lblBookCost.setLocation(280, 200);
menuFrame.add(lblBookCost);

tfBookCost = new JTextField();
tfBookCost.setSize(140, 20);
tfBookCost.setLocation(420, 200);
tfBookCost.addKeyListener(this);
menuFrame.add(tfBookCost);

lblBookYearOfPublication = new JLabel("Publication Date :");
lblBookYearOfPublication.setSize(140, 20);
lblBookYearOfPublication.setLocation(280, 250);
lblBookYearOfPublication.setFont(getLblFont());
menuFrame.add(lblBookYearOfPublication);

JTextField tfBookYearOfPublication = new JTextField();
tfBookYearOfPublication.setSize(140, 20);
tfBookYearOfPublication.setLocation(420, 250);
tfBookYearOfPublication.setText("DD-MM-YYYY");
tfBookYearOfPublication.addActionListener(this);
menuFrame.add(tfBookYearOfPublication);

JLabel lblbookPublishngHouse = new JLabel("Publishing Orgaisation :");
lblbookPublishngHouse.setSize(140, 20);
lblbookPublishngHouse.setLocation(280, 300);
lblbookPublishngHouse.setFont(getLblFont());
menuFrame.add(lblbookPublishngHouse);

JTextField tfBookPublishingHouse = new JTextField();
tfBookPublishingHouse.setSize(140, 20);
tfBookPublishingHouse.setLocation(420, 300);
menuFrame.add(tfBookPublishingHouse);

JLabel lblBookISBN = new JLabel("ISBN :");
lblBookISBN.setSize(140, 20);
lblBookISBN.setLocation(280, 350);
lblBookISBN.setFont(getLblFont());
menuFrame.add(lblBookISBN);

JTextField tfBookISBN = new JTextField();
tfBookISBN.setSize(140, 20);
tfBookISBN.setLocation(420, 350);
menuFrame.add(tfBookISBN);

JLabel lblBookPages = new JLabel("Number of Pages :");
lblBookPages.setSize(140, 20);
lblBookPages.setLocation(280, 400);
lblBookPages.setFont(getLblFont());
menuFrame.add(lblBookPages);

JTextField tfBookPages = new JTextField();
tfBookPages.setSize(140, 20);
tfBookPages.setLocation(420, 400);
menuFrame.add(tfBookPages);

menuFrame.setVisible(true);
}

这是验证部分

    public void EntryCheck() {

if (tfBookName.getText().equals("")) {
EntryChecker++;
System.out.println(EntryChecker);
}
if (tfBookISBN.getText().equals("")) {
EntryChecker++;
}
if (tfBookCost.getText().equals("")) {
EntryChecker++;
}
if (tfBookPages.getText().equals("")) {
EntryChecker++;
}
if (tfBookPublishingHouse.getText().equals("")) {
EntryChecker++;
}
if (tfBookYearOfPublication.getText().equals("")) {
EntryChecker++;
}
if (tfBookCost.getText().equals("DD-MM-YYY")) {
EntryChecker++;
}
System.out.println(EntryChecker);
}

EntryChecker 是一个整数,每次用户离开 TextField 而不在其中输入信息时,我都会向它添加一个值。

每次调试程序时,我都会在此验证行中遇到 NullPointerException。

if (tfBookISBN.getText().equals("")) {
EntryChecker++;

有人知道问题出在哪里以及如何解决吗?

谢谢!

最佳答案

如果-正如您在评论中所说-您有

private JTextField tfBookISBN = new JTextField();

类中的全局范围,那么你必须删除该行

JTextField tfBookISBN = new JTextField();

来自 DrawBookAttributes()

如果它在声明时未初始化(我认为它没有),则需要将该行保留在 DrawBookAttributes()但是作为初始化,而不是声明,如下:

tfBookISBN = new JTextField();

因为您正在方法中创建一个新字段,并且未初始化全局字段,这会在 EntryCheck() 时创建 NPE尝试访问该对象以调用 getText() .

关于Java,JTextField .equals,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40843759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com