-1

Here's the code:

public Form1()
{
    string[] reader = File.ReadAllLines(@"F:\Bioshock2SP.ini");

    foreach (string lines in reader)
    {
        if (lines.Contains("VoVolume="))
        {
            lines.Substring(lines.Length -2);
            TextBox.Text = lines; //Exception
        }
    }

    InitializeComponent();
}

I'm trying to find a line in a file and make only the actual setting part of the file show in the textbox. For example I want to find the line containing "VoVolume=55", but I only want, "55" to show in the textbox or even just, "1.534".

I keep getting an exception when I launch the program. Any ideas?

Exception Details:

NullReferenceException was unhandled.

An unhandled exception of type 'System.NullReferenceException' occurred in EditingTextFromFile.exe

Additional information: Object reference not set to an instance of an object.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
ShadyOrb09
  • 75
  • 1
  • 10
  • Why would you make us guess what the exception is? Post the details. – Blorgbeard Aug 18 '16 at 01:37
  • Sorry , I'm new to programming entirely, I'll post the details. – ShadyOrb09 Aug 18 '16 at 01:38
  • 1
    #1 rule of programming: error messages are *not* interchangeable (as popularly believed by users). Always read them, always include them when asking for help! – Blorgbeard Aug 18 '16 at 01:40
  • [Here](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) is some bedtime reading for you. You should also check the stack trace from the error and see which line causes the exception. – Blorgbeard Aug 18 '16 at 01:58
  • @Blorgbeard Thanks :) – ShadyOrb09 Aug 18 '16 at 02:37

3 Answers3

2

Ok. First, you can't use the textBox if it hasn't been initialized. You need to make sure InitializeComponent is run first. This is what is causing your exception. Second, that Substring is not doing anything as you're not using the output value.

Here is your code slightly altered to correct those two issues :

public Form1()
{
    InitializeComponent();
    string[] reader = File.ReadAllLines(@"F:\Bioshock2SP.ini");
    foreach (string lines in reader)
    {
        if (lines.Contains("VoVolume="))
        {
            lines = lines.Substring(lines.Length - 2);
            TextBox.Text = lines.Split(new string[] { "=" }, StringSplitOptions.None).Last();
        }
    }   
}

And TextBox is a terrible name for a TextBox instance.

Edit : I've used the code from Mike's answer.

Kinetic
  • 2,640
  • 17
  • 35
  • Hey thanks that worked! It shows the whole line in the textbox as, "VoVolume=55", or "VoVolume=1.552" etc. How do I get it to just print "55"? – ShadyOrb09 Aug 18 '16 at 02:31
  • I've edited my answer to incorporate Mike answer into mine. You can split on the "=" character and get the array's last string. – Kinetic Aug 18 '16 at 02:35
0

TextBox is a class, not an instance.

If you drag and drop a TextBox into form, you can find the Name property, like: textBox1

Then you can try following code:

textBox1.Text = lines.Split(new string[] { "=" }, StringSplitOptions.None).Last();
Mike
  • 91
  • 5
  • If you suggest something suggest the best. `textBox1` will not be a good name – sujith karivelil Aug 18 '16 at 01:44
  • I tried your code and I got the same exception as above. "TextBox" was the textbox's name, I just named it that at the time because I'm trying this as a test for my main program. – ShadyOrb09 Aug 18 '16 at 01:47
-1
  • See link sample: Reading/writing an INI file
  • I think my code run ok

    var MyIni = new IniFile(@"F:\Bioshock2SP.ini");

    TextBox.text = MyIni.Read("VoVolume");

Community
  • 1
  • 1
Tai Nguyen
  • 105
  • 1
  • 10