Exception Error

lpaul

Active member
Joined
Dec 7, 2005
Messages
27
Programming Experience
Beginner
I am getting an
An unhandled exception of type 'System.NullReferenceException' occurred in test.exe
Additional information: Object reference not set to an instance of an object.

error on this line :

g1.MoveFirst()
That is below this line :
g1 = GetRecordsetCmd(g1, strSQL, 2)
Where do I need to start looking at to solve???????
:(

This the GetRecordset function line - would it have anything to do with?
Public Function GetRecordsetCmd(ByRef recRecordset As ADODB.Recordset, ByRef strQuery As String, ByRef intCmdType As Short) As Object
 
Last edited:
Yes, that function returned Nothing, so g1 is Nothing, and MoveFirst is not a method of Nothing.
 
Firstly, you should avoid using ADO in a .NET app if possible. Use ADO.NET if you can. Having said that, it doesn't really make sense that you are passing a variable by reference and then assigning the result to that same variable. Did you write that GetRecordsetCmd method yourself? Do you know the difference between ByVal and ByRef? Finally, if your g1 variable is a null reference after calling that method then that method is not returning an object, so that's where your issue is. You need to debug that method.
 
Did you write that GetRecordsetCmd method yourself? >>>No
Do you know the difference between ByVal and ByRef? >>>No
Finally, if your g1 variable is a null reference after calling that method then that method is not returning an object, so that's where your issue is. You need to debug that method.>>>>Can you guide me so I can learn. I am new with vb.net and do not know vb6. This was written by someone else.
__________________
Public Function GetRecordsetCmd(ByRef recRecordset As ADODB.Recordset, ByRef strQuery As String, ByRef intCmdType As Short) As Object
On Error GoTo ErrorHandler
recRecordset =
Nothing
recRecordset = New ADODB.Recordset
recRecordset.Open(strQuery, gConData, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockPessimistic, intCmdType)
GetRecordsetCmd = recRecordset
Exit Function
ErrorHandler:
MsgBox(Err.Description & " in GetRecordsetcmd", MsgBoxStyle.Critical)
End Function
 
I was reading just now that ByRef was a vb6 standard and now that I am using vb.net do I change all of the byref's to byval????
 
ByRef (By Reference) and ByVal (By Value) are ways of passing a variable as a parameter of a function/subroutine. The ByRef statement passes the variable so that whenever it is changed in your function/subroutine, it is changing its value throughout the program. The ByVal statement passes the variable as a copy so the scope of the variable is only local to that function/sub and it does not change its value throughout. There is some more information here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vakeyByRef.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vakeyByRef.asp
 
VB.NET:
[SIZE=2][COLOR=#0000ff] Public[/COLOR][/SIZE] [SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] GetRecordsetCmd([/SIZE][SIZE=2][COLOR=#0000ff]ByRef[/COLOR][/SIZE][SIZE=2] recRecordset [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ADODB.Recordset, [/SIZE][SIZE=2][COLOR=#0000ff]ByRef[/COLOR][/SIZE][SIZE=2] strQuery [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByRef[/COLOR][/SIZE][SIZE=2] intCmdType [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Short[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]On[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Error[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]GoTo[/COLOR][/SIZE][SIZE=2] ErrorHandler
recRecordset = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2]recRecordset = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ADODB.Recordset
recRecordset.Open(strQuery, gConData, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockPessimistic, intCmdType)
GetRecordsetCmd = recRecordset
[/SIZE][SIZE=2][COLOR=#0000ff]Exit[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function
[/COLOR][/SIZE][SIZE=2]ErrorHandler:
MsgBox(Err.Description & " in GetRecordsetcmd", MsgBoxStyle.Critical)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function
[/COLOR][/SIZE]


Your problem here is that you are not returning anything, you have no return statement, thus getting a Null Reference exception. At the end of that function, put in: return
recRecordset . Also, take out that onerrorgoto statement and try using the try, catch, finally statement.
 
elitesama said:

Your problem here is that you are not returning anything, you have no return statement, thus getting a Null Reference exception.
That's not actually true. The value of the 'recRecordset' variable is being assigned to the implicit local variable with the same name as the method. When you execute a function with no Return statement the value of this implicit local variable becomes the methods return value, so if you have assigned something to it then you are OK. Doing so is not advisable though, as it is less efficient than an explicit Return statement.

I assume that this code was written in VB6 and upgraded. There are a number of ways it could be improved, but in reality it is not obvious what the issue is. Unless the call to Recordset.Open is throwing an exception you should be getting a Recordset object returned. Do you get a MessageBox indicating that that is the case? If not I suggest that you place a breakpoint on the first line of that method and step through it. Click in the left hand border beside the first line and the line should be highlighted in red. When you run your app execution will halt at that point. Use F10 to step one line at a time and follow the values of variables, properties, etc. using the Autos, Locals and Watch windows. That's what debugging is all about.
 
I did a break and stepped though - I did not see anything that tells me I am getting something returned - Please see comments by >>> Is this what I should see???

recRecordset = New ADODB.Recordset >>>>Nothing
recRecordset.Open(strQuery, ConData, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockPessimistic, intCmdType)>> >points to a query
GetRecordsetCmd = recRecordset >>>> ADODB.RecordClass
 
This is a VB6 conversion and this line that should show the gRs1 = GetRecordsetCmd(gRs1, strSQL, 2) Shows "Nothing"
 
I found this code also which may work??? What do you think???
Private Sub btnSearchTitles_Click(sender As Object, e As System.EventArgs)
' database connection string
Dim DBConnection As String = Provider = SQLOLEDB;uid = sa;password = password;database = Pubs;DataSource = local

' sql statment
Dim SQL As String = "select title_id,title,type,price from Titles where title like '%" + txtTitles.Text.ToString() + "%' order by title" '

'create ADODB Connection object
Dim Conn As New ADODB.Connection()
'create ADODB Recordset object
Dim rs As New ADODB.Recordset()
'create OleDb Adapter object
Dim daTitles As New OleDbDataAdapter()
' finally Dataset to store returned recordset
Dim dsTitles As New DataSet("Authors")
'open connection with the string as above
Conn.Open(DBConnection, "", "", - 1)
'execute the query specifying static sursor, batch optimistic locking
rs.Open(SQL, DBConnection, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockBatchOptimistic, 1)
'use the overloaded version of Fill method which takes recordset as parameter
daTitles.Fill(dsTitles, rs, "Tiltes")
'bind datagrid to dataset
dataGridTitles.SetDataBinding(dsTitles, "Tiltes")
'close the connection
Conn.Close()
End Sub 'btnSearchTitles_Click
 
Back
Top