• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
tangara goh
Ranch Hand
Posts: 276
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66261
151
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?
 
Tim Moores
Saloon Keeper
Posts: 3960
94
  • 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
 
Dave Tolls
Ranch Foreman
Posts: 3003
37
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56221
171
  • 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: 3960
94
  • 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: 276
  • 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
Ranch Foreman
Posts: 3003
37
  • 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: 276
  • 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
Ranch Foreman
Posts: 3003
37
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!