Re: I lost the point: purpose of putting objects to null?

From: AnonymousOne (nobody_at_nowhere.org)
Date: 09/23/04


Date: Thu, 23 Sep 2004 20:23:06 GMT

I personally use this to test if an object was ever set or not. If you
just check for (x == null) without actually SETTING x to null, then the
compiler would give you a "x may not be initialized" error.

Example:

java.sql.Connection c;

try {
        c = java.sql.DriverManager.connect(...);
        java.sql.Statement s = c.createStatement();
        ...
}
catch (Throwable thrown) {
        // some code here
}
finally {
        if (c == null) then c.close();
}

This example would not compile, because "c" is never initialized.
However, if we change the first line to:

java.sql.Connection c = null;

Then the finally block would work.

Dado wrote:
> I lost the point:
> What is purpose of putting objects to null:
>
> If I make a class, JFrame for example, with button which start connection
> dialog, which is class with connection and statement Object.
> And every time I click the button first I put :
> dialog = null
> dialog = new dialog()
>
> What is purpose to put dialog = null when all its objects are stay non-null?
>
>
>
>
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.725 / Virus Database: 480 - Release Date: 19.7.2004
>
>



Relevant Pages

  • Re: reading a fixed-length message
    ... EOF is generated under all circumstances in a timely fashion? ... disconnect, it doesn't close the connection, it just disappears. ... packed in big-endian order and the above code works purely by chance on ... The compiler only has to compute the expression you've specified, ...
    (comp.unix.programmer)
  • Re: what am I doing wrong ?
    ... close the connection in the finally, because we can't rely on the dispose ... The compiler uses a try..finally block when the using statement is compiled. ... OleDbConnection conn = new OleDbConnection; ... able to handle a particular exception in code, ...
    (microsoft.public.dotnet.languages.csharp)
  • ATL Compiler Error When Adding Events
    ... I have another problem in that the connection point wizard seems to be using an older version of my type library to generated the connection point. ... ATL Compiler Error When Adding Events ... interfaces and that included entries in the COM MAP for the ...
    (microsoft.public.vc.atl)
  • Re: Scope confusion -- compilation issue -- please help me understand
    ... The problem is that the compiler doesn't know that Environment.Exit never ... So if the constructor for SqlConnection threw an exception (leaving ... connection uninitialised), the compiler would consider the use of connection ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Does indemnify mean what I thinik it does
    ... officers, directors, employees, agents and representatives harmless ... your activities in connection with the Site or Site-related services. ... damages arising from any breach of the agreement by you. ... purpose is legitimate and is accomplished by the clause. ...
    (misc.legal)