1

I'm new here and I tried looking through old questions but I am new to c# as well, and I am finding it difficult to solve my problem below:

if (File.Exists(@"C:\ESC\Impostazioni.txt"))
{
    TextReader lettore_file = new StreamReader(@"C:\ESC\Impostazioni.txt");
    String opzioni = lettore_file.ReadLine();

    int i;
    for (i = 0; i < opzioni.Length; i++) <----here, indicating "i=0"
    {
        if (opzioni[i] == '-')
        {
             char[] coloregenerale = new char[i];
             for (int j = 0; j < i; j++)
               coloregenerale[j] = opzioni[j];

           break;
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
maxpesa
  • 185
  • 10
  • What is the value of `opzioni`? I'm going to bet it is `null`. – gunr2171 Jul 29 '13 at 17:07
  • 1
    If the EOF is on the first line your `ReadLine` will return `null`. Be sure your file exists and contains text. – Pierre-Luc Pineault Jul 29 '13 at 17:09
  • Have you tried setting a breakpoint and debugging through the code to pinpoint where exactly it is failing? If you can tell us where exactly it is failing it will be easier to help you. – Ciaran Gallagher Jul 29 '13 at 17:09
  • 5
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jul 29 '13 at 17:09
  • You need to indicate where in the code is the Exception thrown. – John Alexiou Jul 29 '13 at 17:10
  • I do not understand your code opzioni[i] would be one char what are you stepping through in your second for loop? – Bit Jul 29 '13 at 17:11
  • @N4TKD He's copying everything from before the `-` into a new char array. Very inefficiently. – Russell Uhl Jul 29 '13 at 17:13
  • 1
    @Russell Uhi ok so he may want to search substring or split and avoid this. – Bit Jul 29 '13 at 17:15

5 Answers5

6

You should put a check to see if the string value is null or empty before trying to loop through each character, like this:

if(!String.IsNullOrEmpty(opzioni))
{
    // Put loop through character logic here
}
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
3

You need to debug through and find out if your opzioni string is a null reference after your call to String opzioni = lettore_file.ReadLine();

Also, you should probably declare i within the for loop, instead of before it, like shown below.

for (int i = 0; i < opzioni.Length; i++)

Jim
  • 6,753
  • 12
  • 44
  • 72
  • clarification for maxpesa: declaring `i` inside the loop is not necessary, although it is considered "normal" practice, as not only is the variable destroyed upon exit of the loop, but it also looks nice too – Russell Uhl Jul 29 '13 at 17:10
  • Yes, I didn't mean to say that was causing a problem, but wanted to point it out as something that is atypical. – Jim Jul 29 '13 at 17:13
  • yup! and I agree. I just didn't want there to be any confusion. – Russell Uhl Jul 29 '13 at 17:13
  • @uhi did you mean (int i=0;i<....)? if yes, i put it outside the loop because i thought it was the origin of the problem – maxpesa Jul 29 '13 at 19:43
1

There are several wrongs inside that code:

You're missing a using statement.

You're not checking the result of StreamReader.ReadLine.

It looks like you're reimplementing string.Substring.

Sample:

if (File.Exists(@"C:\ESC\Impostazioni.txt"))
{
    using (var letterFile = new StreamReader(@"C:\ESC\Impostazioni.txt"))
    {
        var opzioni = letterFile.ReadLine();

        if(string.IsNullOrWhiteSpace(opzioni))
        {
            // end of file
        }

        var dashIndex = opzioni.IndexOf("-");

        string coloregenerale = dashIndex > -1
                                    ? opzioni.Substring(0, dashIndex)
                                    : opzioni;
    }
}
Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92
0

If your null reference exception is occurring on the indicated line, the culprit is probably the opzioni variable. If the earlier call to lettore_file.ReadLine() returns null, opzioni will be null when you try to get it's length in the for loop. That will throw the exception you're experiencing. A solution is to check that variable for null before entering the loop

Charles Josephs
  • 1,495
  • 3
  • 14
  • 25
0

The opzioni may have the chances of null value on that particular index, better handle inside of that line with IsNullorEmpty Condition

char[] coloregenerale = new char[i];
            for (int j = 0; j < i; j++)
              {
                if( String.IsNullOrEmpty(opzioni[j] == false ) // This is mandatory check at this place
                coloregenerale[j] = opzioni[j];
              }
Smaug
  • 2,625
  • 5
  • 28
  • 44