aspose file tools*
The moose likes Beginning Java and the fly likes question about exceptions. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question about exceptions." Watch "question about exceptions." New topic
Author

question about exceptions.

Joseph Tulowiecki
Greenhorn

Joined: Jun 10, 2011
Posts: 25
I have a method

public void setSalary(int salary) {
this.salary = salary;
}

//The method above is called from the method below. They are in two seperate classes. I want to display an error message if salaryTxt is not an integer.
// However I don't know how to do this because I cant display the string to the screen from the method above. I need to do it from the method below. The method above does not
//have access to the label I want to print the error message out on.

private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {
Employee e = new Employee();

e.setSalary(Integer.parseInt(salaryTxt.getText()));
}


Any suggestions appreciated.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18116
    
    8

Joseph Tulowiecki wrote:
// However I don't know how to do this because I cant display the string to the screen from the method above. I need to do it from the method below. The method above does not
//have access to the label I want to print the error message out on.


Then you should change your code so that it does have a reference to that label.
Joseph Tulowiecki
Greenhorn

Joined: Jun 10, 2011
Posts: 25
The only way to do this is to reorganize? sorry im quite new. i guess maybe make a parent class and just parent.errorLabel.setText(error); ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
What has parent got to do with it? You can pass a reference to the label. Then your method can read the text from the label, and reset the text as appropriate.
But there are lots of different ways to achieve this. Throwing a NumberFormatException is probably better. Then you can test for it with a try-catch. You can even put the try-catch into a loop, so the user has to re-enter the text until it is a valid number.
Joseph Tulowiecki
Greenhorn

Joined: Jun 10, 2011
Posts: 25
Ok it appears as if I'm a moron. The method below does have access and that is where the exception would occur. Wow too many hours of programming i think :[ Thanks everyone.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
19 lines, of which 8 have no writing on.

You would usually fill in the salary in a text field or similar, not a label. You would presumably have some sort of reference to the object on which you wish to set the salary.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about exceptions.
 
Similar Threads
Class declaration problem.
Encapsulation ?
how comparator works?
Query -- select maximum salary in a sum table
creating object using newInstance() method