• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question about exceptions.

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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); ?
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic