1

This is a simple stuff but driving me really crazy now. Spent hours on figuring this out which I have many many times before.

I am trying to read a parse xmlsimple doc. But I don't know why can't access elements by index number. I can't understand the problem, when I try this in the console it works, but not in actual code. It gives me this error on the view page:

undefined method `[]' for nil:NilClass

Code:

@i = 0
list =""
while @i <= 2
  puts xml
  a = parsed_items["Item"][@i]["ItemId"]
  list <<  a.to_s << ","
  @i += 1
end
puts list.to_s

If I do it by giving a int value manually in my code then it works:

a = parsed_items["Item"][0]["ItemId"] # it works with other exact code

Change to @i and not working:

a = parsed_items["Item"][@i]["ItemId"] # it does not work with other exact code

XML:

1.9.2p290 :013 > items = "<ItemList> <Item> <ItemId>123</ItemId> <ItemName>abc</ItemName> <ItemType>xyz</ItemType> <Status>bad</Status> </Item> <Item> <ItemId>456</ItemId> <ItemName>fgh</ItemName> <ItemType>nbv</ItemType> <Status>bad</Status> </Item> </ItemList>"
     => "<ItemList> <Item> <ItemId>123</ItemId> <ItemName>abc</ItemName> <ItemType>xyz</ItemType> <Status>bad</Status> </Item> <Item> <ItemId>456</ItemId> <ItemName>fgh</ItemName> <ItemType>nbv</ItemType> <Status>bad</Status> </Item> </ItemList>" 

1.9.2p290 :014 > parsed_items = XmlSimple.xml_in(items, { 'KeyAttr' => 'name' })
     => {"Item"=>[{"ItemId"=>["123"], "ItemName"=>["abc"], "ItemType"=>["xyz"], "Status"=>["bad"]}, {"ItemId"=>["456"], "ItemName"=>["fgh"], "ItemType"=>["nbv"], "Status"=>["bad"]}]} 

XML:

<ItemList>
  <Item>
    <ItemId>123</ItemId>
    <ItemName>abc</ItemName>
    <ItemType>xyz</ItemType>
    <Status>bad</Status>
  </Item>
  <Item>
    <ItemId>456</ItemId>
    <ItemName>fgh</ItemName>
    <ItemType>nbv</ItemType>
    <Status>bad</Status>
  </Item>
</ItemList>

2 Answers 2

2

Paraphrased, that error means "Hey, you put [] after something that was nil, but nil doesn't have that method!"

You only have 2 items in your array, so when @i gets to 2which is the third item in a 0-based list—the code parse_items["Item"][@i] is returning nil; when you try to then execute ["ItemId"] on that value you get the error you stated.

Simplest change to fix this:

while @i<2 # instead of <=2

Better change (let Ruby iterate for you):

list = ""
parsed_items["Item"].each do |item|
  list << item["ItemId"].to_s << ","
end
puts list

Even better change (let Ruby do your work for you):

puts parsed_items["Item"].map{ |item| item["ItemId"] }.join(',')
Sign up to request clarification or add additional context in comments.

Comments

0

For some reason you're defining an instance variable instead of a local one. Also conversing list into a string is completely unnecessary since it's a string from a very beginning. Working code should look somewhat like this:

i = 0
list =""
while i <= 2
  puts xml
  a = parsed_items["Item"][i]["ItemId"]
  list <<  a.to_s << ","
  i += 1
end
puts list

I strongly suggest you to read about different variable types.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.