aspose file tools*
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
Author

Initialization problem

Anna Jean
Greenhorn

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:
<CODE>
<INPUT TYPE="Radio" NAME="car" VALUE="1" >
<INPUT TYPE="Radio" NAME="car" VALUE="2" >
</CODE>
From the Servlet:
<CODE>
if (req.getParameter("car") == "1")
{
Entity1 anEntity = new Entity1();
}else if (req.getParameter("car") == "2"){
Entity2 anEntity = new Entity2();
}
</CODE>
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:
<CODE>
String userId = req.getParameter("UserId") ;
anEntity.setUserId(userId);
</CODE>
Can anyone help me understand why this code is not valid?
Thanks,
Anna
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
Marshal

Joined: May 26, 2003
Posts: 30957
    
158

Anna,
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:


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Anna Jean
Greenhorn

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:
anEntity.getTime()
anEntity.getLocation()
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.
Thanks,
Anna
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Initialization problem