0

I tried to split a String into a parts array and combine them at the end into a result String. But while I tested a little bit, I get a message.

By pressing convert_click:

"NullRefenceException was unhandeled"

Object reference not set to an instance of an object.

Here the main code:

    public string []parts { get; set; }
    public string inputStr { get; set; }

    private void inputText_TextChanged(object sender, EventArgs e)
    {
        String inputStr = inputText.ToString(); 
        //example 
        //inputStr = "984, fenceshit2, 0, 1994.56025813, -1592.16428141, 16.105, 0.653280779782, 0.270598520636, 0.653281646552, 0.270598879665, -1";
    }

    private void convert_Click(object sender, EventArgs e)
    {
        String creObj = "CreateObject(";
        String result;
        String[] parts = inputStr.Split(new char[]  { ',' });

        result = creObj +
                 parts[0] + "," +
                 parts[2] + "," +
                 parts[3] + "," +
                 //...up to "parts[10"
                 ");";
        outputText.Text = result; 
        //output(should be in this case): 
        //"CreateObject(984,    1994.56025813,  -1592.16428141,  16.105, 0.653280779782,  0.270598520636,  0.653281646552, 0.270598879665, -1);"
    }

    //If I need to creat a code line in the main Designer.cs, please let me know.

I just want to split a String and combine them in the end into 1 string and send this into a text box.

If someone wants the sourcecode, pm me.

Bill Gregg
  • 7,067
  • 2
  • 22
  • 39
Knolle
  • 1

2 Answers2

1

Because you are assigning inputText.toString() to local inputStr. Inside function inputText_TextChanged, just write

inputStr = inputText.Text;
cissharp
  • 216
  • 1
  • 11
  • If I do this I got new problems. `Non-invocable member 'System.Windows.Forms.Control.Text' cannot be used like a method.` – Knolle Jun 19 '13 at 17:59
  • I was guessing inputText is TextBox control. Can I know what is the inputText control is? – cissharp Jun 19 '13 at 18:03
  • What you do you mean "control"? – Knolle Jun 19 '13 at 18:20
  • I mean what type of GUI element is inputText box is? Is it TextBox, ComboBox, ListBox, Form or ... ? – cissharp Jun 19 '13 at 18:24
  • Are you sure inputText is the name (or ID) you used in your designer. Can you send the designer code? – cissharp Jun 19 '13 at 18:28
  • 2
    @Knolle Did you write in your code `inputStr = inputText.Text();` or `inputStr = inputText.Text;`? – Chris Sinclair Jun 19 '13 at 18:28
  • `private void inputText_TextChanged(object sender, EventArgs e) { inputStr = inputText.ToString(); }` – Knolle Jun 19 '13 at 18:32
  • Yes like Chris said you wrote inputStr = inputText.Text(); instead of inputStr = inputText.Text; – cissharp Jun 19 '13 at 18:34
  • Ok, after a few another test I can't find the real problem. Can someone code it for me, maybe? – Knolle Jun 19 '13 at 18:47
  • That is not the way this forum works. You need to try and do some research about TextBox control, its properties and methods. You have very minor problem in your program. – cissharp Jun 20 '13 at 15:03
0

You are declaring a local copy of input string, when you really want to be assigning to the public one.

Instead of

String inputStr = inputText.ToString(); 

Just do this:

inputStr = inputText.ToString(); 
Bill Gregg
  • 7,067
  • 2
  • 22
  • 39