0

'Button1

Dim ex1 As New Threading.Thread(AddressOf ex1_sub)

Dim ex2 As New Threading.Thread(AddressOf ex2_sub)

ex1.Start()
ex2.Start()

'End Sub

Sub ex1_sub()

Dim result_ex1 As IWebResult

Dim client_ex1 As New GwebSearchClient("http://www.google.com")

Dim results_ex1 As IList(Of IWebResult) = client_ex1.Search("love", 2)

For Each result_ex1 In results_ex1
MsgBox(result_ex1.Title)
Next

End Sub


Sub ex2_sub()
Dim result_ex2 As IWebResult

Dim client_ex2 As New GwebSearchClient("http://www.google.com")

Dim results_ex2 As IList(Of IWebResult) = client_ex2.Search("hate", 2)

For Each result_ex2 In results_ex2
MsgBox(result_ex2.Title)
Next

End Sub

It points either to results_ex2 or results_ex1 and tells me Object reference not set to an instance of an object.

How could I fix that ?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
eawedat
  • 409
  • 2
  • 8
  • 17

1 Answers1

0

If you can't guarantee that client_ex1.Search() or client_ex2.Search() always return a value, do simple check after function call to avoid Null reference error. For example :

.......
Dim results_ex2 As IList(Of IWebResult) = client_ex2.Search("hate", 2)
If results_ex2 Is Nothing Then
    Console.WriteLine("API returns no result")
Else
    For Each result_ex2 In results_ex2
        MsgBox(result_ex2.Title)
    Next
End If
.......
Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137
  • have you applied the same building block for `results_ex1`? In which line you get the error? – har07 Feb 23 '14 at 10:35
  • Yes I did , I get error in this line im results_ex2 As IList(Of IWebResult) = client_ex2.Search("hate", 2) – eawedat Feb 23 '14 at 10:37
  • It seems that `client_ex2` is `Nothing` instead of `results_ex2` that trigger the exception. Can you confirm that? – har07 Feb 23 '14 at 10:39
  • I think results_ex2 and results_ex2 both do have Things because If I comment one of threads above the problem will be solved. – eawedat Feb 23 '14 at 14:24