Results 1 to 13 of 13
  1. #1
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    65
    Rep Power
    0

    Default Error Handling / Validation in GUI

    I have a GUI that prompts the user for information. When they click 'submit', a new customer gets created from a class like so ...

    Java Code:
    		public void actionPerformed(ActionEvent arg0) {
    			
    			Customer customer = new Customer(); // New customer object
    			customer.name = view.getName();
    			customer.age = Integer.parseInt(view.getAge());
    			customer.ccn = view.getCCNumber();
    I am having difficulties validating my variables for nulls. For instance ...

    Java Code:
    		// METHOD TO RETRIEVE TEXTBOX INPUT -- NAME
    	public String getName() {
    		
    		String name = null;
    		
    		if (jtfFirstName.getText() == null || jtfLastName.getText() == null || !jtfFirstName.getText().matches("[a-zA-Z]+") || jtfLastName.getText().matches("[a-zA-Z]+")){ 	// Validate name fields
    			JOptionPane.showMessageDialog(null, "<html><i>Improper Input Detected.</i>\n\nName fields must only contain alphabetic characters.", "ERROR!", 2);
    			jtfFirstName.requestFocus();
    		}
    		
    		else
    			name = jtfFirstName.getText() + " " + jtfLastName;
    		
    		return name;
    		
    	} // End of Name Validation
    I can't seem to win with this. Whether the fields are filled in properly or not, I get the error message and the program continues to completion using the name "null".

    Hopefully that makes sense, I'm lost. A while loop does not work in this situation because it does not allow you the time to change your input fields.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Error Handling / Validation in GUI

    I don't think the getText() method will return null. Have you tested it and found that it returns a null? I think it will return an empty String if there was nothing entered in the text field.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    65
    Rep Power
    0

    Default Re: Error Handling / Validation in GUI

    Okay. But then this is not accepted ... (it wants me to throw a 'return null' at the end).

    Java Code:
    	// METHOD TO RETRIEVE TEXTBOX INPUT -- NAME
    	public String getName() {
    		
    		if (!jtfFirstName.getText().matches("[a-zA-Z]+") || jtfLastName.getText().matches("[a-zA-Z]+")){ 	// Validate name fields
    			JOptionPane.showMessageDialog(null, "<html><i>Improper Input Detected.</i>\n\nName fields must only contain alphabetic characters.", "ERROR!", 2);
    			jtfFirstName.requestFocus();
    		}
    		else
    		return jtfFirstName.getText() + " " + jtfLastName;
    		
    	} // End of Name Validation
    And if I remove the 'else' statement it returns whatever is entered.

    The same goes for all of my other fields. It prompts you that what you entered is invalid, although it still gets assigned to the variables and therefore to the Customer class.

    You can't use a 'while' loop like this in a GUI. So how do I ensure that all required fields are filled in properly?
    Last edited by mallorz; 02-10-2014 at 01:34 AM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Error Handling / Validation in GUI

    it wants me to throw a 'return null' at the end).
    The getName() method requires that a String (or null) be returned.

    do I ensure that all required fields are filled in properly?
    Test them and issue an error message and remember that there was an error to keep the program execution in the "get input" area.

    begin loop to validate input
    test all input
    if no errors exit the loop
    end loop
    Last edited by Norm; 02-10-2014 at 01:54 AM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    65
    Rep Power
    0

    Default Re: Error Handling / Validation in GUI

    Yes, but how do I ensure that a 'null' value does not go through?

    In standard code, I would use a While loop. This does not work in this application because the user is never given a chance to correct themselves. It creates an infinite loop.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Error Handling / Validation in GUI

    It creates an infinite loop.
    Use a for() loop instead of a while() to limit the iterations of bad user input. Or while(cnt > 0) and decrement cnt every time.

    user is never given a chance to correct themselves
    The program should give the user a few chances to correct themselves.


    how do I ensure that a 'null' value does not go through?
    Test for it and don't exit the "get input" loop until valid input or the trials count is exceeded.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    65
    Rep Power
    0

    Default Re: Error Handling / Validation in GUI

    Java Code:
    	public String getName() {
    		
    		for (int x = 0; x < 10; x++)
    		{
    		while (!jtfFirstName.getText().matches("[a-zA-Z]+") || !jtfLastName.getText().matches("[a-zA-Z]+")){ 	// Validate name fields
    			JOptionPane.showMessageDialog(null, "<html><i>Improper Input Detected.</i>\n\nName fields must only contain alphabetic characters.", "ERROR!", 2);
    			jtfFirstName.requestFocus();
    		}
    		}
    		
    		return jtfFirstName.getText() + " " + jtfLastName;
    		
    	} // End of Name Validation
    = infinite loop.

    Or did I misunderstand?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Error Handling / Validation in GUI

    Why use a while statement where an if statement should be used?

    You need to describe what the whole program does. Can you list the steps it takes to do its job?

    For example:
    display GUI
    wait for user to enter data and press Submit button
    test data
    if data is OK, continue to the next step
    if data is bad, continue waiting for good user input
    Last edited by Norm; 02-10-2014 at 02:30 AM.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    65
    Rep Power
    0

    Default Re: Error Handling / Validation in GUI

    Quote Originally Posted by Norm View Post
    Why use a while statement where an if statement should be used?
    I changed it to an 'if' and it is the same results - still an infinite loop (in a sense, to whatever 'x' is assigned, does not allow for editing) and one the loop completes the rest of the code processes.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Error Handling / Validation in GUI

    You need to describe what the whole program does. Can you list the steps it takes to do its job?

    For example:
    display GUI
    wait for user to enter data and press Submit button
    test data
    if data is OK, continue to the next step
    if data is bad, continue waiting for good user input


    Where is the infinite loop? All loops can be coded to have an exit after some count.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    65
    Rep Power
    0

    Default Re: Error Handling / Validation in GUI

    1. Display GUI
    2. User enters data and clicks 'Submit' or 'Cancel'
    3. Cancel exits program, Submit calls new instance of the Customer class
    4. Values from textfields are to be validated and assigned to objects within the Customer class
    5. Car class is called referencing information from the Customer class
    6. Car variables are determined based on customer
    7. Method is called to display message/receipt


    Real issues begin at #4. I cannot seem to debug, it says 'Source not Found'. A Google search on that says something about the JDK, but I installed the latest about a month ago - 1.7 I believe.

    Edit: The loops mentioned above were in the getName(), getAge() ... methods when assigning them to the customer.

    i.e.
    customer.name = getName();
    customer.age = getAge();
    ....

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Error Handling / Validation in GUI

    The program should stay in the first part of step 4 until there is good input or the trial count is exceeded.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    65
    Rep Power
    0

    Default Re: Error Handling / Validation in GUI

    Quote Originally Posted by Norm View Post
    The program should stay in the first part of step 4 until there is good input or the trial count is exceeded.
    Thanks, Norm. That actually give me an idea! Too bad I'm at work now and won't be able to touch it for about 10 hours ...

Similar Threads

  1. Input Validation Error
    By donewithmytime in forum Advanced Java
    Replies: 1
    Last Post: 01-29-2012, 02:02 PM
  2. how to give error message after validation of username and password
    By trupesh1991 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-17-2012, 11:54 AM
  3. CRUD Validation Error: Value is not valid
    By jambon in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 06-30-2010, 08:34 PM
  4. JSP validation error
    By sysout in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 11-07-2009, 06:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •