DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Thread: LINQ Query with a Null Reference Exception

  1. #1
    Join Date
    Aug 2006
    Posts
    9

    LINQ Query with a Null Reference Exception

    Code:
    Dim strAuthorSearch As String
                    strAuthorSearch = "http://isbndb.com/api/authors.xml?access_key=CYGCL3GT&index1=name&value1=" & Me.txtSearchTerm.Text
                    Clipboard.SetText(strAuthorSearch)
                    xResults = XElement.Load(strAuthorSearch)
                    Dim authors = From retwriter In xResults.Descendants("AuthorData") Select AuthorID = xResults.Attribute("person_id").Value, Name = Me.txtSearchTerm.Text
    
                    For Each retwriter In authors
                        strSearchKey = retwriter.AuthorID
                    Next
                    strURLSearch = strURLSearch & "person_id"
                    xResults = Nothing
    And on a Search of Mark Twain here's what I get back.
    Code:
    <ISBNdb server_time="2008-12-28T02:16:11Z">
    −
    <AuthorList total_results="5" page_size="10" page_number="1" shown_results="5">
    −
    <AuthorData person_id="mark_twain_the_author">
    <Name>Mark Twain The Author</Name>
    </AuthorData>
    −
    <AuthorData person_id="twain_mark_a01">
    <Name>TWAIN MARK</Name>
    </AuthorData>
    −
    <AuthorData person_id="mark_twain_media">
    <Name>Mark Twain Media</Name>
    </AuthorData>
    −
    <AuthorData person_id="bancroft_library_mark_twain_pr">
    <Name>Bancroft Library Mark Twain Project</Name>
    </AuthorData>
    −
    <AuthorData person_id="twain_mark">
    <Name>Twain, Mark</Name>
    </AuthorData>
    </AuthorList>
    </ISBNdb>
    But I'm getting a NullReferenceError
    Last edited by Hack; 12-30-2008 at 09:17 AM. Reason: Added Code Tags

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Do you have blank, or empty records, that it could be hitting?

  3. #3
    Join Date
    Aug 2006
    Posts
    9
    Don't think so. Just to be on the safe side I did a Clipboard.SetText with the URL and then brought it up in Firefox. It seemed to be accurate. I was just wondering if I was misspelling something.

  4. #4
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Quote Originally Posted by pueblonative View Post
    I did a Clipboard.SetText with the URL and then brought it up in Firefox. It seemed to be accurate.
    So are you having the problem based on the browser you are using (I hate those!)?

  5. #5
    Join Date
    Aug 2006
    Posts
    9
    Don't think so either. The XML I posted up is what I'm getting from the isbndb.com in both Firefox AND directly through my VB app.

  6. #6
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Ok.

    However, if you are getting a Null exception, that means something is coming back as Null.

    You are doing a search on Author Names. That would indicate there is an author name that is Null or one or more of the person_Id pieces of information is Null.

    Does this error occur regardless of the author you are doing the search for or only when you do Mark Twain?

  7. #7
    Join Date
    Aug 2006
    Posts
    9
    Regardless. I even just to be on the safe side did a find and replace on all the spaces in the search term. No go.

  8. #8
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Lets look at this.

    http://isbndb.com/api/authors.xml?access_key=CYGCL3GT&index1=name&value1=" & Me.txtSearchTerm.Text

    If you hard code values for "name", "value1" and "txtSearchTerm.Text", do you have any problems running it?

  9. #9
    Join Date
    Aug 2006
    Posts
    9
    Yep, same problem.

  10. #10
    Join Date
    Aug 2006
    Posts
    9
    Quote Originally Posted by pueblonative View Post
    Yep, same problem.
    BTW, here's the documentation on the XML for that site. Maybe it helps.

    http://isbndb.com/docs/api/54-authors.html

  11. #11
    Join Date
    Mar 2004
    Location
    UK
    Posts
    31
    The problem is in the line:

    Code:
    Select AuthorID = xResults.Attribute("person_id").Value
    The xResults variable refers to the root of the document, which doesn't have a "person_id" attribute. Try changing the code to:

    Code:
    Select AuthorID = retwriter.Attribute("person_id").Value

  12. #12
    Join Date
    Aug 2006
    Posts
    9
    Thank you, thank you, thank you! That did the trick.

Similar Threads

  1. Get start and end date from a weeknumber
    By pieneman in forum Database
    Replies: 7
    Last Post: 08-15-2008, 01:09 PM
  2. Replies: 1
    Last Post: 07-20-2006, 12:46 AM
  3. Getting a large number of identities
    By Colin McGuigan in forum Database
    Replies: 12
    Last Post: 04-15-2002, 07:43 AM
  4. reg...select count distinct....query
    By Mohan Ekambaram in forum Database
    Replies: 0
    Last Post: 12-09-2001, 12:56 PM
  5. FOR XML EXPLICIT
    By reddy in forum XML
    Replies: 1
    Last Post: 06-04-2001, 02:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center