File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initialization problem

 
Anna Jean
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not an initialization problem. It sounds like you haven't imported Entity1 and Entity2 or perhaps you haven't defined them yet.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33691
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Anna Jean
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic