Help with object reference error

  • Thread starter Thread starter Brett
  • Start date Start date
B

Brett

I'm not sure why I keep getting this error, "Object reference not set to an
instance of an object".

Private Function somefunction() as string
Dim MyCurrentClass As New Class1

Try
For i As Integer = 0 To LinkResults.Length - 1
With MyCurrentClass.SqlCmd_Insert_LinkVB
.CommandType = System.Data.CommandType.StoredProcedure
'error occurs here

-- do something --
End Function

The above code is in Class1. If there is a problem with the reference to
the object instance, why doesn't this line throw an error at
With MyCurrentClass.SqlCmd_Insert_LinkVB

The new class has been created, which means there is an instance available.
What am I doing wrong?

Thanks,
Brett
 
The error "Object reference not set to an instance of an object" always
means (afaik) that you are referencing an object that is set to nothing.
You are correct that you create a new version of Class1 and MyCurrentClass
is set to something, that is not what the runtime is complaining about. It
is complaining that the object returned by
MyCurrentClass.SqlCmd_Insert_LinkVB is set to nothing.

So you need to find out why the SqlCmd_Insert_LinkVB method returns nothing.

Hope this helps.
Chris
 
Ok, I see the problem now that you describe it this way. I first call a sub
named, InitailizeDatabaseConnections()

In the sub I do this:

Me.SqlCmd_Insert_LinkVB.CommandText = "dbo.[Insert_LinkVB]"
Me.SqlCmd_Insert_LinkVB.CommandType =
System.Data.CommandType.StoredProcedure
Me.SqlCmd_Insert_LinkVB.Connection = Me.mycn

In the watches, I see that Me.SqlCmd_Insert_LinkVB does have values. Once I
leave InitailizeDatabaseConnections(), Me.SqlCmd_Insert_LinkVB is set to
Nothing. I've moved the above declarations out of the sub and into the
class. However, I declare the parameters within the sub:

Me.SqlCmd_Insert_LinkVB.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@message_id", System.Data.SqlDbType.Int,
4))

The above is just one parameter declaration but it keeps loosing its value
after I move out of InitailizeDatabaseConnections(). I must do the
assignments with a sub. How else can this be done so other subs will have
access to the params?

I'd like to declare all the params in one sub then do assignments to the
params in another sub.

Thanks,
Brett
 
Well you are doing more than you describe in the sub. Where do you declare
SqlCmd_Insert_LinkVB. Since you didnt show enough code I can't tell you
what you are doing wrong. I will give you an example of how to make it work
though.


'Note: I typed this all in here, syntax here will occur, not tested
Public Class Class1

Private m_SqlCmd_Insert_LinkVB as SqlCommand 'This is the local variable
to the class

Private Sub InitailizeDatabaseConnections
'This creates a new version of the command object.
m_SqlCmd_Insert_LinkVB = New SqlCommand

'Now the command object is vaild, load up your settings
Me.m_SqlCmd_Insert_LinkVB.CommandText = "dbo.[Insert_LinkVB]"
Me.m_SqlCmd_Insert_LinkVB.CommandType =
System.Data.CommandType.StoredProcedure
Me.m_SqlCmd_Insert_LinkVB.Connection = Me.mycn
End Sub

'This property gets called from an outside class to return an object
Public ReadOnly Property SqlCmd_Insert_LinkVB
Get
Return m_SqlCmd_Insert_LinkVB
end get
End Property

End Class

Hope this helps
Chris
Brett said:
Ok, I see the problem now that you describe it this way. I first call a
sub named, InitailizeDatabaseConnections()

In the sub I do this:

Me.SqlCmd_Insert_LinkVB.CommandText = "dbo.[Insert_LinkVB]"
Me.SqlCmd_Insert_LinkVB.CommandType =
System.Data.CommandType.StoredProcedure
Me.SqlCmd_Insert_LinkVB.Connection = Me.mycn

In the watches, I see that Me.SqlCmd_Insert_LinkVB does have values. Once
I leave InitailizeDatabaseConnections(), Me.SqlCmd_Insert_LinkVB is set to
Nothing. I've moved the above declarations out of the sub and into the
class. However, I declare the parameters within the sub:

Me.SqlCmd_Insert_LinkVB.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@message_id",
System.Data.SqlDbType.Int, 4))

The above is just one parameter declaration but it keeps loosing its value
after I move out of InitailizeDatabaseConnections(). I must do the
assignments with a sub. How else can this be done so other subs will have
access to the params?

I'd like to declare all the params in one sub then do assignments to the
params in another sub.

Thanks,
Brett

Chris said:
The error "Object reference not set to an instance of an object" always
means (afaik) that you are referencing an object that is set to nothing.
You are correct that you create a new version of Class1 and
MyCurrentClass is set to something, that is not what the runtime is
complaining about. It is complaining that the object returned by
MyCurrentClass.SqlCmd_Insert_LinkVB is set to nothing.

So you need to find out why the SqlCmd_Insert_LinkVB method returns
nothing.

Hope this helps.
Chris
 
That's what I'm doing - declaring them as private in the class. I haven't
created the initializeDatabaseConnection() sub yet. I have everything in
one sub for simplicity. Now I keep getting this error "Exception has been
thrown by the target of an invocation".

on this line
..ExecuteNonQuery()

Here is the code:
With SqlCmd_spamBlacklist_Insert
.CommandType =
System.Data.CommandType.StoredProcedure
.Parameters("@messageid").Value = messageid
.Parameters("@listname").Value = ListName
.Parameters("@listurl").Value = ListURL
.ExecuteNonQuery()
End With

It's a very generic error. How do I go about debugging it?

Thanks,
Brett


Chris said:
Well you are doing more than you describe in the sub. Where do you
declare SqlCmd_Insert_LinkVB. Since you didnt show enough code I can't
tell you what you are doing wrong. I will give you an example of how to
make it work though.


'Note: I typed this all in here, syntax here will occur, not tested
Public Class Class1

Private m_SqlCmd_Insert_LinkVB as SqlCommand 'This is the local
variable to the class

Private Sub InitailizeDatabaseConnections
'This creates a new version of the command object.
m_SqlCmd_Insert_LinkVB = New SqlCommand

'Now the command object is vaild, load up your settings
Me.m_SqlCmd_Insert_LinkVB.CommandText = "dbo.[Insert_LinkVB]"
Me.m_SqlCmd_Insert_LinkVB.CommandType =
System.Data.CommandType.StoredProcedure
Me.m_SqlCmd_Insert_LinkVB.Connection = Me.mycn
End Sub

'This property gets called from an outside class to return an object
Public ReadOnly Property SqlCmd_Insert_LinkVB
Get
Return m_SqlCmd_Insert_LinkVB
end get
End Property

End Class

Hope this helps
Chris
Brett said:
Ok, I see the problem now that you describe it this way. I first call a
sub named, InitailizeDatabaseConnections()

In the sub I do this:

Me.SqlCmd_Insert_LinkVB.CommandText = "dbo.[Insert_LinkVB]"
Me.SqlCmd_Insert_LinkVB.CommandType =
System.Data.CommandType.StoredProcedure
Me.SqlCmd_Insert_LinkVB.Connection = Me.mycn

In the watches, I see that Me.SqlCmd_Insert_LinkVB does have values.
Once I leave InitailizeDatabaseConnections(), Me.SqlCmd_Insert_LinkVB is
set to Nothing. I've moved the above declarations out of the sub and
into the class. However, I declare the parameters within the sub:

Me.SqlCmd_Insert_LinkVB.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@message_id",
System.Data.SqlDbType.Int, 4))

The above is just one parameter declaration but it keeps loosing its
value after I move out of InitailizeDatabaseConnections(). I must do
the assignments with a sub. How else can this be done so other subs will
have access to the params?

I'd like to declare all the params in one sub then do assignments to the
params in another sub.

Thanks,
Brett

Chris said:
The error "Object reference not set to an instance of an object" always
means (afaik) that you are referencing an object that is set to nothing.
You are correct that you create a new version of Class1 and
MyCurrentClass is set to something, that is not what the runtime is
complaining about. It is complaining that the object returned by
MyCurrentClass.SqlCmd_Insert_LinkVB is set to nothing.

So you need to find out why the SqlCmd_Insert_LinkVB method returns
nothing.

Hope this helps.
Chris



I'm not sure why I keep getting this error, "Object reference not set
to an instance of an object".

Private Function somefunction() as string
Dim MyCurrentClass As New Class1

Try
For i As Integer = 0 To LinkResults.Length - 1
With MyCurrentClass.SqlCmd_Insert_LinkVB
.CommandType =
System.Data.CommandType.StoredProcedure 'error occurs here

-- do something --
End Function

The above code is in Class1. If there is a problem with the reference
to the object instance, why doesn't this line throw an error at
With MyCurrentClass.SqlCmd_Insert_LinkVB

The new class has been created, which means there is an instance
available. What am I doing wrong?

Thanks,
Brett
 
I have it working now. I needed to declare this way in the public class
area:

Friend WithEvents SqlCmd_Insert_LinkVB As System.Data.SqlClient.SqlCommand

Then in the sub, I do this:
Me.SqlCmd_Insert_LinkVB = New System.Data.SqlClient.SqlCommand

Thanks,
Brett

Brett said:
That's what I'm doing - declaring them as private in the class. I haven't
created the initializeDatabaseConnection() sub yet. I have everything in
one sub for simplicity. Now I keep getting this error "Exception has been
thrown by the target of an invocation".

on this line
.ExecuteNonQuery()

Here is the code:
With SqlCmd_spamBlacklist_Insert
.CommandType =
System.Data.CommandType.StoredProcedure
.Parameters("@messageid").Value = messageid
.Parameters("@listname").Value = ListName
.Parameters("@listurl").Value = ListURL
.ExecuteNonQuery()
End With

It's a very generic error. How do I go about debugging it?

Thanks,
Brett


Chris said:
Well you are doing more than you describe in the sub. Where do you
declare SqlCmd_Insert_LinkVB. Since you didnt show enough code I can't
tell you what you are doing wrong. I will give you an example of how to
make it work though.


'Note: I typed this all in here, syntax here will occur, not tested
Public Class Class1

Private m_SqlCmd_Insert_LinkVB as SqlCommand 'This is the local
variable to the class

Private Sub InitailizeDatabaseConnections
'This creates a new version of the command object.
m_SqlCmd_Insert_LinkVB = New SqlCommand

'Now the command object is vaild, load up your settings
Me.m_SqlCmd_Insert_LinkVB.CommandText = "dbo.[Insert_LinkVB]"
Me.m_SqlCmd_Insert_LinkVB.CommandType =
System.Data.CommandType.StoredProcedure
Me.m_SqlCmd_Insert_LinkVB.Connection = Me.mycn
End Sub

'This property gets called from an outside class to return an object
Public ReadOnly Property SqlCmd_Insert_LinkVB
Get
Return m_SqlCmd_Insert_LinkVB
end get
End Property

End Class

Hope this helps
Chris
Brett said:
Ok, I see the problem now that you describe it this way. I first call a
sub named, InitailizeDatabaseConnections()

In the sub I do this:

Me.SqlCmd_Insert_LinkVB.CommandText = "dbo.[Insert_LinkVB]"
Me.SqlCmd_Insert_LinkVB.CommandType =
System.Data.CommandType.StoredProcedure
Me.SqlCmd_Insert_LinkVB.Connection = Me.mycn

In the watches, I see that Me.SqlCmd_Insert_LinkVB does have values.
Once I leave InitailizeDatabaseConnections(), Me.SqlCmd_Insert_LinkVB is
set to Nothing. I've moved the above declarations out of the sub and
into the class. However, I declare the parameters within the sub:

Me.SqlCmd_Insert_LinkVB.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@message_id",
System.Data.SqlDbType.Int, 4))

The above is just one parameter declaration but it keeps loosing its
value after I move out of InitailizeDatabaseConnections(). I must do
the assignments with a sub. How else can this be done so other subs
will have access to the params?

I'd like to declare all the params in one sub then do assignments to the
params in another sub.

Thanks,
Brett

"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com>
wrote in message The error "Object reference not set to an instance of an object" always
means (afaik) that you are referencing an object that is set to
nothing. You are correct that you create a new version of Class1 and
MyCurrentClass is set to something, that is not what the runtime is
complaining about. It is complaining that the object returned by
MyCurrentClass.SqlCmd_Insert_LinkVB is set to nothing.

So you need to find out why the SqlCmd_Insert_LinkVB method returns
nothing.

Hope this helps.
Chris



I'm not sure why I keep getting this error, "Object reference not set
to an instance of an object".

Private Function somefunction() as string
Dim MyCurrentClass As New Class1

Try
For i As Integer = 0 To LinkResults.Length - 1
With MyCurrentClass.SqlCmd_Insert_LinkVB
.CommandType =
System.Data.CommandType.StoredProcedure 'error occurs here

-- do something --
End Function

The above code is in Class1. If there is a problem with the reference
to the object instance, why doesn't this line throw an error at
With MyCurrentClass.SqlCmd_Insert_LinkVB

The new class has been created, which means there is an instance
available. What am I doing wrong?

Thanks,
Brett
 
Two questions.

1: Why are you using the WithEvents keyword. This is fine if you are
consuming event fired byt he object, but I doubt you are.

2: Why use friends? I assume you are doing this so objects outside of the
class can access the variable. This is fine, but I prefer the way I showed
you in my example by declaring the object as private and then giving access
to it in a method. This lets you make changes in the future easier.

Chris

Brett said:
I have it working now. I needed to declare this way in the public class
area:

Friend WithEvents SqlCmd_Insert_LinkVB As System.Data.SqlClient.SqlCommand

Then in the sub, I do this:
Me.SqlCmd_Insert_LinkVB = New System.Data.SqlClient.SqlCommand

Thanks,
Brett

Brett said:
That's what I'm doing - declaring them as private in the class. I
haven't created the initializeDatabaseConnection() sub yet. I have
everything in one sub for simplicity. Now I keep getting this error
"Exception has been thrown by the target of an invocation".

on this line
.ExecuteNonQuery()

Here is the code:
With SqlCmd_spamBlacklist_Insert
.CommandType =
System.Data.CommandType.StoredProcedure
.Parameters("@messageid").Value =
messageid
.Parameters("@listname").Value = ListName
.Parameters("@listurl").Value = ListURL
.ExecuteNonQuery()
End With

It's a very generic error. How do I go about debugging it?

Thanks,
Brett


Chris said:
Well you are doing more than you describe in the sub. Where do you
declare SqlCmd_Insert_LinkVB. Since you didnt show enough code I can't
tell you what you are doing wrong. I will give you an example of how to
make it work though.


'Note: I typed this all in here, syntax here will occur, not tested
Public Class Class1

Private m_SqlCmd_Insert_LinkVB as SqlCommand 'This is the local
variable to the class

Private Sub InitailizeDatabaseConnections
'This creates a new version of the command object.
m_SqlCmd_Insert_LinkVB = New SqlCommand

'Now the command object is vaild, load up your settings
Me.m_SqlCmd_Insert_LinkVB.CommandText = "dbo.[Insert_LinkVB]"
Me.m_SqlCmd_Insert_LinkVB.CommandType =
System.Data.CommandType.StoredProcedure
Me.m_SqlCmd_Insert_LinkVB.Connection = Me.mycn
End Sub

'This property gets called from an outside class to return an object
Public ReadOnly Property SqlCmd_Insert_LinkVB
Get
Return m_SqlCmd_Insert_LinkVB
end get
End Property

End Class

Hope this helps
Chris
Ok, I see the problem now that you describe it this way. I first call
a sub named, InitailizeDatabaseConnections()

In the sub I do this:

Me.SqlCmd_Insert_LinkVB.CommandText = "dbo.[Insert_LinkVB]"
Me.SqlCmd_Insert_LinkVB.CommandType =
System.Data.CommandType.StoredProcedure
Me.SqlCmd_Insert_LinkVB.Connection = Me.mycn

In the watches, I see that Me.SqlCmd_Insert_LinkVB does have values.
Once I leave InitailizeDatabaseConnections(), Me.SqlCmd_Insert_LinkVB
is set to Nothing. I've moved the above declarations out of the sub
and into the class. However, I declare the parameters within the sub:

Me.SqlCmd_Insert_LinkVB.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@message_id",
System.Data.SqlDbType.Int, 4))

The above is just one parameter declaration but it keeps loosing its
value after I move out of InitailizeDatabaseConnections(). I must do
the assignments with a sub. How else can this be done so other subs
will have access to the params?

I'd like to declare all the params in one sub then do assignments to
the params in another sub.

Thanks,
Brett

"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com>
wrote in message The error "Object reference not set to an instance of an object"
always means (afaik) that you are referencing an object that is set to
nothing. You are correct that you create a new version of Class1 and
MyCurrentClass is set to something, that is not what the runtime is
complaining about. It is complaining that the object returned by
MyCurrentClass.SqlCmd_Insert_LinkVB is set to nothing.

So you need to find out why the SqlCmd_Insert_LinkVB method returns
nothing.

Hope this helps.
Chris



I'm not sure why I keep getting this error, "Object reference not set
to an instance of an object".

Private Function somefunction() as string
Dim MyCurrentClass As New Class1

Try
For i As Integer = 0 To LinkResults.Length - 1
With MyCurrentClass.SqlCmd_Insert_LinkVB
.CommandType =
System.Data.CommandType.StoredProcedure 'error occurs here

-- do something --
End Function

The above code is in Class1. If there is a problem with the
reference to the object instance, why doesn't this line throw an
error at
With MyCurrentClass.SqlCmd_Insert_LinkVB

The new class has been created, which means there is an instance
available. What am I doing wrong?

Thanks,
Brett
 
How is the property called:

Public ReadOnly Property SqlCmd_Insert_LinkVB
Get
Return m_SqlCmd_Insert_LinkVB
end get
End Property

Do I just reference:
Me.SqlCmd_Insert_LinkVB.CommandType =
System.Data.CommandType.StoredProcedure

or is some type of initialization needed?

Thanks,
Brett

Chris said:
Two questions.

1: Why are you using the WithEvents keyword. This is fine if you are
consuming event fired byt he object, but I doubt you are.

2: Why use friends? I assume you are doing this so objects outside of
the class can access the variable. This is fine, but I prefer the way I
showed you in my example by declaring the object as private and then
giving access to it in a method. This lets you make changes in the future
easier.

Chris

Brett said:
I have it working now. I needed to declare this way in the public class
area:

Friend WithEvents SqlCmd_Insert_LinkVB As
System.Data.SqlClient.SqlCommand

Then in the sub, I do this:
Me.SqlCmd_Insert_LinkVB = New System.Data.SqlClient.SqlCommand

Thanks,
Brett

Brett said:
That's what I'm doing - declaring them as private in the class. I
haven't created the initializeDatabaseConnection() sub yet. I have
everything in one sub for simplicity. Now I keep getting this error
"Exception has been thrown by the target of an invocation".

on this line
.ExecuteNonQuery()

Here is the code:
With SqlCmd_spamBlacklist_Insert
.CommandType =
System.Data.CommandType.StoredProcedure
.Parameters("@messageid").Value =
messageid
.Parameters("@listname").Value = ListName
.Parameters("@listurl").Value = ListURL
.ExecuteNonQuery()
End With

It's a very generic error. How do I go about debugging it?

Thanks,
Brett


"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com>
wrote in message Well you are doing more than you describe in the sub. Where do you
declare SqlCmd_Insert_LinkVB. Since you didnt show enough code I can't
tell you what you are doing wrong. I will give you an example of how
to make it work though.


'Note: I typed this all in here, syntax here will occur, not tested
Public Class Class1

Private m_SqlCmd_Insert_LinkVB as SqlCommand 'This is the local
variable to the class

Private Sub InitailizeDatabaseConnections
'This creates a new version of the command object.
m_SqlCmd_Insert_LinkVB = New SqlCommand

'Now the command object is vaild, load up your settings
Me.m_SqlCmd_Insert_LinkVB.CommandText = "dbo.[Insert_LinkVB]"
Me.m_SqlCmd_Insert_LinkVB.CommandType =
System.Data.CommandType.StoredProcedure
Me.m_SqlCmd_Insert_LinkVB.Connection = Me.mycn
End Sub

'This property gets called from an outside class to return an object
Public ReadOnly Property SqlCmd_Insert_LinkVB
Get
Return m_SqlCmd_Insert_LinkVB
end get
End Property

End Class

Hope this helps
Chris
Ok, I see the problem now that you describe it this way. I first call
a sub named, InitailizeDatabaseConnections()

In the sub I do this:

Me.SqlCmd_Insert_LinkVB.CommandText = "dbo.[Insert_LinkVB]"
Me.SqlCmd_Insert_LinkVB.CommandType =
System.Data.CommandType.StoredProcedure
Me.SqlCmd_Insert_LinkVB.Connection = Me.mycn

In the watches, I see that Me.SqlCmd_Insert_LinkVB does have values.
Once I leave InitailizeDatabaseConnections(), Me.SqlCmd_Insert_LinkVB
is set to Nothing. I've moved the above declarations out of the sub
and into the class. However, I declare the parameters within the sub:

Me.SqlCmd_Insert_LinkVB.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@message_id",
System.Data.SqlDbType.Int, 4))

The above is just one parameter declaration but it keeps loosing its
value after I move out of InitailizeDatabaseConnections(). I must do
the assignments with a sub. How else can this be done so other subs
will have access to the params?

I'd like to declare all the params in one sub then do assignments to
the params in another sub.

Thanks,
Brett

"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com>
wrote in message The error "Object reference not set to an instance of an object"
always means (afaik) that you are referencing an object that is set
to nothing. You are correct that you create a new version of Class1
and MyCurrentClass is set to something, that is not what the runtime
is complaining about. It is complaining that the object returned by
MyCurrentClass.SqlCmd_Insert_LinkVB is set to nothing.

So you need to find out why the SqlCmd_Insert_LinkVB method returns
nothing.

Hope this helps.
Chris



I'm not sure why I keep getting this error, "Object reference not
set to an instance of an object".

Private Function somefunction() as string
Dim MyCurrentClass As New Class1

Try
For i As Integer = 0 To LinkResults.Length - 1
With MyCurrentClass.SqlCmd_Insert_LinkVB
.CommandType =
System.Data.CommandType.StoredProcedure 'error occurs here

-- do something --
End Function

The above code is in Class1. If there is a problem with the
reference to the object instance, why doesn't this line throw an
error at
With MyCurrentClass.SqlCmd_Insert_LinkVB

The new class has been created, which means there is an instance
available. What am I doing wrong?

Thanks,
Brett
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top