Solved

Object Reference Not Set to an Instance of an Object

Posted on 2014-03-15
10
251 Views
Last Modified: 2014-03-16
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
Comment
Question by:MicheleRobinson
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39931281
with what error message?
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 39931284
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39931285
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
 

Author Comment

by:MicheleRobinson
ID: 39931352
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
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39931476
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:MicheleRobinson
ID: 39932253
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
 

Author Comment

by:MicheleRobinson
ID: 39932305
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
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39932544
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
 

Author Closing Comment

by:MicheleRobinson
ID: 39933141
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
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39933152
Not a problem, glad to help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now