variable "retriever" might not have been initialized - confused  
Author Message
phillip.s.powell@gmail.com





PostPosted: 2006-1-16 18:07:00 Top

java-programmer, variable "retriever" might not have been initialized - confused [CODE]
String message = "", send = "", quit = "", errorMsg = "", cookie =
"";
boolean hasSubmittedMessage;
HTMLRetriever retriever;

// SEND MESSAGE TO SERVLET
if (hasSubmittedMessage && send.length() > 0) {
try { // USING NEW VERSION OF URLEncoder.encode() THAT REQUIRES
try{} BLOCK DUE TO NEW 2ND PARAMETER OF ENC-TYPE
retriever = new HTMLRetriever(ChatGlobals.SERVLET_SELF +
"/ppowell.ChatServlet?message=" +
URLEncoder.encode(message, "UTF-8") +
"&nickname=" +
URLEncoder.encode(cookie, "UTF-8")
); // SEND MESSAGE
} catch (UnsupportedEncodingException uee) {
errorMsg = "Error involving message submittal: " + uee.toString();
} catch (Exception e) {
errorMsg += "Unknown error: " + e.toString();
}
} else if (hasSubmittedMessage && quit.length() > 0) {
try { // USING NEW VERSION OF URLEncoder.encode() THAT REQUIRES
try{} BLOCK DUE TO NEW 2ND PARAMETER OF ENC-TYPE
retriever = new HTMLRetriever(ChatGlobals.SERVLET_SELF +
"/ppowell.ChatServlet?message=" +
URLEncoder.encode("/q", "UTF-8") +
"&nickname=" +
URLEncoder.encode(cookie, "UTF-8")
); // SEND "QUIT" COMMAND
} catch (UnsupportedEncodingException uee) {
errorMsg += "Error involving message submittal: " + uee.toString();
} catch (Exception e) {
errorMsg += "Unknown error: " + e.toString();
}
}

try {
if (retriever.getHTML() != null && quit.length() > 0)
out.println("<script type=\"text/javascript\">\n<!--
self.parent.close();\n//-->\n</script>\n");
} catch (Exception e) {} // DO NOTHING

// STUFF

[/CODE]

Produces this error:


[ERROR]
/~ppowell/includes/chat_submit_message.jsp:63: variable retriever might
not have been initialized
if (retriever.getHTML() != null && quit.length() > 0)
[/ERROR]

I can't figure out why since I'm capturing all errors because
"retriever" might not be initialized, which is the case if
hasSubmittedMessage = false. You see, if the user hasn't yet submitted
a message, nothing can be sent to the servlet, thus, the HTMLRetriever
variable "retriever" will not be initialized into a class object
because it can't unless it receives a value, which it can only do so
upon the right conditions, otherwise, no value can be passed into
"retriever".

Anyone know what I can do about this? I'm sorry but once again in PHP
this is not a problem, and I'm sorry but I have no choice but to
"translate" PHP scripts into JSP due to client dropping PHP support.

Thanx
Phil

 
Torkel Franzen





PostPosted: 2006-1-16 18:25:00 Top

java-programmer >> variable "retriever" might not have been initialized - confused "email***@***.com" <email***@***.com> writes:


> Anyone know what I can do about this?

You satisfy the compiler by changing the declaration of retriever to

HTMLRetriever retriever=null;
 
phillip.s.powell@gmail.com





PostPosted: 2006-1-16 18:45:00 Top

java-programmer >> variable "retriever" might not have been initialized - confused Good grief that was too simple, and I just didn't get it. Tack s? mycket!

Phil

Torkel Franzen wrote:
> "email***@***.com" <email***@***.com> writes:
>
>
> > Anyone know what I can do about this?
>
> You satisfy the compiler by changing the declaration of retriever to
>
> HTMLRetriever retriever=null;

 
 
Nigel Wade





PostPosted: 2006-1-18 1:01:00 Top

java-programmer >> variable "retriever" might not have been initialized - confused email***@***.com wrote:

> Good grief that was too simple, and I just didn't get it. Tack s氓
> mycket!
>
> Phil
>
> Torkel Franzen wrote:
>> "email***@***.com" <email***@***.com> writes:
>>
>>
>> > Anyone know what I can do about this?
>>
>> You satisfy the compiler by changing the declaration of retriever to
>>
>> HTMLRetriever retriever=null;

The problem is that there is a route to the code:

if (retriever.getHTML()...

where retriever has not been set to any value. The catch blocks don't assign it,
but allow execution to continue.

As Torkel says, you can satisfy the compiler by initializing retriever to null,
and the compiler error will go away. The logic is still broken, however, as
there is still a route to that piece of code where you use retriever without it
having a proper value. Now, you will get a NullPointerException if that route
is taken. The compiler error is there to help point out to you that there is
something wrong in the code. Getting rid of the compiler error message hasn't
got rid of the fundamental error in the logic.

You've replaced a compiler error with a logic bomb waiting to dump on you at
some later time, unless you catch that NullPointerException and deal with it
appropriately, or protect the retriever.getHTML() by testing for null first.

I think a better option would be to re-organize the code so that retriever is
only used within the try {} block, where it has a valid value. The code looks
like it's designed with errors signalled by return value rather than thrown
exceptions.

This would be one "exceptional" way of doing it:

try {
if (hasSubmittedMessage && send.length() > 0) {
retriever = new HTMLRetriever(...
}
else if (hasSubmittedMessage && quit.length() > 0) {
retriever = new HTMLRetriever(...
}
if (retriever.getHTML() ...
}
} catch (UnsupportedEncodingException uee) {
...

--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : email***@***.com
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555