• 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

why am I getting age cannot be resolved to a variable ?

 
Ranch Hand
Posts: 1021
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi expert,

I am stumped by this error :

age cannot be resolved to a variable



I have everything correctly done so I am not sure why I am getting this error in my servlet :




My getter and setter


Hope someone can help me.  Tks.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Getters and setters indicate that your servlet class has instance methods. It should not! Servlets are shared across threads and must be thread safe. That means (for one thing), no read/write instance variables.

Perhaps you could show us the entire code of the servlet?
 
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because it is defined inside the "try { ... }" block, and not visible outside of it (variables are visible inside the block of code where they're declared, but not outside of it).

Read https://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html to learn about blocks
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Tim says, age is not visible outside the try/catch block.

You need to decide what you want to do if the user enters an invalid age.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would also suggest that catching number format exceptions is not a good way to ensure you get a valid number. You need a loop to ensure you get a valid number, and I would suggest you use the hasNextInt or similar method of Scanner to verify that input.
 
Tim Moores
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You need a loop to ensure you get a valid number, and I would suggest you use the hasNextInt or similar method of Scanner to verify that input.


As this code is in a servlet, it can't work quite like that. But yes, processing can't continue if a required input is invalid.
 
tangara goh
Ranch Hand
Posts: 1021
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

Thanks for all your advice.

Apparently, there is something wrong with Eclipse cos now it is working without any error.  And I had removed the try catch... I was following advice from stackoverflow but it did not work out.  Eclipse is the main culprit.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I doubt Eclipse was the culprit for the error in your original post.

If you remove the try/catch then yes, age will be visible to the later line where you use it to construct a Tutor...that's not Eclipse having a problem, though.
 
tangara goh
Ranch Hand
Posts: 1021
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:I doubt Eclipse was the culprit for the error in your original post.

If you remove the try/catch then yes, age will be visible to the later line where you use it to construct a Tutor...that's not Eclipse having a problem, though.



Hi Dave,

Before I had try/catch, I was getting the same error.

I then used the advice from stackoverflow to have try/catch.

And now I re-start my Eclipse, it was ok.

 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect there was something else going on.

Eclipse is pretty solid on this sort of thing.

But since you've probably not got the exact code it was complaining about anymore, it's a bit moot.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic