Search code examples
javaswinguser-interfacejtextfield

Java, JTextField .equals


I am trying to compare value in JTextField for a validation part, to make sure user can not save progress if all TextFields don't have a value.

This Is my Method for Text Fields.

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);
}

And This is Validation part

    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 is an Integer, I am Adding a value to it each time when user left TextField without Entering information in it.

Every time I debug the program, I am facing NullPointerException in this Validation Line.

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

Any one have Idea where is the problem and how to fix it?

Thank you!


Solution

  • If -as you sayin comments- you have

    private JTextField tfBookISBN = new JTextField();

    global scope in your class, then you have to remove that line

    JTextField tfBookISBN = new JTextField();

    from DrawBookAttributes()

    in case it's not initialized (i think its not) at declaration, you need to keep that line in DrawBookAttributes() but as initialize, not declare, as below:

    tfBookISBN = new JTextField();

    because you are creating a new field in the method, and leaving the global one uninitialized which creates a NPE when EntryCheck() tries to access that object to invoke getText().