-1

I am trying to split the string tab or space. This my code:

string s = File.ReadLine();

string[] str =s.Split(new Char[] { ' ', '\t'});"

"Soccer SOCCER 11" //this is the content of variable s.

But I am getting the following error:

Object reference not set to an instance of an object

What am I doing wrong? Please help me to debug this.

CyanCoding
  • 836
  • 7
  • 25
Kushal
  • 17
  • 6

1 Answers1

-2

Your line of code for the split is correct, i just tried it and it works as expected.

The error must be when reading from the file, i saw your comment about writing to a file before reading, try to call the Dispose() method on the StreamWriter before trying to read from it, otherwise you can get errors like this.

Leonardo Menezes
  • 466
  • 5
  • 11
  • string Path = ConfigurationManager.AppSettings["SportPath"]; using (System.IO.StreamReader File = new System.IO.StreamReader(Path)) { while ((File.ReadLine()) != null) { string s = File.ReadLine(); string[] str =s.Split(new Char[] {' ', '\t'}); UpdateRecords.Sport(str);} this the code I am using to read the text file. – Kushal Apr 15 '18 at 00:09
  • Now i get the error, you are reading two times the line, on the second read you get the error. Your code should be: string Path = ConfigurationManager.AppSettings["SportPath"]; using (System.IO.StreamReader File = new System.IO.StreamReader(Path)) { string readString = File.ReadLine(); while (readString != null) { string[] str = readString.Split(new Char[] { ' ', '\t' }); UpdateRecords.Sport(str); } } – Leonardo Menezes Apr 15 '18 at 00:16
  • Yes, I was reading the file twice I fixed it and its working.Thank you. – Kushal Apr 15 '18 at 00:23