Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Method Invokation

  1. #1
    Member
    Join Date
    Apr 2011
    Location
    Niperia
    Posts
    30
    My Mood
    Amazed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Method Invokation

    I need help solving this please: I have TWO methods i want to execute, but i only want the both of them to be executed if any of the methods doesn't return an error! i have tried doing this:
    addButton.addActionListener(new ActionListener(){
            	public void actionPerformed(ActionEvent ev){
            		boolean valid = true;
            		String fnField = fnameField.getText();
            		String mnField = mnameField.getText();
            		String lnField = lnameField.getText();
            		String user = usnField.getText();
            		String sx = group.getSelection().getActionCommand();
            		//
            		@SuppressWarnings("deprecation")
    				String pwd = pwdField.getText();
            		//
            		String img = imagePath.getText().trim();
            		Insert ins = new Insert();
            		int numField = 0;
            		// 
            		employee emp = new employee();
            		String errCheck = emp.err("");
            		//
            		if(fnField.equals(errCheck)){
            			valid = false;
            		}
            		if(mnField.equals(errCheck)){
            			valid = false;
            		}
            		if(lnField.equals(errCheck)){
            			valid = false;
            		}
            		if(user.equals(errCheck)){
            			valid = false;
            		}
            		if(pwd.equals(errCheck)){
            			valid = false;
            		}
            		if(isNum(numField) <= 0){
            			valid = false;
            			JOptionPane.showMessageDialog(null, "The Id field must be a"
            					+ " number greater than 0!");
            		}
            		if(img.equals(errCheck)){
            			valid = false;
            			JOptionPane.showMessageDialog(null, "Please Choose an image!");
            		}
            		if(valid){
            	 **  emp.addEmployee(isNum(numField), user, pwd, fnField, mnField, lnField, sx);
                **  ins.ImageInsert(isNum(numField), (new File(img)));
            	  }
            	}
            });
    //
    errCheck() is a method i created to check for errors in my forms! But i noticed that
    ** ins.ImageInsert(isNum(numField), (new File(img))); is executed even if there's an error with this: ** emp.addEmployee(isNum(numField), user, pwd, fnField, mnField, lnField, sx);
    //
    What can i do to prevent any of them executing if there's an error generated by at least one of them?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Method Invokation

    to prevent any of them executing if there's an error generated
    Use a boolean variable to remember if there has been an error generated. Initialize it to false, set it true if there is an error and use its value in an if statement to control whether to call the methods.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2011
    Location
    Niperia
    Posts
    30
    My Mood
    Amazed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Method Invokation

    Thanks Man. But am sure that's pretty much what i did on my code as you can see! But even at that, one of the method tends to execute despite the fact that the other one returned an error. Maybe am doing something wrongly!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Method Invokation

    one of the method tends to execute despite the fact that the other one returned an error.
    Where does the code use the value of the boolean to control whether or not method is called?
    What method(s) calls do you want to control based on the boolean variable's value?

    What method returned the error?

    Try to be more specific: use the method names and the variable names when describing the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2011
    Location
    Niperia
    Posts
    30
    My Mood
    Amazed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Method Invokation

    Actually the code makes use of the value of the boolean within an actionPerformed method. The objective is to check if form variables are unassigned, if the are; the errCheck() method is a method that returns a message telling the user that a field has been left empty or unfilled. But if no field is empty or blank or null as the case may be, the ImageInsert() and addEmployee() of the employee class are executed! But i noticed that one of the methods e.g the ImageInsert() method is executed even if there was an error displayed from the errCheck() method. Now the addEmployee() method adds an employee records into the `employee` table of my database,while the ImageInsert() method inserts images into the `employeeImages` table of the same database. Hope you can understand me now?

    --- Update ---

    This is my errorCheck() method:

    	public String err(String e){
    		JPanel panel = new JPanel();
    		panel.setLayout(new java.awt.FlowLayout());
    		JLabel errLabel = new JLabel();
     
    		if(e.length() == 0){
    			errLabel.setText("A field is missing!");
    			errLabel.setFont(new Font("MonoSpaced", Font.BOLD+Font.ITALIC, 13));
    			errLabel.setForeground(new Color(120,100,100));
     
    			JOptionPane.showMessageDialog(panel,errLabel, "Error", JOptionPane.OK_OPTION);
    		}
    		return e;
    	}

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Method Invokation

    the ImageInsert() method is executed even if there was an error
    The imageImsert() method is called if the variable: valid has a value of true. Where is the error detected and does that code set valid false and there is no other code that sets valid true after it is set false.

    This is my errorCheck() method:
    I don't see where a method named: errorCheck() is defined. I see the definition for a method named err() and I see references to a method named errCheck() and I see a variable named: errCheck.

    It is very important when talking about code to use the correct names to avoid confusion.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2011
    Location
    Niperia
    Posts
    30
    My Mood
    Amazed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Method Invokation

    I understand what you are trying to say man, my bad! the method isn't errCheck(), the method is err(). errCheck is a variable referencing the method err(). err() is a method in a Class named employee(), i've used the emp object to call the method err(): that is; emp.err(). Where emp is an instance of the class employee.
    Sorry for the confusion - my apologies please!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Method Invokation

    How does the err() method tell its caller that it found an error so that its caller can change what other methods are called?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Apr 2011
    Location
    Niperia
    Posts
    30
    My Mood
    Amazed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Method Invokation

    Never Mind! Solved It already! Thanks Though!

Similar Threads

  1. Replies: 1
    Last Post: January 23rd, 2013, 07:29 AM
  2. [SOLVED] How to create a Java generic method, similar to a C++ template method?
    By Sharmeen in forum Object Oriented Programming
    Replies: 3
    Last Post: October 18th, 2012, 02:33 AM
  3. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM
  4. How do I call a method from the main method?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 19th, 2011, 08:37 PM
  5. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM