Java,JTextField .equals

时间:2016-11-28 11:58:54

标签: java swing user-interface jtextfield

我正在尝试将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++; 

任何人都有想法问题在哪里以及如何解决?

谢谢!

1 个答案:

答案 0 :(得分:1)

如果你在评论中说 - 你有

private JTextField tfBookISBN = new JTextField();

您班级的全球范围,然后您必须删除该行

JTextField tfBookISBN = new JTextField();

来自DrawBookAttributes()

如果在声明时未初始化(我认为不是),您需要将该行保留在DrawBookAttributes() 作为初始化,而不是声明,如下所示:

tfBookISBN = new JTextField();

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