VB2008 to Access NullReferenceException

Object reference not set to an instance of an object

Page 1 of 1

9 Replies - 3654 Views - Last Post: 23 December 2008 - 12:19 PM Rate Topic: -----

#1 joshuaobelenus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-December 08

VB2008 to Access NullReferenceException

Post icon  Posted 23 December 2008 - 10:56 AM

I have a database I am attempting to write to, but I get an "Object reference not set to an instance of an object" error when running it.

Imports System
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
	Dim SerialCallLogDatabaseLocation As String = "B:\SomePlace\SomeWhere\CallLog.mdb"
	'Dim SerialCallLogDatabaseLocation As String = "c:\Inventory1.mdb" 'test data
	Dim SerialCallLogTableName As String = "tblInventory"
	Dim SerialCallLogDatabaseName As String = "CallLog" 'added 1
	Dim Serialinc As Integer
	Dim SerialMaxRows As Integer
	Dim Serialcon As New OleDb.OleDbConnection
	Dim Serialds As New DataSet
	Dim Serialda As OleDb.OleDbDataAdapter
	Dim Serialsql As String



	Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

		'Entry
		Dim Serialcb As New OleDb.OleDbCommandBuilder(Serialda)
		Dim SerialdsNewRow As DataRow

		SerialdsNewRow = Serialds.Tables(SerialCallLogTableName).NewRow

		SerialdsNewRow.Item("ID") = txtBarcode.Text
		SerialdsNewRow.Item("InvoiceNumber") = txtInvNum.Text
		SerialdsNewRow.Item("OrderNumber") = txtOrdNum.Text
		SerialdsNewRow.Item("InvoiceDate") = txtInvDate.Text
		SerialdsNewRow.Item("ItemDescription") = txtItem.Text
		SerialdsNewRow.Item("ItemCost") = txtCost.Text
		SerialdsNewRow.Item("DateUsed") = txtDateUsed.Text
		SerialdsNewRow.Item("OfficeUsed") = txtOffice.Text
		SerialdsNewRow.Item("SerialNumber") = txtCompSer.Text
		SerialdsNewRow.Item("UsedDefinition") = txtHowUsed.Text
		SerialdsNewRow.Item("UPCIfThere") = txtUPC.Text
		SerialdsNewRow.Item("SerialNumberIfThere") = txtSerNum.Text

		Serialds.Tables(SerialCallLogDatabaseName).Rows.Add(SerialdsNewRow)

		Serialda.Update(Serialds, SerialCallLogDatabaseName)

		'Reset
		txtBarcode.Clear()
		txtInvNum.Clear()
		txtOrdNum.Clear()
		txtInvDate.Clear()
		txtItem.Clear()
		txtCost.Clear()
		txtDateUsed.Clear()
		txtOffice.Clear()
		txtCompSer.Clear()
		txtHowUsed.Clear()
		txtUPC.Clear()
		txtSerNum.Clear()
		txtBarcode.Focus()

	End Sub

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'Connect
		Serialcon.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = " & SerialCallLogDatabaseLocation
		Serialcon.Open()
		Serialsql = "SELECT * FROM " & SerialCallLogTableName
		Serialda = New OleDb.OleDbDataAdapter(Serialsql, Serialcon)
		Serialda.Fill(Serialds, SerialCallLogDatabaseName)
		Serialcon.Close()
		SerialMaxRows = Serialds.Tables(SerialCallLogDatabaseName).Rows.Count
		Serialinc = -1

	End Sub
End Class



This error happens at the "SerialdsNewRow = Serialds.Tables(SerialCallLogTableName).NewRow"

Any suggestions?

Is This A Good Question/Topic? 0
  • +

Replies To: VB2008 to Access NullReferenceException

#2 dklingman  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 263
  • Joined: 23-December 08

Re: VB2008 to Access NullReferenceException

Posted 23 December 2008 - 11:07 AM

are you sure that the Tables contains SerialCallLogTableName?

i'm also not seeing anywhere that you set Serialds to a new instance of a dataset.

ignore what i said previously. what i meant to say was that i don't see anywhere you are adding any tables to the Serialds object.

This post has been edited by dklingman: 23 December 2008 - 11:10 AM

Was This Post Helpful? 0
  • +
  • -

#3 joshuaobelenus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-December 08

Re: VB2008 to Access NullReferenceException

Posted 23 December 2008 - 11:13 AM

Sorry, I replied before you said "ignore",

Since the table exists already, how should I go about adding a table (if needed)?

This post has been edited by joshuaobelenus: 23 December 2008 - 11:15 AM

Was This Post Helpful? 0
  • +
  • -

#4 dklingman  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 263
  • Joined: 23-December 08

Re: VB2008 to Access NullReferenceException

Posted 23 December 2008 - 11:24 AM

either put your code in a try catch block to get more information or walk through the code and check if the Serialds.Tables actually does have the table or if something happened that the table no long exists
Was This Post Helpful? 0
  • +
  • -

#5 dklingman  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 263
  • Joined: 23-December 08

Re: VB2008 to Access NullReferenceException

Posted 23 December 2008 - 11:30 AM

another thing is that you may need to fill the scheme. do the following in your form load
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'Connect
		Serialcon.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = " & SerialCallLogDatabaseLocation
		Serialcon.Open()
		Serialsql = "SELECT * FROM " & SerialCallLogTableName
		Serialda = New OleDb.OleDbDataAdapter(Serialsql, Serialcon)

		'Add this here
		'*************************************************************
		Serialds.Tables.AddRange(Serialda.FillSchema(Serialds, SchemaType.Mapped))
		'*************************************************************
		Serialda.Fill(Serialds, SerialCallLogDatabaseName)
		Serialcon.Close()
		SerialMaxRows = Serialds.Tables(SerialCallLogDatabaseName).Rows.Count
		Serialinc = -1

	End Sub


Was This Post Helpful? 0
  • +
  • -

#6 joshuaobelenus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-December 08

Re: VB2008 to Access NullReferenceException

Posted 23 December 2008 - 11:45 AM

View Postdklingman, on 23 Dec, 2008 - 10:30 AM, said:

another thing is that you may need to fill the scheme. do the following in your form load
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'Connect
		Serialcon.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = " & SerialCallLogDatabaseLocation
		Serialcon.Open()
		Serialsql = "SELECT * FROM " & SerialCallLogTableName
		Serialda = New OleDb.OleDbDataAdapter(Serialsql, Serialcon)

		'Add this here
		'*************************************************************
		Serialds.Tables.AddRange(Serialda.FillSchema(Serialds, SchemaType.Mapped))
		'*************************************************************
		Serialda.Fill(Serialds, SerialCallLogDatabaseName)
		Serialcon.Close()
		SerialMaxRows = Serialds.Tables(SerialCallLogDatabaseName).Rows.Count
		Serialinc = -1

	End Sub



I added the Schema code and it cam back as "DataTable already belongs to this DataSet." error.

From my other program (for the Call Log), I have the following:
'in Form1
	Dim CallLogDatabaseLocation As String = My.Settings.DatabaseLocation
	Dim CallLogTableName As String = My.Settings.DatabaseTable
	Dim CallLogDatabaseName As String = My.Settings.DatabaseName
	Dim inc As Integer
	Dim MaxRows As Integer
	Dim con As New OleDb.OleDbConnection
	Dim ds As New DataSet
	Dim da As OleDb.OleDbDataAdapter
	Dim sql As String

'On Load
'Added 9-25-08 For Database
		con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = " & CallLogDatabaseLocation
		con.Open()
		sql = "SELECT * FROM " & CallLogTableName
		da = New OleDb.OleDbDataAdapter(sql, con)
		da.Fill(ds, CallLogDatabaseName)
		con.Close()
		MaxRows = ds.Tables(CallLogDatabaseName).Rows.Count
		inc = -1

'on call completion
 'added 9-25-08 for database

		Dim cb As New OleDb.OleDbCommandBuilder(da)
		Dim dsNewRow As DataRow

		dsNewRow = ds.Tables(CallLogDatabaseName).NewRow()

		dsNewRow.Item("SerialNumber") = Serial
		dsNewRow.Item("SerialIP") = ComputerIP
		dsNewRow.Item("CallStart") = StartCall
		dsNewRow.Item("CallEnd") = EndCall
		dsNewRow.Item("CallLength") = CallTime
		dsNewRow.Item("Technician") = My.Settings.TechName
		dsNewRow.Item("CallerName") = Caller
		dsNewRow.Item("CallerOffice") = Office
		dsNewRow.Item("CallIssue") = MainIssue
		dsNewRow.Item("CallNotes") = Notes
		dsNewRow.Item("CallBillable") = BillableBoolean
		dsNewRow.Item("CallResolved") = ResolvedBoolean
		dsNewRow.Item("UncheckWhenBilled") = BillableBoolean
		dsNewRow.Item("DoNotBill") = DoNotBill
		dsNewRow.Item("DoNotBillReason") = DoNotBillReason
		dsNewRow.Item("BillableHardware") = BillForHardware
		dsNewRow.Item("BillableHardwareList") = txtBillableHardware.Text
		dsNewRow.Item("BillableSoftware") = BillForSoftware
		dsNewRow.Item("BillableSoftwareList") = txtBillableSoftware.Text
		dsNewRow.Item("BillableHardwareQuote") = HardwareQuotedPrice
		dsNewRow.Item("BillableSoftwareQuote") = SoftwareQuotedPrice
		dsNewRow.Item("AdditionalIPs") = AdditionalIPs
		dsNewRow.Item("AdditionalSerials") = AdditionalSerials
		dsNewRow.Item("MainIssue") = cbxMainIssue.Text
		dsNewRow.Item("SubIssue") = cbxSubIssue.Text

		ds.Tables(CallLogDatabaseName).Rows.Add(dsNewRow)

		da.Update(ds, CallLogDatabaseName)

		Reset() 'Calls the reset routine to clear the board for the next call.




and this is the code for the serial number entry:

Private Sub SerialTabEntry()
		Dim Serialcb As New OleDb.OleDbCommandBuilder(Serialda)
		Dim SerialdsNewRow As DataRow

		SerialdsNewRow = Serialds.Tables(SerialCallLogDatabaseName).NewRow()

		SerialdsNewRow.Item("SerialSerialNumber") = SerialSerial
		SerialdsNewRow.Item("SerialDateEntered") = dtpSerialDateEntered.Text
		SerialdsNewRow.Item("SerialDateRemoved") = dtpSerialDateRemoved.Text
		SerialdsNewRow.Item("SerialDateDestroyed") = dtpSerialDestroyed.Text
		SerialdsNewRow.Item("SerialComputerName") = txtSerialComputerName.Text
		SerialdsNewRow.Item("SerialCurrentLocation") = cboSerialLocation.Text
		SerialdsNewRow.Item("SerialCurrentCondition") = SerialCurrentStatus
		SerialdsNewRow.Item("SerialUser1Name") = txtSerialFirstUser.Text
		SerialdsNewRow.Item("SerialUser1Pass") = txtSerialFirstPass.Text
		SerialdsNewRow.Item("SerialUser2Name") = txtSerialSecondUser.Text
		SerialdsNewRow.Item("SerialUser2Pass") = txtSerialSecondPass.Text
		SerialdsNewRow.Item("SerialUser3Name") = txtSerialThirdUser.Text
		SerialdsNewRow.Item("SerialUser3Pass") = txtSerialThirdPass.Text
		SerialdsNewRow.Item("SerialUser4Name") = txtSerialFourthUser.Text
		SerialdsNewRow.Item("SerialUser4Pass") = txtSerialFourthPass.Text
		SerialdsNewRow.Item("SerialUser5Name") = txtSerialFifthUser.Text
		SerialdsNewRow.Item("SerialUser5Pass") = txtSerialFifthPass.Text
		SerialdsNewRow.Item("SerialPrinter1Model") = txtSerialFirstPrinter.Text
		SerialdsNewRow.Item("SerialPrinter1Share") = txtSerialFirstPrintShare.Text
		SerialdsNewRow.Item("SerialPrinter2Model") = txtSerialSecondPrinter.Text
		SerialdsNewRow.Item("SerialPrinter2Share") = txtSerialSecondPrintShare.Text
		SerialdsNewRow.Item("SerialNIC1MAC") = txtSerialFirstNICMAC.Text
		SerialdsNewRow.Item("SerialNIC1Speed") = SerialNIC1Speed
		SerialdsNewRow.Item("SerialNIC2MAC") = txtSerialSecondNICMAC.Text
		SerialdsNewRow.Item("SerialNIC2Speed") = SerialNIC2Speed
		SerialdsNewRow.Item("SerialMoBoMaker") = txtSerialMoBoMaker.Text
		SerialdsNewRow.Item("SerialMoBoModel") = txtSerialMoBoModel.Text
		SerialdsNewRow.Item("SerialCPUType") = txtSerialCPUType.Text
		SerialdsNewRow.Item("SerialCPUSpeed") = txtSerialCPUSpeed.Text
		SerialdsNewRow.Item("SerialCPUSocket") = cboSerialCPUSocket.Text
		SerialdsNewRow.Item("SerialHDDType") = cboSerialHDDType.Text
		SerialdsNewRow.Item("SerialHDDSize") = txtSerialHardDriveSize.Text
		SerialdsNewRow.Item("SerialOpticalType") = cboSerialOpticalType.Text
		SerialdsNewRow.Item("SerialRAMSpeed") = txtSerialRAMSpeed.Text
		SerialdsNewRow.Item("SerialRAMAmount") = txtSerialRAMAmount.Text
		SerialdsNewRow.Item("SerialOSVersion") = cboSerialOSVersion.Text
		SerialdsNewRow.Item("SerialOSKey") = txtSerialOSKey.Text
		SerialdsNewRow.Item("SerialOfficeVersion") = cboSerialOfficeVersion.Text
		SerialdsNewRow.Item("SerialOfficeKey") = txtSerialOfficeKey.Text
		SerialdsNewRow.Item("SerialOEMSerial") = txtSerialOEMSerial.Text
		SerialdsNewRow.Item("SerialOEMInvoice") = txtSerialOEMInvoice.Text
		SerialdsNewRow.Item("SerialOEMOrder") = txtSerialOEMOrder.Text
		SerialdsNewRow.Item("SerialOriginalCost") = txtSerialCost.Text
		SerialdsNewRow.Item("SerialDepreciatedValue") = txtSerialDepVal.Text
		SerialdsNewRow.Item("SerialAdditionalSoftware") = SerialAddSoft
		SerialdsNewRow.Item("SerialAdditionalHardware") = SerialAddHard
		SerialdsNewRow.Item("SerialAdditionalInformation") = SerialAddInfo
		
		Serialds.Tables(SerialCallLogDatabaseName).Rows.Add(SerialdsNewRow)

		Serialda.Update(Serialds, SerialCallLogDatabaseName)
	End Sub



