Janaki Ram

Janaki Ram

  • NA
  • 5
  • 1.1k

Object reference not set to an instance of an object

Nov 12 2017 12:34 PM
Hi All,
 
Can anyone one help me to rectify the error for the blow code for String type.
If i pass string directly it is working but if i  mention string value1= Console.ReadLine(); not executing.
 
error:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Program.Main () [0x0000c] in :0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Program.Main () [0x0000c] in :0  
 
C# code:
 
using System;
public class Program
{
public static void Main()
{
int sp = 0;
int ws = 0;
int di = 0;
string value1= Console.ReadLine();
foreach (char c in value1)
{
if (!char.IsLetterOrDigit(c))
{ sp++;
if(char.IsWhiteSpace(c))
{
sp--;
}
}
if (char.IsWhiteSpace(c))
{ ws++;
}
if (char.IsDigit(c))
{ di++;
}
}
Console.WriteLine("special "+sp);
Console.WriteLine("white "+ws);
Console.WriteLine("digit "+di);
}
}

Answers (2)