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

  • Home
  • SEARCH
  • 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 7958355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:24:36+00:00 2026-06-04T04:24:36+00:00

I have some problems with StreamReader. Firstly, below, is my simple code: using (StreamReader

  • 0

I have some problems with StreamReader. Firstly, below, is my simple code:

using (StreamReader reader = new StreamReader("Content/Levels/" + mapName + ".txt"))
{
    for (int i = 0; i < 20; i++)
        for (int j = 0; j < 36; j++)
        {
            string[] objLoc = reader.ReadLine().Split(',');
            map[i, j] = Convert.ToInt32(objLoc[j]);
        }
}

So, I have a text file which has rows and columns, just like an array. Each position holds an integer. Those integers are delimited by , chars.

I want to read each character from the position within the text file, and then convert it to an actual integer and add it to a separate array. I’ll read from that array to build the map after the code I’ve shown you.

Being new to C# and programming, I assume that my code actually reads every position from a line using that Split method, and then I use the read char to insert it in the map array.

Am I doing it right? At the moment, I’m getting an exception:

NullReferenceException was unhandled: Object reference not set to an instance of an object.

I’ve read the documentation from MS also. Stumbled upon numerous similar problems, but none fixed my issue.

Any help would be highly appreciated!

  • 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-04T04:24:37+00:00Added an answer on June 4, 2026 at 4:24 am

    You are reading a whole new line in your inner loop, which means you run out of lines fast. You need to read a new line in the outer loop, and loop throught the result of the split (the inidividual elements) in the inner loop

    Try something like

    using (StreamReader reader = new StreamReader("Content/Levels/" + mapName + ".txt"))
    {
      for (int i = 0; i < 20; i++) {
        string[] objLoc = reader.ReadLine().Split(',')
        for (int j = 0; j < 36; j++) {
          map[i, j] = Convert.ToInt32(objLoc[j]);
        }
      }
    }
    

    Note: you will need to check for errors in case the line does not contain enough elements or the file is too short. The conversion to int might fail as well

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some problems sending mails through SMTP using Spring's MailSender interface and the
i have some problems with rowspan: var doc1 = new Document(); doc1.SetPageSize(PageSize.A4.Rotate()); string path
I have some problems to start programming using cocos2D on linux + android. I
I have some problems with a java app i'm developing, i'm using HtmlCleaner 2.2
i'm using regex to get some information from a website, i have this code:
Have some problems with NSIS uninstall code. RMDir /r $SMPROGRAMS\${PRODUCT_NAME} In Windows 7 this
I have some problems with OpenCV s cvCanny(...) and the Image data types it
I have some problems sending an id though jquery. I have a form select
I have some problems with mod_rewrited at .httacess. We have created a website, the
I have some problems with controlling a while loop inside an event structure. Say

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.