These work, so im kinda wondering why this program is not.
I will try a new blank database here in 3 minutes.



I tried with new blank database and still got same NullReference error
Was This Post Helpful? 0
  • +
  • -

#7 joshuaobelenus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-December 08

Re: VB2008 to Access NullReferenceException

Posted 23 December 2008 - 11:56 AM

View Postdklingman, on 23 Dec, 2008 - 10:24 AM, said:

either put your code in a try catch block to get more information or walk through the code and check if the Serialds.Tables actually does have the table or if something happened that the table no long exists


After putting in a try-->catch block, I still get a vauge execption:

"System.NullReferenceException: Object reference not set to an instance of an object. at Inventory_Item_Control.Form1.btnAdd_Click(Object sender, EventArgs e) in C:\docs\user\vs2008\projects\Inventory Item Control\Form1.vb:line 41"

Line 41 is
SerialdsNewRow = Serialds.Tables(SerialCallLogTableName).NewRow


Was This Post Helpful? 0
  • +
  • -

#8 dklingman  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 263
  • Joined: 23-December 08

Re: VB2008 to Access NullReferenceException

Posted 23 December 2008 - 12:08 PM

i see what your problem is... look at your form load and what you are doing in the click event

'this is in your load event
'notice the variable that you are using is SerialCallLogDatabaseName
Serialda.Fill(Serialds, SerialCallLogDatabaseName)
		Serialcon.Close()
		SerialMaxRows = Serialds.Tables(SerialCallLogDatabaseName).Rows.Count



'this is what you have in your click event
'again notice what variable you are using SerialCallLogTableName
SerialdsNewRow = Serialds.Tables(SerialCallLogTableName).NewRow



as i said before are you sure that the table exist? this is your problem you have your variables screwed up. your looking for the a table by the name of CallLog rather than tblInventory, because you are using the wrong variables

This post has been edited by dklingman: 23 December 2008 - 12:09 PM

Was This Post Helpful? 1
  • +
  • -

#9 joshuaobelenus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-December 08

Re: VB2008 to Access NullReferenceException

Posted 23 December 2008 - 12:16 PM

View Postdklingman, on 23 Dec, 2008 - 11:08 AM, said:

i see what your problem is... look at your form load and what you are doing in the click event

'this is in your load event
'notice the variable that you are using is SerialCallLogDatabaseName
Serialda.Fill(Serialds, SerialCallLogDatabaseName)
		Serialcon.Close()
		SerialMaxRows = Serialds.Tables(SerialCallLogDatabaseName).Rows.Count



'this is what you have in your click event
'again notice what variable you are using SerialCallLogTableName
SerialdsNewRow = Serialds.Tables(SerialCallLogTableName).NewRow



as i said before are you sure that the table exist? this is your problem you have your variables screwed up. your looking for the a table by the name of CallLog rather than tblInventory, because you are using the wrong variables



AHHHHH!!!!! Thank You!!!! You dont know how many times I racked my head on the table over this.
I blame VB and the "Helpful Feature" of AutoComplete, but I also blame myslef for not checking this.

Thank you for all your help.

-Joshua D. Obelenus


Fixed and it worls like a charm!
Was This Post Helpful? 0
  • +
  • -

#10 dklingman  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 263
  • Joined: 23-December 08

Re: VB2008 to Access NullReferenceException

Posted 23 December 2008 - 12:19 PM

not a problem. it's the little things that always get us

just a rule of thumb and something that i do to ensure i don't do things that this anymore. when naming your variables do something like the following

Const TBL_THENAMEOFTHETABLE As String "tblInv"
Const DB_THENAMEOFTHEDATABASE As String = "CallLog"


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1