Results 1 to 14 of 14

Thread: [RESOLVED] Object not set error

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Resolved [RESOLVED] Object not set error

    Hello,

    This should be an easy thing to do, but I can't figure out what I am doing wrong.

    I have a codejock report control on the main form that list users in a database. After a new user is added thru a different form, I want the list to update. This should be easy, but I get an "object reference not set to an object" error. The code that does the update is in a class. When the user selects the option to view the users, then report control is passed byref to the sub as a parameter. On the second form I dim frm as frmMain(my main form that holds the report control) and do frm.PEPReportControl when I call the sub. The code is below of two of the things I have tried.

    My form class is public, my class is public. I have done searching on the web, but haven't found any help. Also, I don't get any lines under the code. I have tried creating a reference to the form, and the control type, and pass that in the parameter, but that didn't work either.

    Any help would very good. Thank you.

    VB Code:
    1. Try
    2.      Dim frm As frmMain
    3.      Dim RC As AxXtremeReportControl.AxReportControl = frm.PEPReportControl
    4.      MyServerControl.AddUser(Me.UserName.Text.ToString, Me.Password.Text.ToString, Me.FName.Text.ToString, Me.LName.Text.ToString)
    5.      MyFormControl.PopulateReportControlUserInfo(RC)
    6.      frm = Nothing
    7.      Me.Hide()
    8. Catch ex As Exception
    9.      MessageBox.Show(ex.ToString)
    10. End Try

    VB Code:
    1. Try
    2.     Dim frm As frmMain
    3.     MyServerControl.AddUser(Me.UserName.Text.ToString, Me.Password.Text.ToString, Me.FName.Text.ToString, Me.LName.Text.ToString)
    4.     MyFormControl.PopulateReportControlUserInfo(frm.PEPReportControl)
    5.     frm = Nothing
    6.     Me.Hide()
    7. Catch ex As Exception
    8.     MessageBox.Show(ex.ToString)
    9. End Try

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Re: Object not set error

    Sorry, forgot to add that this is VS2003.

  3. #3
    Hyperactive Member syntaxeater's Avatar
    Join Date
    Dec 2006
    Location
    Des Moines, IA
    Posts
    460

    Re: Object not set error

    frm as new frmmain

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Object not set error

    Mmmmm 'kaaaay.... Despite the rumors to the contrary, most of us are not mind readers.... so, could you point out what line causes the error?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Re: Object not set error

    Sorry, I thought that I did a good enough job of verbally expressing that the error was caused on the line that I pass the reference to the report control on, and that populates the report control. Since that line has the word "ReportControl" and populate in it, I thought that would be enough. I am not trying to be crappy, just trying to point out that I was not expecting you to read my mind, but did make an effort to give enough information.

    I will try new in the reference to the form, but I have a question about that. Does the new create a new instance of the form, or just a new pointer to that form? That should probably be obvious, but I am new to .NET, sorry.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Object not set error

    You cannot simply pluck a reference to an existing object out of thin air. If you want to create a new instance of your form class then you use the "New" key word to do so. If you want to refer to an existing instance of that class then you need to get a reference to that existing instance. Remember, you could have thousands of existing instances of that same class, so how would the app know which one you want if you don't specify?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Object not set error

    Dim frm As New frmMain will create a new instance of frmMain, and I believe it's not what you want. If you want a reference back to the main form in the current form, you should set its Owner property to the main form. Do something like this:
    VB Code:
    1. 'In main form
    2. Dim frm2 As New Form2
    3. frm2.Owner = Me
    4. frm2.Show
    Then in the 2nd form you do this
    VB Code:
    1. Dim frm As frmMain = DirectCast(Me.Owner, frmMain)
    2. Dim RC As AxXtremeReportControl.AxReportControl = frm.PEPReportControl
    3. '... blablabla...

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Re: Object not set error

    jmcilhinney, that makes sense. I understand that, that is why I didn't try the new word. I thought that would create a new instance of the form. I want to refer to the form that is already instanced. In visual basic 6 I would just go frmMain.PEPReportControl, but that doesn't work in .NET (which I am sure you already know), and I did a search on the site and found that I have to dimension the form Dim frm as frmMain. That allows me to access controls on the form, so if I wanted to pass text from form two to form one, from what I read, that would work, so I thought that it would work in passing a reference to the control thru a parameter to the sub, but it didn't. So, I am not sure where to go from here. I tried the new word, and no errors were given, and I stepped thru the code, and it all functioned well, but it did not populate the list. This make me think that the new work created a new instance of the form, not reference the one I already have. So, I guess my question is now, how do I reference the form and control that I already have in .NET?

    Thank you for your help.

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

    Re: Object not set error

    Quote Originally Posted by stanav
    Dim frm As New frmMain will create a new instance of frmMain, and I believe it's not what you want. If you want a reference back to the main form in the current form, you should set its Owner property to the main form. Do something like this:
    VB Code:
    1. 'In main form
    2. Dim frm2 As New Form2
    3. frm2.Owner = Me
    4. frm2.Show
    Then in the 2nd form you do this
    VB Code:
    1. Dim frm As frmMain = DirectCast(Me.Owner, frmMain)
    2. Dim RC As AxXtremeReportControl.AxReportControl = frm.PEPReportControl
    3. '... blablabla...
    You should be aware of exactly what setting the Owner of a form does. The owned form becomes a modeless dialogue of the owner. That means that the owned from will always be displayed over the owner, it will be minimised and restored with the owner and it will be closed with the owner. If that's what you want then fine but it's a problem if that's not what you want.

    If you're displaying the owned form as a modal dialogue, i.e. calling ShowDialog, then it's no issue because the modal behaviour overrides the modeless anyway. If you're calling Show though, it may well be an issue.

    Basically, if the second form needs to refer to the first form then the first form needs to pass the second from a reference to itself. That's exactly what setting the Owner does, but if you don't want to create an owned form then it's a simple case of providing your own property or method to do it. If the seconf form is ALWAYS going to created by the first form then the logical solution is to pass the reference to the constructor. That way you don't need to cast the Owner property either, because your variable or property will already be the correct type.

    Note that, as always, this OOP behaviour mimics the real world. Let's say that you send someone off to do a job for you and you want them to contact at some point while they're doing it, how can they do so if they don't have your contact details? You might give them your phone number, which is the equivalent of passing them a reference to you. A reference is just a way to get access to an object.

    I strongly suggest that you read the "Forms in VB.NET" tutorial in my signature, which addresses some of these points. I'd also suggest that you read the OOP section from the Start VB.NET tutorial.

    Most importantly though, you need to remember that forms are objects and behave in exactly the same way as any other objects. Every form you design in the IDE is not an object. It is a class, i.e. a type. At run time, each form you create is an object, i.e. an instance of that type. You only create one form in the IDE but you can create an unlimited number of instances of that type.
    Last edited by jmcilhinney; Dec 7th, 2006 at 09:27 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Re: Object not set error

    Thank you Stanav for that information.

    jmcilhinney, I will read the information in your signature. I taught myself vb 6, and I am learning VB .NET on my own as well, so thank you for that detailed explanation. OOP makes sense, but sometimes it is hard for me to think OO. I am reading Microsoft's Programming Visual Basic .NET version 2003, and they discuss OOP, sometimes I have to stop and say, "What?" because it seems complex, but I enjoy it very much. Anyway, enough blabbing. While I was reading your post, during the part about modeless and showdialog, I remembered that I was showing this modal, or showdialog, and I remembered that code execution stops at the showdialog line until the form is hidden, so instead of populating the report control from frmUser (the second form) I hide the form, then execute the populate report control sub right after the show dialog line, since it is in frmMain, I just put Me.PEPReportControl. Then I dispose of frmUser.

    Again, thank you for suggesting the information about forms, and for the information that you posted.

    Ben

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Object not set error

    If you're displaying a modal dialogue then it is generally not appropriate for the dialogue to access the caller. The more correct way to do it is for the dialogue to expose whatever data is required via properties or methods and for the caller to retrieve it once ShowDialog returns. Here's an example of what that might look like.

    In Form1:
    VB Code:
    1. Dim f2 As New Form2
    2.  
    3. If f2.ShowDialog() = Windows.Forms.DialogResult.OK Then
    4.     'Get the data from the dialogue and display it.
    5.     Me.TextBox1.Text = f2.Value1
    6.     Me.TextBox2.Text = f2.Value2
    7. End If
    8.  
    9. f2.Dispose()
    It's up to you to declare the appropriate properties in Form2 to expose the required data, e.g.
    VB Code:
    1. Public ReadOnly Property Value1() As String
    2.     Get
    3.         Return Me.TextBox1.Text
    4.     End Get
    5. End Property
    6.  
    7. Public ReadOnly Property Value2() As String
    8.     Get
    9.         Return Me.TextBox2.Text
    10.     End Get
    11. End Property
    Now the dialogue is completely independent of the caller. The dialogue just appears, does its thing and disappears and this can be done from anywhere at any time. The Form2 doesn't care who calls it when or where. It just exposes the appropriate data and whoever wants it can get it. That's much better than the dialogue depending on being called by a specific type of form.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Re: [RESOLVED] Object not set error

    So then I should move the code that calls the sub to add the user to the database from the modal form to the main form, correct?

    Also, this form is for adding and editing users in the database. With the editing, should I make the properties both get and set? Then when I create and instance of the form I can load the values into the properties, then in the set put me.username.text = value?

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Object not set error

    That all sounds reasonable. There's also another option. Your caller could pass a DataRow to the dialogue. The dialogue could then edit that DataRow directly and neither form needs to access the other at all. When the user presses the OK button the dialogue simply updates the DataRow object it was passed and that's it. Those changes will be reflected in the caller because it references the same DataRow object.

    Form1:
    VB Code:
    1. Private Sub AddNewRow()
    2.     Dim dr As DataRow = myDataTable.NewRow()
    3.     Dim f2 As New Form2(dr)
    4.  
    5.     If f2.ShowDialog() = Windows.Forms.DialogResult.OK Then
    6.         myDataTable.Rows.Add(dr)
    7.     End If
    8. End Sub
    9.  
    10. Private Sub EditExistingRow(ByVal index As Integer)
    11.     Dim f2 As New Form2(myDataTable.Rows(index))
    12.  
    13.     f2.ShowDialog()
    14. End Sub
    Form2:
    VB Code:
    1. Private data As DataRow
    2.  
    3. Public Sub New(ByVal data As DataRow)
    4.     ' This call is required by the Windows Form Designer.
    5.     InitializeComponent()
    6.  
    7.     ' Add any initialization after the InitializeComponent() call.
    8.  
    9.     Me.data = data
    10.  
    11.     Me.TextBox1.Text = CStr(data("Column1"))
    12.     Me.TextBox2.Text = CStr(data("Column2"))
    13. End Sub
    14.  
    15. Private Sub okButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles okButton.Click
    16.     Me.data("Column1") = Me.TextBox1.Text
    17.     Me.data("Column2") = Me.TextBox2.Text
    18. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Re: [RESOLVED] Object not set error

    That's true, I already did the first option, but I am going to be thinking about this project before I go to bed, so I will definently give it some more thought.

    Thank you for your help.

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