Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

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

0
MicheleRobinson
Asked:
MicheleRobinson
  • 4
  • 3
  • 2
  • +1
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
with what error message?
0
 
sammySeltzerCommented:
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.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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 " ?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
MicheleRobinsonAuthor Commented:
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?
0
 
Fernando SotoRetiredCommented:
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

0
 
MicheleRobinsonAuthor Commented:
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.
0
 
MicheleRobinsonAuthor Commented:
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
0
 
Fernando SotoRetiredCommented:
Hi MicheleRobinson;

Because you are creating the DataTable manually you will also need to give the table a name because it seems to be a requirement of the DataView object. When you get a DataTable from a Database connection this is done for you automatically so you need to do this, Change this line in your code :
Dim dTblInvoice As New DataTable

' To this

Dim dTblInvoice As New DataTable("Invoice")

Open in new window

Note that the parameter "Invoice" will become the TableName of the DataTable object and "Invoice" can be any name you wish.

The other thing I noticed is that of these two lines of code :
rowView("WORKORDER") = txtWO.Text
rowView("WODESCRIP") = txtJobDesc.Text  

Open in new window

There are a couple of places that capitalization matters in Visual Basic when accessing XML data from a XML document and another is here when accessing the column names of the DataTable and I sure there some more. Make the following two changes from the above two lines :
rowView("WorkOrder") = txtWO.Text
rowView("woDesc") = txtJobDesc.Text

Open in new window

That should correct the issues.
0
 
MicheleRobinsonAuthor Commented:
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
0
 
Fernando SotoRetiredCommented:
Not a problem, glad to help.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now