0

I'm reading a .txt from my .dll that I made.

My code is:

Assembly assembly = Assembly.LoadFile(Application.StartupPath + "/MyFiles.dll");
System.Resources.ResourceManager resourcemanager = new System.Resources.ResourceManager("ClassLibrary1.Properties.Resources", assembly);
string[] strArrays15 = resourcemanager.GetString("JobsList").Split('\n');
for (int t = 1; t < (int)strArrays15.Length; t++)
    comboBox1.Items.Add(strArrays15[0].Split('\t'));
return;

I'm getting the following error :Object reference not set to an instance of an object on the third line.

LB2
  • 4,802
  • 19
  • 35
  • 2
    Either `new System.Resources.ResourceManager("ClassLibrary1.Properties.Resources", assembly);` is returning `null` to `resourcemanager`, or `resourcemanager.GetString("JobsList")` is returning null. Use the debugger and step through your code to see which one. Also assigning `resourcemanager.GetString("JobList")` to it's own `string` variable, then running `.Split()` on it would help you determine which item is null. – Gary Schreiner May 12 '14 at 17:32
  • Yea I didn't add JobsList to the .dll well . now When I add .. I don't get the .txt content on ComboBox – user3597342 May 12 '14 at 17:32
  • I'am getting (on the combobox) : "Tableau de String[]" , in english means : String Array[] – user3597342 May 12 '14 at 17:33
  • So strArrays15 contains a valid string array before you go into the for statement? And please clarify what you mean you're getting String Array[] on the combobox. Include the actual complete error if you're getting one. – Gary Schreiner May 12 '14 at 17:36
  • Also, just a pointer, you might want to look into doing some null checking and error checking in the code to make sure you're not running string operations on null strings. It'll help you in the long run from trying to figure out what is causing unhandled exceptions in the future. – Gary Schreiner May 12 '14 at 17:38
  • http://epvpimg.com/V6j6f.jpg here look , but the .txt isn't null :x – user3597342 May 12 '14 at 17:41
  • ok i'm going to post an answer and i'll throw some error checking in for you to help you out. – Gary Schreiner May 12 '14 at 17:43
  • at the begining Object reference not set to an instance of an object but I fixed it It's not that error anymore . I'am getting like I showed you on the screen . no error messages – user3597342 May 12 '14 at 17:47

1 Answers1

0

your problem is with comboBox1.Items.Add(strArrays15[0].Split('\t')); you are running a for statement and adding a string array from the Split of the first strArrays15 array to the combobox items each time the for loop runs. This just creats a huge list of string arrays. Either combobox.AddRange on a single string array, or loop through and add each individual item.

Here's one more problem.

You're looping through (int)strArrays15.Length but using strArrays15[0].Split('\t'); instead of strArrays15[i].Split('\t'); and adding the output string array from the split to the combox item, which doesn't make sense. You need to be more clear on what you're trying to accomplish.

Assembly assembly = Assembly.LoadFile(Application.StartupPath + "/MyFiles.dll");
System.Resources.ResourceManager resourcemanager = new System.Resources.ResourceManager("ClassLibrary1.Properties.Resources", assembly);
if (resourcemanager != null) {
  string jobListString = resourcemanager.GetString("JobsList");
  if (!String.isNullOrEmpty(jobListString))
    comboBox1.Items.AddRange(strArrays15[0].Split('\t'));
  else
    // handle error
} else
  //handle error
return;

From your code though it looks like strArrays15 has more than 1 line, so unless you tell me exactly what data you're trying to put into the combox, and what's in strArrays15, I can't help you any further.

Gary Schreiner
  • 902
  • 5
  • 14