Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about exceptions.

 
Joseph Tulowiecki
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20763
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic