Solved

Object Reference Not Set to an Instance of an Object

Posted on 2014-03-15
10
249 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

10 Experts available now in Live!

Get 1:1 Help Now