Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7814415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:11:14+00:00 2026-06-02T05:11:14+00:00

This is a simple stuff but driving me really crazy now. Spent hours on

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:11:15+00:00Added an answer on June 2, 2026 at 5:11 am

    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 2—which 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(',')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, this is for homework about hashtables, but this is the simple stuff I
I'm trying to do some really simple stuff in C++, but I can't find
I have this simple function which I pass in an array of strings: function
This might sound strange at first but its rather simple. I have a wcf
This is probably a really stupid MSBuild question but if I have <ItemGroup> <Dll
NHibernate is driving me 'Nuts'!!! I have the following mapping. Which returns this error
I wrote a quick generic linked list, simple stuff. But I have a bug
Consider this simple example (which displays in red): echo -e \033[31mHello World\033[0m It displays
This should be so simple, but I'm making heavy weather of it. Div region
This is driving me crazy. I read the Sun's tutorial regarding the creation of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.