Results 1 to 14 of 14

Thread: [RESOLVED] XDocument - Object reference not set to an instance of an object

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    28

    Resolved [RESOLVED] XDocument - Object reference not set to an instance of an object

    Hi,

    got a little problem with XDocument. I use XDocument to get a value from an XML document. Everything is working fine but I get an error (Object reference not set to an instance of an object) when I try to debug the project...but it's working when I click next. This is my code:

    Code:
    Dim cbt As String
    I do assign the value from a combobox and I can display cbt so the value seems to be assigned correctly
    Code:
    cbt = ComboBox1.Text
    Then I start the sub with XDocument
    Code:
    Dim materialXDoc = XDocument.Load("C:\Test\Material.xml")
            Dim groupXDoc = XDocument.Load("C:\Test\Zerspanungsgruppen.xml")
    
            'Hard coded in this example you said you get it from a combobox selection
            Dim searchValue = cbt
    
            'Search for a specific name in materialXDoc and get the value of the Zerpspanungsgruppe node
            'Will return Nothing if the search value is not found
            Dim searchedgroup = (From n In materialXDoc...<Material>
                                             Where n.@name = searchValue
                                             Select n.<Zerspanungsgruppe>.Value).SingleOrDefault()
    
            'Search the Zerpspanungsgruppe.xml file and return the spezSchnittkraft and AnstiegswerMc node values
            'Will return nothing if the search value is not found
            Dim correspondingValues = (From n In groupXDoc...<Zerspanungsgruppe>
                                      Where n.@name = searchedgroup
                                      Select New With {.SpezSchnittkraft = CInt(n.<spezSchnittkraft>.Value), .AnstiegswertMc = CDbl(n.<AnstiegswertMc>.Value)}).SingleOrDefault()
            Dim ssk = correspondingValues.SpezSchnittkraft
            Dim amc = correspondingValues.AnstiegswertMc
            Label3.Text = ssk
            Label4.Text = amc
    The error (Object ...) is pointing to "Dim ssk = correspondingValues.SpezSchnittkraft" but yeah, when I click on next and ignore the error everything is working

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    28

    Re: XDocument - Object reference not set to an instance of an object

    I really don't get it! The cbt variable itself is working but it seems it's not used for the search.
    There is definitely no typo inside the XML files ....

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,375

    Re: XDocument - Object reference not set to an instance of an object

    Generally you get that error because you never declare a new instance of that type. Try adding something along these lines:
    Code:
    Dim materialXDoc As New XDocument
    materialXDoc.Load("C:\Test\Material.xml")
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: XDocument - Object reference not set to an instance of an object

    Put a breakpoint on the Dim ssk line and check what's in the searchedgroup and correspondingValues objects.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    28

    Re: XDocument - Object reference not set to an instance of an object

    I think I don't get it. Isn't this all the same instance?
    ie I load file1 and file2 before I start searching for something?

    XDocument is something I never used before

    @Matt
    will try it at home in a few minutes

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,375

    Re: XDocument - Object reference not set to an instance of an object

    Quote Originally Posted by Moritz83 View Post
    I think I don't get it. Isn't this all the same instance?
    ie I load file1 and file2 before I start searching for something?

    XDocument is something I never used before

    @Matt
    will try it a home in a few minutes
    Neither have I, but it seems like what you're doing is creating an instance of an object, just not a new instance of that object. That's when the Object reference not set to an insance of an object generally comes about.

    Edit -
    Try to compile this:
    Code:
    Dim fooList As List(Of Integer)
    fooList.AddRange({1, 2, 3, 4})
    Then correct it like this:
    Code:
    Dim fooList As New List(Of Integer)
    fooList.AddRange({1, 2, 3, 4})
    Last edited by dday9; May 2nd, 2013 at 09:25 AM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    28

    Re: XDocument - Object reference not set to an instance of an object

    @dday9
    I see ... in the first example I will get an error and the second one is working cause of the new list ... like a restart if I got it right.
    So do I have to put the second XDocument into a new sub?

    @MattP
    searchedgroup is giving me the correct value, how can I get the "correspondingvalues" values?

  8. #8
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: XDocument - Object reference not set to an instance of an object

    So I'm assuming you're not getting a match in groupXDoc. Can you post the structure of it and the value you're expecting to find?
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    28

    Re: XDocument - Object reference not set to an instance of an object

    Well if I click on next within the error dialog I get a working version of my program :/

    Material.xml
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <Materialien>
    <Material name="Aluminium">
    <Zerspanungsgruppe>P2</Zerspanungsgruppe>
    <EModul>70000</EModul>
    </Material>
    <Material name="Inconel">
    <Zerspanungsgruppe>P3</Zerspanungsgruppe>
    <EModul>70000</EModul>
    </Material>
    </Materialien>
    Zerspanungsgruppen.xml
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <Zerspanungsgruppen>
      <Zerspanungsgruppe name="P1">
        <spezSchnittkraft>5</spezSchnittkraft>
        <AnstiegswertMc>0.1</AnstiegswertMc>
      </Zerspanungsgruppe>
      <Zerspanungsgruppe name="P2">
        <spezSchnittkraft>10</spezSchnittkraft>
        <AnstiegswertMc>0.2</AnstiegswertMc>
      </Zerspanungsgruppe>
      <Zerspanungsgruppe name="P3">
        <spezSchnittkraft>15</spezSchnittkraft>
        <AnstiegswertMc>0.3</AnstiegswertMc>
      </Zerspanungsgruppe>
    </Zerspanungsgruppen>
    All material names can be choosen in a combobox.
    i.e.: I choose Aluminium -> P2 goes to the variable cbt -> cbt is used as search value -> it should return 10 and 0.2

  10. #10
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: XDocument - Object reference not set to an instance of an object

    I'm not getting any errors with those files when I search for "Aluminium".

    If you're putting "P2" into the varaible cbt you'll have a problem. The name attribute for the Material node will contain "Aluminium" and "P2" is the value you want to get out of your 1st search. If you already know that Zerspanungsgruppe is "P2" then you don't need to search the Material file and you can plug it in to the value you're searching for in groupXDoc.

    vb.net Code:
    1. Dim materialXDoc = XDocument.Load("C:\Temp\Material.xml")
    2.         Dim groupXDoc = XDocument.Load("C:\Temp\Zerspanungsgruppen.xml")
    3.  
    4.         Dim searchValue = "Aluminium"
    5.  
    6.         Dim searchedgroup = (From n In materialXDoc...<Material>
    7.                                          Where n.@name = searchValue
    8.                                          Select n.<Zerspanungsgruppe>.Value).SingleOrDefault()
    9.  
    10.         Dim correspondingValues = (From n In groupXDoc...<Zerspanungsgruppe>
    11.                                   Where n.@name = searchedgroup
    12.                                   Select New With {
    13.                                       .SpezSchnittkraft = CInt(n.<spezSchnittkraft>.Value),
    14.                                       .AnstiegswertMc = CDbl(n.<AnstiegswertMc>.Value)
    15.                                   }).SingleOrDefault()
    16.  
    17.         Dim ssk = correspondingValues.SpezSchnittkraft
    18.         Dim amc = correspondingValues.AnstiegswertMc

    There's no reason to create the ssk and amc variable if you're just going to assign them to the Text properties of Labels.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    28

    Re: XDocument - Object reference not set to an instance of an object

    the labels are just there for testing purpose
    P2 was just an example, each material has a different "Zerspanungsgruppe" that's why I want to use cbt

    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            _dsData.ReadXml("C:\Test\Material.xml")
            ComboBox1.DataSource = _dsData.Tables("Material")
            ComboBox1.DisplayMember = "name"
        End Sub
    
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            myDataRow = DirectCast(ComboBox1.SelectedItem, DataRowView)
            cbt = ComboBox1.Text
            zid = myDataRow(0)
            Label2.Text = zid
            ChangeValue()
        End Sub
    
        Public Sub ChangeValue()
            Dim materialXDoc = XDocument.Load("C:\Test\Material.xml")
            Dim groupXDoc = XDocument.Load("C:\Test\Zerspanungsgruppen.xml")
    
            Dim searchValue = cbt
    
            Dim searchedgroup = (From n In materialXDoc...<Material>
                                             Where n.@name = searchValue
                                             Select n.<Zerspanungsgruppe>.Value).SingleOrDefault()
    
            Dim correspondingValues = (From n In groupXDoc...<Zerspanungsgruppe>
                                      Where n.@name = searchedgroup
                                      Select New With {
                                          .SpezSchnittkraft = CInt(n.<spezSchnittkraft>.Value),
                                          .AnstiegswertMc = CDbl(n.<AnstiegswertMc>.Value)
                                      }).SingleOrDefault()
    
            Dim ssk = correspondingValues.SpezSchnittkraft
            Dim amc = correspondingValues.AnstiegswertMc
    My bad, I don't use "P2", I use cbt and cbt is the name of the material. If I use "Aluminium" as searching value everything is alright so there must be a problem with cbt or it's value although the value of it is correct (tested with a label).

    EDIT
    This version is working:
    Code:
            Dim value1 = (From n In groupXDoc...<Zerspanungsgruppe>
                                      Where n.@name = searchedgroup
                                      Select n.<spezSchnittkraft>.Value).SingleOrDefault()
            Dim value2 = (From n In groupXDoc...<Zerspanungsgruppe>
                              Where n.@name = searchedgroup
                              Select n.<AnstiegswertMc>.Value).SingleOrDefault()
    Last edited by Moritz83; May 2nd, 2013 at 12:44 PM. Reason: additional information

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    28

    Re: XDocument - Object reference not set to an instance of an object

    Playing around for 2 hours now and I think I got the problem.
    Your code is working perfectly as long as I use
    Code:
    Dim searchValue = "Aluminium"
    or
    Code:
    Dim searchValue = "Inconel"
    but when I try to use cbt (Variable) instead something goes wrong although "searchedgroup" is returning the correct value (like P1, P2 or something else).
    My posted solution is working but it's not very good :/


    The "error" is in here
    Code:
                                      Select New With {
                                          .SpezSchnittkraft = CInt(n.<spezSchnittkraft>.Value),
                                          .AnstiegswertMc = CDbl(n.<AnstiegswertMc>.Value)
                                      }).SingleOrDefault()
    because I replaced the "New With" procedure with the one we have in "searchedgroup" with just 1 search criteria and it's working.


    I know I am annoying but I don't want to give up that fast ...

  13. #13
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: XDocument - Object reference not set to an instance of an object

    so why aren't you doing that in the original code?

    Code:
            Dim value1 = (From n In groupXDoc...<Zerspanungsgruppe>
                                      Where n.@name = searchedgroup
                                      Select n.<spezSchnittkraft>.Value).SingleOrDefault()
            Dim value2 = (From n In groupXDoc...<Zerspanungsgruppe>
                              Where n.@name = searchedgroup
                              Select n.<AnstiegswertMc>.Value).SingleOrDefault()
    Note the difference between working (above) and broken (below)
    Code:
            Dim searchedgroup = (From n In materialXDoc...<Material>
                                             Where n.@name = searchValue
                                             Select n.<Zerspanungsgruppe>.Value).SingleOrDefault()
    
            Dim correspondingValues = (From n In groupXDoc...<Zerspanungsgruppe>
                                      Where n.@name = searchedgroup
                                      Select New With {
                                          .SpezSchnittkraft = CInt(n.<spezSchnittkraft>.Value),
                                          .AnstiegswertMc = CDbl(n.<AnstiegswertMc>.Value)
                                      }).SingleOrDefault()
    see how in the first code you used searchedGroup in the selection of Value1... and then in the second query you used searchedgroup again... and NOT VALUE1 !!!! but in the broken code... you used searchValue to assign to searchedGroup... then used SearchedGroup in the second one...

    I suspect that if you changed it to
    Where n.@name = Value1 it will break again...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    28

    Re: XDocument - Object reference not set to an instance of an object

    that's it!
    My code was way too complexe

    Code:
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            myDataRow = DirectCast(ComboBox1.SelectedItem, DataRowView)
            zid = myDataRow(0)
            Label2.Text = ComboBox1.Text
            ChangeValue()
        End Sub
       
     Public Sub ChangeValue()
            Dim groupXDoc = XDocument.Load("C:\Test\Zerspanungsgruppen.xml")
            Dim correspondingValues = (From n In groupXDoc...<Zerspanungsgruppe>
                                      Where n.@name = zid
                                      Select New With {
                                          .SpezSchnittkraft = CInt(n.<spezSchnittkraft>.Value),
                                          .AnstiegswertMc = CDbl(n.<AnstiegswertMc>.Value)
                                      }).SingleOrDefault()
            Label3.Text = correspondingValues.SpezSchnittkraft
            Label4.Text = correspondingValues.AnstiegswertMc
        End Sub
    Inside the ComboBox1 sub I can already get the "Zerspanungsgruppe" value I need to get the 2 other values from "Zerspanungsgruppen.xml".
    So there is no need for
    Code:
            Dim searchedgroup = (From n In materialXDoc...<Material>
                                             Where n.@name = searchValue
                                             Select n.<Zerspanungsgruppe>.Value).SingleOrDefault()
    I think I have to reconsider my whole project and restart from zero with everything I know and learned.

    Thanks to all!

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