NullReferenceException unhandled....

syntax-error

New member
Joined
Mar 18, 2005
Messages
3
Programming Experience
1-3
"NullReferenceException unhandled: Object reference not set to an instance of an object."

This is the error im getting. I dont understand how to fix it. Here is the source for the form (red text is where error occurs):

VB.NET:
Imports System
Imports System.Data
Imports System.Xml

Public Class Form1

	'initiate the object
	Dim recsave As New adoxml.save.qssave


	Sub Page_Load()

		recsave = New adoxml.save.qssave

	End Sub

	Private Sub btnName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnName.Click


		'call the method for a new record
		recsave.newrecord(txtDbName.Text.ToString)

	End Sub

	Private Sub btnSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSet.Click


	End Sub
End Class

And here is the code for the qssave object (error is thrown in this object)

VB.NET:
'Title:	 qsave
'Date:	  3-18-05
'Author:	Chase Spell
'Desc.:	 A class for saving user input to a dataset then exporting the
'		   dataset to an xml file to be read from by a different class.

Imports System
Imports System.data
Imports System.Xml

Namespace adoxml.save

	Public Class qssave
		Inherits System.Data.DataSet

#Region "Declarations"



		Dim mQuestion As Integer = 0
#End Region

#Region "Properties"

#End Region

#Region "Methods"

		Public Sub newrecord(ByVal recordname As String)

			'check to make sure var "recordname" has been supplied
			If Not recordname = "" Then

			 ' Create a new "Database" DataTable and add it to the DataSet
			    Dim databasetable As New DataTable("database")
				MyBase.Tables.Add(databasetable)

			 ' Create a new column for the table, set its name to var 
			 ' recordname and add it to the Columns collection for the table. 
				Dim DbName As New DataColumn("name")
			 DbName.DataType = System.Type.GetType("System.String")
				[color=Red]MyBase.Tables("databasetable").Columns.Add(DbName)[/color]

			 ' Create a DataRow, add it to the table, and set its values. 
				Dim database As DataRow
			    database = MyBase.Tables("databasetable").NewRow
			    MyBase.Tables("databasetable").Rows.Add(database)
				database("name") = recordname
				MyBase.AcceptChanges()

			 ' Now create order DataTable with appropriate structure
			    Dim questiontable As New DataTable("question")
				MyBase.Tables.Add(questiontable)

			    'create a datacolumn for questiontable number
			    Dim number As New DataColumn("number")
			 number.DataType = System.Type.GetType("System.Int32")
			    MyBase.Tables("questiontable").Columns.Add(number)

			    'create a datacolumn for questiontable question
				Dim q As New DataColumn("q")
			    q.DataType = System.Type.GetType("System.String")
			    MyBase.Tables("questiontable").Columns.Add(q)

			    'create a datacolumn for questiontable a
				Dim a As New DataColumn("a")
			    a.DataType = System.Type.GetType("System.String")
			    MyBase.Tables("questiontable").Columns.Add(a)

			    'create a datacolumn for questiontable b
				Dim b As New DataColumn("a")
			    b.DataType = System.Type.GetType("System.String")
			    MyBase.Tables("questiontable").Columns.Add(b)

			    'create a datacolumn for questiontable c
				Dim c As New DataColumn("a")
			    c.DataType = System.Type.GetType("System.String")
			    MyBase.Tables("questiontable").Columns.Add(c)

			    'create a datacolumn for questiontable d
				Dim d As New DataColumn("a")
			    d.DataType = System.Type.GetType("System.String")
			    MyBase.Tables("questiontable").Columns.Add(d)

			    'create a datacolumn for questiontable e
				Dim e As New DataColumn("a")
			    e.DataType = System.Type.GetType("System.String")
			    MyBase.Tables("questiontable").Columns.Add(e)

			    'create a datacolumn for questiontable correct
			    Dim correct As New DataColumn("correct")
			 correct.DataType = System.Type.GetType("System.String")
			    MyBase.Tables("questiontable").Columns.Add(correct)

			End If

		End Sub

		Public Sub newquestion(ByVal question As String, ByVal _
		usera As String, ByVal userb As String, ByVal userc As String, ByVal _
		userd As String, ByVal usere As String, ByVal usercorrect As String)
			'if mQuestion is <= 31 then add the new question
			If mQuestion <= 31 Then

				'set the value of question
				Dim quest As DataRow
			    quest = MyBase.Tables("questiontable").NewRow
				quest.Item("number") = mQuestion
				quest.Item("q") = question
				quest.Item("a") = usera
				quest.Item("b") = userb
				quest.Item("c") = userc
				quest.Item("d") = userd
				quest.Item("e") = usere
				quest.Item("correct") = usercorrect
			    MyBase.Tables("questiontable").Rows.Add(quest)
			    mQuestion += 1  'Increment mQuestion by 1


			End If

		End Sub
#End Region

	End Class

End Namespace

Thanks for your help!
 
Last edited:
Hi,
I guess u want to add column in "database" but in code u r table name is "databasetable" which is not there in Dataset......................

Just replace

Dim databasetable As New DataTable("database")

with

Dim databasetable As New DataTable("databasetable")

I hope this will solve ur problem.......................:cool:

Regards,
Ritesh
 
Back
Top