Results 1 to 6 of 6

Thread: [RESOLVED] Button2 not referenced !

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    2,066

    Resolved [RESOLVED] Button2 not referenced !

    Hi,

    Form1 has two buttons, Button1 and Button2, and a label, Label1. They're all stilled called by their generated names.

    There's not much code and yet starting the app. I get an error... What on earth is going on ?

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     ReadOnly yb As Integer = Button2.Location.Y
    4.  
    5.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    6.         Label1.Text = Nothing
    7.     End Sub
    8.  
    9.  
    10.     Private Sub AnimateBack() Handles Button2.MouseUp
    11.         With Button2
    12.             .Location = New Point(.Location.X, yb)
    13.             .Height = 38
    14.         End With
    15.     End Sub
    16.  
    17.     Private Sub AnimateDown() Handles Button2.MouseDown
    18.         With Button2
    19.             .Location = New Point(.Location.X, yb + 10)
    20.             .Height = 28
    21.         End With
    22.     End Sub
    23.  
    24.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    25.         Me.Close()
    26.     End Sub
    27. End Class
    Error !
    System.NullReferenceException
    HResult=0x80004003
    Message=Object reference not set to an instance of an object.
    Source=Zing
    StackTrace:
    at Zing.Form1..ctor() in D:\~VB Trials\Zing\Form1.vb:line 3
    How can Button2 not be referenced ?


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

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,657

    Re: Button2 not referenced !

    You can't reference controls before they are created, and the declarations section actually runs before the controls are created.

    Set the value of yb in the Load event instead.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,967

    Re: Button2 not referenced !

    Code:
    ReadOnly yb As Integer = Button2.Location.Y
    As si_the_geek says, at that point in your app. Button2 hasn't been declared.
    If you want a readonly value at Form scope...

    Code:
    Const yb As Integer = 12

  4. #4

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    2,066

    Re: Button2 not referenced !

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

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,891

    Re: Button2 not referenced !

    Quote Originally Posted by .paul. View Post
    As si_the_geek says, at that point in your app. Button2 hasn't been declared.
    That's not really true. Button2 is a variable and it certainly has been declared at that point. The actual problem is that the Button object has not been created and assigned to that variable at that point, so the variable is Nothing.

  6. #6

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Skunthorpe, North Lincolnshire, England.
    Posts
    2,066

    Re: [RESOLVED] Button2 not referenced !

    Thanks John,

    That sounds much more plausible.
    I thought it was odd when I asked the question, I couldn't understand why it wasn't declared, after all... It had already generated Mouse Up and Mouse Down methods.

    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