-1

I am trying to write some dynamic code , where each control's value will go through set of regular expressions attached to it.

In order to achieve so I have created a XML which will represent which all Regex will be applied to its values. Sample XML:

<control name="firstname">
<validationtype ="regex:'. ">
<validationtype ="regex:a-zA-Z">
</control>

I am trying to read the XML in c# code where I loop thru validationtype and adding in to the List<string>

List<string> validations = new List<string>();
foreach(var item in vaildationtype)
{
  if(item.contains("regex:")
  {
   validations.add(item.text.split(':')[1]);
  }
}

Above code when try to add regex expression like ".' " in to List<string> throwing object reference not set to an instance of object error

Appreciate your pointers.

UPDATE: so some guys are actually deviated from my ask: here is what I am after.

I have an array of strings in which I store all the regex that was passed by another program. A program passes the values as "regex:a-zA-z", "regex:0-9", "regex:.' " etc

I want to store all the expressions into List<string> object by splitting the values passed by a program. So, In my list I want to store "a-zA-z", "0-9" etc. but, it is throwing object reference not set to an instance".

Dirty Developer
  • 493
  • 4
  • 21
  • what is `vaildationtype` ? where is the property `item.text` initialized?, also please post compileable code: `validations.add(...` – Mong Zhu Oct 31 '19 at 10:33
  • @MongZhu I know null reference exception. updated the question. validationtype is nothing but the XML node which I extracted from XML as per my need. I thought, let me copy only what is needed to support my question and not the whole program. – Dirty Developer Oct 31 '19 at 11:48
  • @MongZhu I have already searched the internet, not found anything hence the question is here. hoping for help – Dirty Developer Oct 31 '19 at 11:50
  • we don't need the whole programm. you need to debug your application to solve this problem, like it is written in the duplicate. From your post I understand that this line: `validations.add(item.text.split(':')[1]);` throws the exception. But you say this is just pseudo code. With this limited information we cannot help you, and surely we cannot debug your program for you. – Mong Zhu Oct 31 '19 at 12:10
  • @DirtyDeveloper Your edit doesn't add anything new; you didn't provide enough information to receive actual help. 1) Please read how to create a [repro]. 2) Please follow the guidance provided in the linked post; start debugging your code by placing some breakpoints and stepping through the code. – 41686d6564 Oct 31 '19 at 16:22

1 Answers1

-2

It is because in your foreach you wrote : foreach(var item in vaildationtype)
You misspelled "validationtype"

  • misspelling should result in compilation errors and not in runtime errors like exceptions – Mong Zhu Oct 31 '19 at 10:34
  • Not always, he's looking for a type in a XML file, even at compilation the programm can't know whether he's right or not. It's like calling an IPAddress in a programm, until you've called it, the programme can't know if the IP is available or not – Cyril Krzyzowski Oct 31 '19 at 10:38
  • you are talking about the variable name in a foreach loop call, unless there is such a variable with this exact wrong spelling it will yield a compiler error. Your point in your answer has nothing to do with xml – Mong Zhu Oct 31 '19 at 10:57
  • @MongZhu buddy, I have just pasted a code snippet. look at my actual question – Dirty Developer Oct 31 '19 at 11:41
  • @CyrilKrzyzowski it is just a code snippet, psudo code. – Dirty Developer Oct 31 '19 at 11:49