0

I am receiving a Null Reference exception error, and it has something to do with either the substring function or the LastIndex function. I am pretty confused why

          String[] m_SessionNames;
          String[] filenames; //Filenames already initialized in code
          int Index = 0;
                foreach (String name in filenames)
                {
                    MessageBox.Show(filenames[index]) //Works, Displays a string that includes a '\'
                    m_SessionNames[Index] = filenames[Index].Substring((filenames[Index].LastIndexOf('\\') > 0) ? filenames[Index].LastIndexOf('\\') + 1 : 0);
                    Index++;                                                                                                                                                        
                }
user1819301
  • 113
  • 4
  • 17

3 Answers3

1

It looks like you're trying to get the file name out of a path.

Luckily there is already a method: Path.GetFileName

var m_SessionNames = new List<string>();

for (int i = 0; i < filenames.Length; i++)
{
    var filename = filenames[i];

    if (string.IsNullOrWhiteSpace(filename))
    {
        MessageBox.Show("filename is null");
        continue;
    }

    MessageBox.Show(filename);

    m_SessionNames.Add(Path.GetFileName(filename));
}

return m_SessionNames.ToArray();

Here is a more concise way using Linq.

// Get all valid filenames
var sessionNames = filenames.Select(Path.GetFileName)
                            .Where(f => !string.IsNullOrWhiteSpace(f));

m_SessionNames = sessionNames.ToArray();
Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92
  • That is what I am accomplishing. I will try this and let you know how it works. – user1819301 Jul 15 '13 at 16:38
  • Unfortunately I still receive the error, but I can print out filename, and I do not get the filename is null message. – user1819301 Jul 15 '13 at 16:41
  • The Linq way doesn't produce any Null Exceptions. But I'd like it in a string array if that's possible? – user1819301 Jul 15 '13 at 16:46
  • You need to make sure the `m_SessionNames` array is initialized and it has enough capacity to hold all the file names. – Dustin Kingen Jul 15 '13 at 16:47
  • I have it declared as public String[] m_SessionNames; Thats probably the problem right? I'm not too positive on csharp memory allocation. – user1819301 Jul 15 '13 at 16:49
  • You must initialize the array using the `new` operator. e.g. `string[] m_SessionNames; m_SessionNames = new string[];` You can use a [`List(T)`](http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx) for variable length data. – Dustin Kingen Jul 15 '13 at 16:52
  • Don't forget to mark an answer as correct. – Dustin Kingen Jul 15 '13 at 17:13
1

You are saying m_SessionNames and filenames are all already initialized.

If that's the case You should Not have

String[] m_SessionNames; String[] filenames;

On top of your code.

Where they are initialized? Even though they may be intialized somewhere else you are declaring 2 new arrays here which are not initialized.

If you are using this code inside a function then declare m_SessionNames and file names in the class level and then populate them. Then in this function you don't need to declare them again.

  • 1
    The code wouldn't compile if they were in fact not initialized. He wouldn't be able to get the null reference, cause it wouldn't build due to "use of unassigned local...". They definitely need to update their code to something that actually compiles though. – Gray Jul 15 '13 at 16:52
  • I have them declared as public members of the class. Do I have to populate them through the constructor? – user1819301 Jul 15 '13 at 16:53
  • Yes you need to initialize them. – Dustin Kingen Jul 15 '13 at 16:54
  • where did you assing and with what can you show the code? if you have not populate them already yes you need to somewhere before you reach this code(i mean initialize them and populate). – terrybozzio Jul 15 '13 at 16:55
  • If they are public members(fields) of the class, then you need to initialize them "Every" time you create he class. –  Jul 15 '13 at 17:56
0

seems to be filenames[Index]is null. therefore if you invoke any method on the filenames[Index] it may produce the NullPointerException. iif the filenames are already initialized in the code then the reason can be the m_SessionNames[Index] and it can be evaluated to null. this is because we cannot see m_SessionNames is not initialized anywhere in the code

Chathuranga Tennakoon
  • 2,059
  • 1
  • 18
  • 20