File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Initialization problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Initialization problem" Watch "Initialization problem" New topic

Initialization problem

Anna Jean

Joined: Dec 23, 2003
Posts: 8
Hello everyone and Happy New Year!
I am running a test servlet that is reading a radio button field within a jsp to determine what radio button was selected. Based on that, it will create a new entity object. For example, in the jsp:
<INPUT TYPE="Radio" NAME="car" VALUE="1" >
<INPUT TYPE="Radio" NAME="car" VALUE="2" >
From the Servlet:
if (req.getParameter("car") == "1")
Entity1 anEntity = new Entity1();
}else if (req.getParameter("car") == "2"){
Entity2 anEntity = new Entity2();
The problem is that based on the code above the other fields in my servlet that are using "anEntity" are getting a compilation error that "myEntity cannot be resolved" as though they are not being initialized. For example:
String userId = req.getParameter("UserId") ;
Can anyone help me understand why this code is not valid?
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
That's not an initialization problem. It sounds like you haven't imported Entity1 and Entity2 or perhaps you haven't defined them yet.

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33129

Assuming you have imported Entity1 and Entity2, there are two problems I see.
1) anEntity is declared within a very small scope. (scope is the area where the variable can be referenced.) Since you declare anEntity within an if statement, the variable can only be used until the } of that if statement. Then, when the compiler gets to setting the userid, anEntity can no longer be referenced.
2) You need to use .equals() instead of == when comparing strings. For example:

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Anna Jean

Joined: Dec 23, 2003
Posts: 8
Michael and Jeanne,
Thank you for your responses. Jeanne, You were right, my problem was with the scope. I will try to find another way around declaring it within the if statement. Unfortunately, all the other fields are bases on which entity is initialized.Both entities have similiar fields for example:
My entities are declared (within the if statement) as Entity1 anEntity and Entity2 anEntity, so either way, anEntity would work.Can you think of another was to initialize the entity, keeping in mind that the entity should be initialized based on the radio button selected in the jsp.
Any assistance is greatly appreciated.
I agree. Here's the link:
subject: Initialization problem
It's not a secret anymore!