Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2004
    Posts
    31

    Unanswered: I am really struggling with this!

    I have the following code in VB express 2005:
    Controls("giro" & i).Visible = False
    It compiles ok, but whenever I try to execute the command I get the following error:

    Nullreferenceexception was unhandled-object reference not set to an instance for an object. HELP!

    Regards

  2. #2
    Join Date
    Jul 2003
    Location
    Amsterdam, Nederland
    Posts
    450
    Seems like you have a form with more text or labels that are named giro and a number stuck to it so : giro0;giro1;giro2;giro3;giro4 ect


    so if the form is loaded it will set all the labels /text to hide (not visible)

    Set a breakpoint where the code starts with F9 (this will set a redline in your code) if you now run your code it will stop at that line
    now press F8 to navigate line for line through your code and you will see what it does

    TIP : if its stop on the line where there is an i , put your mousepointer over it and you'll see its value
    see where your error is generated
    maybe label giro4 is deleted on the form and it will give you this error

  3. #3
    Join Date
    Apr 2004
    Location
    Arizona
    Posts
    75

    Look like you command is inside a loop

    Look like you command is inside a loop which is counting for more times than you have objects for. Check you for loop, you may have to put a -1 on then end because counting functions are some times 0 based so if you have object0..object9 the count will return 10 and if you do for i = 0 to count then you will get an error because there is no object10.

  4. #4
    Join Date
    Oct 2003
    Posts
    706
    To put it another way, your code will only work if, for any 'i', there exists a control by the name generated by the expression, ("Giro" & i).

    If this expression does not produce the string-value you expect, or no such object exists... then the statement has no choice but to raise a runtime exception and it may not be a terribly readable one. In this case it definitely isn't.

    What I usually do in setting-up such code is to deliberately simplify the statement into several. I might deliberately set a temporary variable to the Controls() entry in the first statement, then use the variable to reference the control in the next statement. I deliberately use two statements to do the work of one. Why? Because it produces better error-messages.

    The language is going to compile or at least semi-compile the statements into more-or-less the same executable code no matter how (within reason...) I write it. Computers are so darned fast that a little "inefficiency" won't be noticed. But it might make the program considerably easier to debug, or to extend in the future.
    ChimneySweep(R): fast, automatic
    table repair at a click of the
    mouse! http://www.sundialservices.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •