Results 1 to 17 of 17

Thread: Strange Error

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    429

    Strange Error

    hi,

    I am creating a simple application which will fetch google search results URL in list box. I am using URI function.
    Code:
     Dim results As Uri()
            results = c.GetResults("asd")
            For Each result As Uri In results
                ListBox1.Items.Add(result)
            Next
    Here i am getting a null reference Error. i tried to use the keyword New but still same error. i can sort it out. plz help. i have another class and its object is c. I don have any error in coding. just runtime error.

  2. #2
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Strange Error

    What is c? Where is the null reference thrown (i.e. which line)? Does Uri actually contain multiple items?

    I've never used a Uri object before but it seems odd that it can both old one and multiple pieces of data. For instance, you usually do a For Each on a String inside of a List(Of String).

    What does GetResults return?
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Strange Error

    Shouldn't your declaration be:

    Code:
    Dim results() As Uri
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    429

    Re: Strange Error

    Quote Originally Posted by Kasracer
    What is c? Where is the null reference thrown (i.e. which line)? Does Uri actually contain multiple items?

    I've never used a Uri object before but it seems odd that it can both old one and multiple pieces of data. For instance, you usually do a For Each on a String inside of a List(Of String).

    What does GetResults return?
    GetResults Return a List of URLs retrived from webpage.

    c is a object or another class which has Getresults. Null Reference is thrown
    Code:
      results = c.GetResults("asd")  'Here

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    429

    Re: Strange Error

    Quote Originally Posted by Pradeep1210
    Shouldn't your declaration be:

    Code:
    Dim results() As Uri

    Is there any declaration mistake? i tried declaring both globally and locally.

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Strange Error

    Quote Originally Posted by yogesh12
    Yeah i have Declared it as Global Variable.
    No... I meant the position of brackets.

    Dim results() As Uri
    Dim results As Uri()

    These two declarations mean two different things.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Strange Error

    Quote Originally Posted by yogesh12
    GetResults Return a List of URLs retrived from webpage.

    c is a object or another class which has Getresults. Null Reference is thrown
    Code:
      results = c.GetResults("asd")  'Here
    So it looks like we have two issues then. First off, results needs to be declared as a List(Of Uri). Or an array (bah!).

    Secondly, if you're getting that error on that line, it sounds like c is null. Where is your declaration of c?
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    429

    Re: Strange Error

    Quote Originally Posted by Pradeep1210
    No... I meant the position of brackets.

    Dim results() As Uri
    Dim results As Uri()

    These two declarations mean two different things.
    I got Error. Array cannot be Declared as New

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    429

    Re: Strange Error

    Quote Originally Posted by Kasracer
    So it looks like we have two issues then. First off, results needs to be declared as a List(Of Uri). Or an array (bah!).

    Secondly, if you're getting that error on that line, it sounds like c is null. Where is your declaration of c?
    Code:
      Uri[] results = Googler.GetResults(txtSearchstring.Text);
                foreach (Uri result in results) {
                    lstResults.Items.Add(result);
    This is c# Code i converted it. Result is an Array of URI. (List of URLS).

  10. #10
    Addicted Member
    Join Date
    Dec 2006
    Location
    Between Try & Catch
    Posts
    249

    Re: Strange Error

    You may not have any red squigglies (otherwise you wouldn't even be able to compile it), but there is something your code is not handling.

    And where is your try/catch?

    Your code should look more like this:
    vb Code:
    1. Dim results() As Uri
    2. Try
    3.           results = c.GetResults("asd")
    4. If Not results = Nothing Then
    5.         For Each result As Uri In results
    6.             ListBox1.Items.Add(result)
    7.          Next
    8.   End If
    9. Catch ex as Exception
    10. 'this is only for testing, never show the user app error messages
    11. Messagebox.Show(ex.ToString)
    12. End Try

    When you step through the code, it should skip from the If to the End If, based on the error you are getting. Any other exceptions will be caught.

    For future reference, don't show exception messages to the user. Most won't understand anything on the messages. You should handle the error, preferrably by writing it to a log, and show a message notifying the user there has been an error (how specific you want to be depends on the detail of your error handling).
    If my post helped you, please rate it!

    Languages: VB/ASP.NET 2005, C# 2008,VB6
    Databases: Oracle (knowledge not currently in use), DB2

    FROM Customers
    WHERE We_Know_What_We_Want <> DB.Null
    SELECT *
    0 rows returned

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    429

    Re: Strange Error

    Quote Originally Posted by Blakk_Majik
    You may not have any red squigglies (otherwise you wouldn't even be able to compile it), but there is something your code is not handling.

    And where is your try/catch?

    Your code should look more like this:
    vb Code:
    1. Dim results() As Uri
    2. Try
    3.           results = c.GetResults("asd")
    4. If Not results = Nothing Then
    5.         For Each result As Uri In results
    6.             ListBox1.Items.Add(result)
    7.          Next
    8.   End If
    9. Catch ex as Exception
    10. 'this is only for testing, never show the user app error messages
    11. Messagebox.Show(ex.ToString)
    12. End Try

    When you step through the code, it should skip from the If to the End If, based on the error you are getting. Any other exceptions will be caught.

    For future reference, don't show exception messages to the user. Most won't understand anything on the messages. You should handle the error, preferrably by writing it to a log, and show a message notifying the user there has been an error (how specific you want to be depends on the detail of your error handling).
    Mate Code is not correct.

  12. #12
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Strange Error

    What is the return type of GetResults?
    Declare the results variable of that type.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    429

    Re: Strange Error

    Quote Originally Posted by Pradeep1210
    What is the return type of GetResults?
    Declare the results variable of that type.
    Code:
     Public Function GetResults(ByVal query As String) As Uri()
    tell me if i am wrong.

  14. #14
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Strange Error

    Put a breakpoint on the line that throws the exception. Highlight c and press Shift+F9, is it Nothing? If it is, then you have a problem there. Whether or not c is the issue can be dispensed with by this simple check. The declaration of results shouldn't impact it.

    The second test is to highligh the entire c.GetResults() portion, and use Shift+F9. If c is not nothing, and you get the same exception when you look at the whole thing, then that would be interesting, but do the tests first.
    My usual boring signature: Nothing

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    429

    Re: Strange Error

    Quote Originally Posted by Shaggy Hiker
    Put a breakpoint on the line that throws the exception. Highlight c and press Shift+F9, is it Nothing? If it is, then you have a problem there. Whether or not c is the issue can be dispensed with by this simple check. The declaration of results shouldn't impact it.

    The second test is to highligh the entire c.GetResults() portion, and use Shift+F9. If c is not nothing, and you get the same exception when you look at the whole thing, then that would be interesting, but do the tests first.

    Great Man. I am really a stupid. i din used "New" Keyword while declaring object... Thanks Man. I also want to know how to protect my software online? via online activation or something like that..

  16. #16
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Strange Error

    That should be the subject of a new thread, as you will get better eyes on the topic with a subject that is more appropriate to the current question. No cost to starting new threads for new topics. Though this thread should be marked resolved.

    I might also add that you shouldn't go into that new question with much optimism. There have been a handful of threads kicking around lately on that subject, and nobody has said "this will work." It has always been more of, "this is how easy it would be to defeat that technique." With such a response to every technique I have seen, you end up asking yourself just what it is you really NEED to protect. You'll get advice, just maybe not a definitive, affirmative, answer.
    My usual boring signature: Nothing

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    429

    Re: Strange Error

    Quote Originally Posted by Shaggy Hiker
    That should be the subject of a new thread, as you will get better eyes on the topic with a subject that is more appropriate to the current question. No cost to starting new threads for new topics. Though this thread should be marked resolved.

    I might also add that you shouldn't go into that new question with much optimism. There have been a handful of threads kicking around lately on that subject, and nobody has said "this will work." It has always been more of, "this is how easy it would be to defeat that technique." With such a response to every technique I have seen, you end up asking yourself just what it is you really NEED to protect. You'll get advice, just maybe not a definitive, affirmative, answer.
    Thanks Man..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width