Avatar of MicheleRobinson
MicheleRobinson asked on

Object Reference Not Set to an Instance of an Object

Looking for some help here.  This piece of code used to run, but now crashes in the debugger at the 3rd line.  I am not sure as to why.  What am I missing here?

Thanks,
-Vernon



    
Private Sub fillAdditional()

Dim dvinv as NEW DataView
        
[b]Dim rowView As DataRowView = dvinv.AddNew[/b]

        rowView("WORKORDER") = txtWO.Text
        rowView("WODESCRIP") = txtJobDesc.Text
        rowView.EndEdit()

        GridAllow.DataSource = dvinv
    End Sub

Open in new window

Visual Basic.NET

Avatar of undefined
Last Comment
Fernando Soto

8/22/2022 - Mon
Guy Hengel [angelIII / a3]

with what error message?
sammySeltzer

That error usually means that the property of the object you are trying to access is null.

Try and figure out which property is null.

You can also wrap an IF statement around them, If null then do something.
Guy Hengel [angelIII / a3]

sorry, with the error message in the question title ...

so, in the third line, is the variable dvinv still "nothing", though you defined "AS NEW " ?
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER
MicheleRobinson

Yes.  dvinv is still nothing.  I thought declaring it as new would instantiate a copy of the class.  The other lines in the routine would then populate dvInv  so that could be used by the grid to display the value. What am I missing?
Fernando Soto

Hi MicheleRobinson;

The problem I see is that you are defining a new DataView but are not initializing it so that it has associated columns to match the data source and so when you go to add a new row the properties that it uses to do that are null / Nothing. See code snippet below.

' You define a new DataView here with zero items in it
Dim dvinv as NEW DataView
' Here you attempt to add a new DataView Item but you have
' no information on what a new DataView item looks like.
' You have no DataTable associated with this new Data View.
Dim rowView As DataRowView = dvinv.AddNew

'For the next part here I will assume that GridAllow is a DataGridView

Dim dvinv as NEW DataView
' The next line adds all the information needed to do what you need
' Adding this new line of code
dvinv.Table = CType(dgv.DataSource, DataTable)
Dim rowView As DataRowView = dvinv.AddNew
' These to columns WORKORDER and WODESCRIP must exist as columns in the table
rowView("WORKORDER") = txtWO.Text
rowView("WODESCRIP") = txtJobDesc.Text
rowView.EndEdit()

GridAllow.DataSource = dvinv

Open in new window

ASKER
MicheleRobinson

Fernando,
Thank you for such a detailed explanation. I understand from your comments what is missing.  Now my next question.

The grid on the form is for display purposes only.  The two columns are part of the grid on the form.  I am not sure how to create a table that does not link to a physical table (e.g. SQL Server).  This code is being used to add items to this grid.  I inherited this code and it used to work. I am not sure why it no longer does.  Any further tips.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
MicheleRobinson

I have studied and tried some additional things, but I keep running into the same problem.  Here is the code snippet where I tried to create an empty table and attach it as the datasource to the grid upon program start up.  It says that the data source type is Object, but it is still



Public Class XtraInvoiceForm
    Dim Acon As New ConnectDB
    Dim con As New OleDb.OleDbConnection
    Dim fi As Integer = 0
    Dim strINVTmp As String = ""

    Dim daINV As OleDb.OleDbDataAdapter
    Dim dsINV As New DataSet
    Dim finvAdd As Boolean = False
	Dim bValueChange As Boolean = False
  Dim strJobDate As String = ""
  Dim dTblInvoice As New DataTable
  Dim Col1 As New DataColumn
  Dim Col2 As New DataColumn
'***************************

    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        con = Acon.GetConnect
        dsINV = Nothing
        ' Add any initialization after the InitializeComponent() call.

        'Call iniInvoice()
    Call controlButton()
    ' Create table for Invoice Grid 
    Col1.DataType = System.Type.GetType("System.String")
    Col1.ColumnName = "WorkOrder"
    Col1.Caption = "Work Order"
    Col1.ReadOnly = True
    dTblInvoice.Columns.Add(Col1)
    ' Add Work Order Description
    Col2.DataType = System.Type.GetType("System.String")
    Col2.ColumnName = "woDesc"
    Col2.Caption = "Description"
    Col2.ReadOnly = True
    dTblInvoice.Columns.Add(Col2)
    ' Set the datasource for the GridAllow 
    GridAllow.DataSource = dTblInvoice
'*******************************************
  Private Sub fillAdditional()

      'For the next part here I will assume that GridAllow is a DataGridView

    ' The next line adds all the information needed to do what you need
    ' Adding this new line of code
    dvinv.Table = CType(GridAllow.DataSource, DataTable)
    Dim rowView As DataRowView = dvinv.AddNew
    ' These to columns WORKORDER and WODESCRIP must exist as columns in the table
    rowView("WORKORDER") = txtWO.Text
    rowView("WODESCRIP") = txtJobDesc.Text
    rowView.EndEdit()

    GridAllow.DataSource = dvinv

Open in new window

20140316-DebuggerShot.JPG
20140316-ErrorMessage.JPG
ASKER CERTIFIED SOLUTION
Fernando Soto

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
MicheleRobinson

Thank you for the excellent assist.  The tip on the need for the same CASE  with the field names was helpful.  This would have been one of those mysteries that take up too much time to resolve.

Thanks again,
-Vernon
Fernando Soto

Not a problem, glad to help.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes