|
-
May 1st, 2013, 10:46 PM
#1
Thread Starter
Junior Member
[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:
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
-
May 2nd, 2013, 08:08 AM
#2
Thread Starter
Junior Member
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 ....
-
May 2nd, 2013, 08:56 AM
#3
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")
-
May 2nd, 2013, 09:08 AM
#4
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.
-
May 2nd, 2013, 09:11 AM
#5
Thread Starter
Junior Member
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
-
May 2nd, 2013, 09:12 AM
#6
Re: XDocument - Object reference not set to an instance of an object
 Originally Posted by Moritz83
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.
-
May 2nd, 2013, 10:16 AM
#7
Thread Starter
Junior Member
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?
-
May 2nd, 2013, 10:31 AM
#8
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.
-
May 2nd, 2013, 10:54 AM
#9
Thread Starter
Junior Member
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
-
May 2nd, 2013, 11:38 AM
#10
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:
Dim materialXDoc = XDocument.Load("C:\Temp\Material.xml")
Dim groupXDoc = XDocument.Load("C:\Temp\Zerspanungsgruppen.xml")
Dim searchValue = "Aluminium"
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
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.
-
May 2nd, 2013, 11:52 AM
#11
Thread Starter
Junior Member
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
-
May 2nd, 2013, 03:11 PM
#12
Thread Starter
Junior Member
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 ...
-
May 2nd, 2013, 03:11 PM
#13
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
-
May 2nd, 2013, 03:32 PM
#14
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|