• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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

 
Ranch Hand
Posts: 432
  • Mark post as helpful
  • send pies
  • 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.
 
Marshal
Posts: 67164
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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: 5046
134
  • Mark post as helpful
  • send pies
  • 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: 3757
40
  • Mark post as helpful
  • send pies
  • 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: 61763
193
  • Mark post as helpful
  • send pies
  • 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: 5046
134
  • Mark post as helpful
  • send pies
  • 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: 432
  • Mark post as helpful
  • send pies
  • 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: 3757
40
  • Mark post as helpful
  • send pies
  • 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: 432
  • Mark post as helpful
  • send pies
  • 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: 3757
40
  • Mark post as helpful
  • send pies
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!