Solved

Object Reference Not Set to an Instance of an Object

Posted on 2014-03-15
10
253 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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
 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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