Confused - control not set to instance inside foreach loop - please help

Discussion in 'ASP .Net' started by Alan Silver, Nov 7, 2005.

  1. Alan Silver

    Alan Silver Guest

    Hello,

    I have code like the following...

    foreach (Control ctl in Page.Controls) {
    if (ctl.ID.StartsWith("X_")) {
    // do stuff
    }
    }

    but this gives a run-time error on the second line of "Object reference
    not set to an instance of an object" which confuses me.

    Surely the foreach loop should ensure that ctl is always set to an
    object?

    The ID property is a string, so that shouldn't be causing the problem.
    If the control doesn't have an ID (if that's possible), the ID should be
    "" and the StartsWith() method should return false.

    <pause>
    I just found something even more weird!! I changed the code to...

    foreach (Control ctl in Page.Controls) {
    string ctlId = ctl.ID;
    Trace.Warn("ctlId = @" + ctlId + "@");
    if (ctlId != "") {
    if (ctlId.StartsWith("X_")) { // RUN-TIME ERROR HERE
    // do stuff
    }
    }
    }

    and it bombed out on the same line, even though the tracing shows that
    the ID was "". I don't understand how it *got* to that line when the
    previous lines checks if the ID is "". Either I've done something
    blindingly stupid here (not unlikely!!), or something very weird is
    happening.

    So, anyone able to explain to me what's going on here? TIA.

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
     
    Alan Silver, Nov 7, 2005
    #1
    1. Advertising

  2. I believe you have at least one Control with a null id. Unfortunately, it is
    not an error to concatenate a string with a null string. So:

    Trace.Warn("ctlId = @" + ctlId + "@");

    will not throw an exception. Neither will:

    if (ctlId != "")

    If ctlId is null, it will evaluate to true (ctlId is not equal to an empty
    string).

    Also, you should be awaare that the Page.Controls Collection is only a
    Collection of those Controls immediately under the Page. You would need a
    recursive function to get at all the nested Controls in the Page.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    A watched clock never boils.

    "Alan Silver" <> wrote in message
    news:...
    > Hello,
    >
    > I have code like the following...
    >
    > foreach (Control ctl in Page.Controls) {
    > if (ctl.ID.StartsWith("X_")) {
    > // do stuff
    > }
    > }
    >
    > but this gives a run-time error on the second line of "Object reference
    > not set to an instance of an object" which confuses me.
    >
    > Surely the foreach loop should ensure that ctl is always set to an
    > object?
    >
    > The ID property is a string, so that shouldn't be causing the problem.
    > If the control doesn't have an ID (if that's possible), the ID should be
    > "" and the StartsWith() method should return false.
    >
    > <pause>
    > I just found something even more weird!! I changed the code to...
    >
    > foreach (Control ctl in Page.Controls) {
    > string ctlId = ctl.ID;
    > Trace.Warn("ctlId = @" + ctlId + "@");
    > if (ctlId != "") {
    > if (ctlId.StartsWith("X_")) { // RUN-TIME ERROR HERE
    > // do stuff
    > }
    > }
    > }
    >
    > and it bombed out on the same line, even though the tracing shows that
    > the ID was "". I don't understand how it *got* to that line when the
    > previous lines checks if the ID is "". Either I've done something
    > blindingly stupid here (not unlikely!!), or something very weird is
    > happening.
    >
    > So, anyone able to explain to me what's going on here? TIA.
    >
    > --
    > Alan Silver
    > (anything added below this line is nothing to do with me)
     
    Kevin Spencer, Nov 7, 2005
    #2
    1. Advertising

  3. Alan Silver

    Alan Silver Guest

    >I believe you have at least one Control with a null id.

    How can the ID be null? If the control itself is not null, and the ID is
    a string (according to the SDK), then surely the ID should give you an
    empty string. Please explain how it can be null.

    I changed my code to...

    if ((ctlId != null) && (ctlId.StartsWith("X_"))) {

    .... and it works fine now. So you were absolutely correct, although I
    still don't really understand how the situation could arise.

    > Unfortunately, it is
    >not an error to concatenate a string with a null string. So:
    >
    >Trace.Warn("ctlId = @" + ctlId + "@");
    >
    >will not throw an exception. Neither will:
    >
    >if (ctlId != "")
    >
    >If ctlId is null, it will evaluate to true (ctlId is not equal to an empty
    >string).


    Hmm, that's really stupid. Still forewarned is forearmed as they say ;-)

    >Also, you should be awaare that the Page.Controls Collection is only a
    >Collection of those Controls immediately under the Page. You would need a
    >recursive function to get at all the nested Controls in the Page.


    Ha, here's me thinking that it's not an issue in this case, and feeling
    smug that I didn't make *that* mistake, when I realised that the
    controls I wanted were all inside a placeholder, and so didn't show up
    in the loop I coded. I needed to loop over the placeholder's control
    collection.

    Ho hum. Thanks for the warning there, you saved me hours of debugging!!

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
     
    Alan Silver, Nov 7, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    16
    Views:
    1,070
  2. Keith Chadwick

    PLEASE HELP - object reference not set to instance of an object

    Keith Chadwick, Nov 29, 2004, in forum: ASP .Net Web Services
    Replies:
    11
    Views:
    288
    Keith Chadwick
    Dec 1, 2004
  3. Vinod. K
    Replies:
    4
    Views:
    107
    Anno Siegel
    Aug 25, 2003
  4. Gavin Sherlock
    Replies:
    1
    Views:
    107
    Uri Guttman
    Dec 10, 2003
  5. Isaac Won
    Replies:
    9
    Views:
    456
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page