2016-11-28 32 views
1

Ich versuche, den Wert in JTextField für einen Validierungsteil zu vergleichen, um sicherzustellen, dass der Benutzer den Fortschritt nicht speichern kann, wenn alle TextFields keinen Wert haben.Java, JTextField .equals

Dies ist meine Methode für Textfelder.

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

Und Dies ist Validation Teil

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 eine ganze Zahl, ich Wert es jedes Mal, wenn der Benutzer Hinzufügen TextField- links ohne Information in es eingeben.

Jedes Mal, wenn ich das Programm debugge, stehe ich in dieser Validierungszeile vor NullPointerException.

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

Jede hat Idee, wo das Problem ist und wie man es beheben?

Vielen Dank!

+0

Sind Sie sicher, dass die 'tfBookISBN' in der if-Anweisung die gleiche Objektreferenz ist, wenn das Feld erstellt wird? Ich meine, was sind die Bereiche des obigen Codes, haben Sie eine globale TfBookISBN? und ein anderer in einer Methode? d. h. poste mehr Code :) – Yazan

+0

@Yazan tfBookISBN ist global und privat, ich deklariere es in einer Methode und zugreife es in einer anderen in der gleichen Klasse. – Helvijs

+0

@Yazan Hinzugefügt Alle Codes von beiden Methoden Wie Sie angefordert haben. – Helvijs

Antwort

1

Wenn -wie Sie sayin Kommentare- Sie haben

private JTextField tfBookISBN = new JTextField();

globale Reichweite in Ihrer Klasse, dann müssen Sie diese Zeile entfernen

JTextField tfBookISBN = new JTextField();

von DrawBookAttributes()

falls es bei der Deklaration nicht initialisiert wird (ich denke es ist nicht), müssen Sie es behalten diese Linie in DrawBookAttributes()aber als initialize, erklärt nicht, wie unten:

tfBookISBN = new JTextField();

, weil Sie ein neues Feld in dem Verfahren zu schaffen, und die globalen einen nicht initialisierten verlassen, die eine NPE erzeugt, wenn EntryCheck() versucht, Zugriff auf das Objekt, das aufgerufen werden soll getText().