Results 1 to 22 of 22

Thread: [RESOLVED] Error

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2021
    Posts
    7

    Resolved [RESOLVED] Error

    Hi,

    I have a problem with syntax....


    Code:
    Dim _i As Integer = 1
    Controls("TextBox" & _i).Text = (ListBox1.Items(_i).ToString) '     this will not work.
    TextBox1.Text = (ListBox1.Items(_i + 5).ToString) '       this works.
    Please Help

    The error I get :An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe

    System.NullReferenceException was unhandled
    HResult=-2147467261
    Message=De objectverwijzing is niet op een exemplaar van een object ingesteld.
    Source=WindowsApplication1
    StackTrace:
    bij WindowsApplication1.Form1.Form1_Load(Object sender, EventArgs e) in Z:\WindowsApplication1\Form1.vb:Line 35
    bij System.EventHandler.Invoke(Object sender, EventArgs e)
    bij System.Windows.Forms.Form.OnLoad(EventArgs e)
    bij System.Windows.Forms.Form.OnCreateControl()
    bij System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
    bij System.Windows.Forms.Control.CreateControl()
    bij System.Windows.Forms.Control.WmShowWindow(Message& m)
    bij System.Windows.Forms.Control.WndProc(Message& m)
    bij System.Windows.Forms.ScrollableControl.WndProc(Message& m)
    bij System.Windows.Forms.Form.WmShowWindow(Message& m)
    bij System.Windows.Forms.Form.WndProc(Message& m)
    bij System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    bij System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    bij System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    InnerException:


    With this line 35 : Controls("TextBox" & _i).Text = (ListBox1.Items(_i).ToString)

    Thanks

    Regards
    Last edited by Lieven; Feb 27th, 2021 at 01:40 PM.

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: Error

    You need to tell us what the error is.

    But this works,

    Code:
            Dim _i As Integer = 1
            Controls("TextBox" & _i).Text = ListBox1.Items(_i).ToString
    I can't tell but is there a space between "&" and "_i"

  3. #3
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Error

    Are you allowed to start a variable name with a _ ??

    Don't you have to cast the integer value to a string before concatenating it with the string?

  4. #4
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Error

    Quote Originally Posted by wes4dbt View Post
    I can't tell but is there a space between "&" and "_i"
    Copy and paste it, then check the pasted string... yes there is a space.

    I don't understand why just removing those parenthesises would resolve the error ?
    And as Arnoutdv says... I'd've thought: -
    Code:
    Controls("TextBox" & _i.ToString).Text = ListBox1.Items(_i)
    I wouldn't've changed the ListBox1 item to a string... It's surly already a string.

    I don't like the '_i' although it does seem to work. What's wrong with just 'i', most folks seem to use it, I always assumed it was a form of shorthand for 'Index'.


    Poppa
    Last edited by Poppa Mintin; Feb 26th, 2021 at 05:47 PM.
    Along with the sunshine there has to be a little rain sometime.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Error

    Code:
    Dim _i As Integer = 1
    DirectCast(Controls("TextBox" & _i), TextBox).Text = ListBox1.Items(_i).ToString

  6. #6
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: Error

    Quote Originally Posted by Arnoutdv View Post
    Are you allowed to start a variable name with a _ ??

    Don't you have to cast the integer value to a string before concatenating it with the string?
    I believe that changed in VS 2010.

    .Paul

    As I said, my example works, don't know why you would need to use DirectCast. Maybe it depends on your project configuration???

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Error

    Without the cast, the object is type Control. I cast it as type TextBox. It’s strange that it was just brackets that caused the error...

  8. #8
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: Error

    It works with the brackets too but I left them off because they're not necessary. That's why I asked the OP to post the error.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Error

    Quote Originally Posted by .paul. View Post
    Code:
    Dim _i As Integer = 1
    DirectCast(Controls("TextBox" & _i), TextBox).Text = ListBox1.Items(_i).ToString
    Quote Originally Posted by .paul. View Post
    Without the cast, the object is type Control. I cast it as type TextBox. It’s strange that it was just brackets that caused the error...
    Text is a member of the Control class so there's no need to cast a Control reference as any other type in order to set the Text property.

  10. #10

    Thread Starter
    New Member
    Join Date
    Feb 2021
    Posts
    7

    Re: Error

    The error I get :An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe

    System.NullReferenceException was unhandled
    HResult=-2147467261
    Message=De objectverwijzing is niet op een exemplaar van een object ingesteld.
    Source=WindowsApplication1
    StackTrace:
    bij WindowsApplication1.Form1.Form1_Load(Object sender, EventArgs e) in Z:\WindowsApplication1\Form1.vb:Line 35
    bij System.EventHandler.Invoke(Object sender, EventArgs e)
    bij System.Windows.Forms.Form.OnLoad(EventArgs e)
    bij System.Windows.Forms.Form.OnCreateControl()
    bij System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
    bij System.Windows.Forms.Control.CreateControl()
    bij System.Windows.Forms.Control.WmShowWindow(Message& m)
    bij System.Windows.Forms.Control.WndProc(Message& m)
    bij System.Windows.Forms.ScrollableControl.WndProc(Message& m)
    bij System.Windows.Forms.Form.WmShowWindow(Message& m)
    bij System.Windows.Forms.Form.WndProc(Message& m)
    bij System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    bij System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    bij System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    InnerException:


    With this line 35 : Controls("TextBox" & _i).Text = (ListBox1.Items(_i).ToString)
    Last edited by Lieven; Feb 27th, 2021 at 01:40 PM.

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Error

    What is the value of _i? Don't forget your ListBox items are zero based. If, for example, you know _i to be equals to 1, do you have a TextBox1 on your form? If TextBox1 is in a Panel, or a GroupBox, you need to use the correct Parent property in your Controls code, i.e. GroupBox1.Controls(' etc

  12. #12

    Thread Starter
    New Member
    Join Date
    Feb 2021
    Posts
    7

    Re: Error

    Here is my code
    Attached Files Attached Files

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Error

    Quote Originally Posted by Lieven View Post
    Here is my code
    I told you. Either the TextBox doesn’t exist in your Form’s Controls collection, or the ListBox Item at _i doesn’t exist. It’s that simple...

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Error

    Every Control has a Parent Control, which can be a Form, a Panel, a GroupBox.
    Controls(“TextBox” & _i) looks for a Control whose parent is your Form...

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Error

    Quote Originally Posted by Lieven View Post
    Code:
    Dim _i As Integer = Nothing
    _i will not be Nothing, it will default to 0 (zero)

    Do you have a TextBox0??? No, it appears you don't

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Error

    This is why Visual Studio provides us with debugging tools. You can set a breakpoint and step through your code, line by line, examining the state of your variables as you go...
    I didn't need to run your app, to find your problem. After reading your Form1.Designer.vb file to assess your control hierarchies, it was easy to spot the error in your Form1.vb Form_Load event.

  17. #17

    Thread Starter
    New Member
    Join Date
    Feb 2021
    Posts
    7

    Re: Error

    Sorry but I tried with the value 0 before but the result was the same.

    Thanks for helping to find a solution, but I'm giving up. I'm trying a different way.
    Last edited by Lieven; Feb 27th, 2021 at 04:48 PM.

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Error

    Code:
    Controls("TextBox" & (_i + 1)).Text = (ListBox1.Items(_i).ToString)

  19. #19

    Thread Starter
    New Member
    Join Date
    Feb 2021
    Posts
    7

    Re: Error

    Thanks Paul!! This works!!! You make my day Respect. Problem solved

  20. #20
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Error

    Quote Originally Posted by Lieven View Post
    Here is my code
    Hi Lieven,

    At this point in your Form1_Load subroutine...
    Code:
       
            TextBox5.Text = (ListBox1.Items(_i + 4).ToString)
            TextBox6.Text = (ListBox1.Items(_i + 5).ToString)
            Controls("TextBox" & _i).Text = (ListBox1.Items(_i).ToString)
            'DirectCast(Controls("TextBox" & _i), TextBox).Text = (ListBox1.Items(_i + 5).ToString)
            'Controls("TextBox" & _i).Text = (ListBox1.Items(_i).ToString)
    Your variable _1 is still at Nothing, It's very unlikely that you have a TextBox which is just called TextBox0. (Zero)


    Poppa


    Oh! I don't believe posts 15 to 19 were displayed on my screen when I started to look through your code.
    Or maybe I just didn't see 'em !

    Pop
    Last edited by Poppa Mintin; Feb 27th, 2021 at 07:28 PM.
    Along with the sunshine there has to be a little rain sometime.

  21. #21
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Error

    Quote Originally Posted by Poppa Mintin View Post
    Oh! I don't believe posts 15 to 19 were displayed on my screen when I started to look through your code.
    Or maybe I just didn't see 'em !

    Pop
    Or... Maybe you did??? I'm guessing the OP didn't see them, or Post #18 wouldn't have been necessary...

  22. #22
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Error

    Hi Lieven,

    Good to know your problem is fixed.

    If you go to the top of this page, you will find a tab 'Thread Tools', click that and you will find how to mark this thread as 'Resolved'.

    We do this when a problem is solved so that anyone with a similar problem will know that they may find a possible answer here.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

Posting Permissions

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



Click Here to Expand Forum to Full Width