1
string dosage = "2/3/5 mg";
string[] dosageStringArray = dosage.Split('/');
int[] dosageIntArray = null;
for (int i = 0; i <= dosageStringArray.Length; i++) 
{
    if (i == dosageStringArray.Length)
    {
        string[] lastDigit = dosageStringArray[i].Split(' ');
        dosageIntArray[i] = Common.Utility.ConvertToInt(lastDigit[0]);
    }
    else
    {
        dosageIntArray[i] = Common.Utility.ConvertToInt(dosageStringArray[i]);
    }
}

I am getting the exception on this line: dosageIntArray[i] = Common.Utility.ConvertToInt(dosageStringArray[i]); I am unable to resolve this issue. Not getting where the problem is. But this line int[] dosageIntArray = null; is looking suspicious.

Exception: Object reference not set to an instance of an object.

3
  • 4
    Oy, I really hope this is nowhere near production code. >_< Commented Aug 29, 2011 at 11:01
  • @asma... I mistaken. but you can use Split('/',' '); and just drop last value Commented Aug 29, 2011 at 11:14
  • @神田翠 ohh I didnt know this. actually I havn't work much in C# Commented Aug 29, 2011 at 11:16

6 Answers 6

3

The biggest problem with your solution is not the missing array declaration, but rather how you'd parse the following code:

string dosage = "2/13/5 mg";

Since your problem is surely domain specific, this may not arise, but some variation of two digits representing same integer.

The following solution splits the string on forward slash, then removes any non-digits from the substrings before converting them to integers.

Regex digitsOnly = new Regex(@"[^\d]");   
var array = dosage.Split('/')
                  .Select(num => int.Parse(digitsOnly.Replace(num, string.Empty)))
                  .ToArray();

Or whatever that looks like with the cuddly Linq synthax.

Sign up to request clarification or add additional context in comments.

1 Comment

Your solution is so neat and delicate. Mine was a mess! I'd prefer to use yours one.
3

You are looking for something like

int[] dosageIntArray = new int[dosageStringArray.Length];

1 Comment

Need to intialize with the length(Array length)
1

You are trying to access a null array (dosageIntArray) here:

dosageIntArray[i] = Common.Utility.ConvertToInt(lastDigit[0]);

You need to initialize it before you can access it like that.

Comments

1

You have to allocate dosageIntArray like this:

in[] dosageIntArray = new int[dosageStringArray.Length];

Also, you have another bug in your code: Index of last element of an array is Length - 1. Your for statement should read as:

for (int i = 0; i < dosageStringArray.Length; i++)

or

for (int i = 0; i <= (dosageStringArray.Length - 1); i++)

The former is preferred and is the most common style you will see.

2 Comments

hmmm... you re right. But the problem started from where Dominik mentioned.
also, the if equality check of the loop variable against array.length needs to be modified to check against array.length - 1
0

I strongly recommend you use Lists instead of Arrays. You don't need to define the size of the List; just add items to it. It's very functional and much easier to use.

3 Comments

I think there is no point of using list if array size is static.
I'd say there's no point using an array if you can use a list.
@Kirk, while the list is certainly more convenient, it uses an array for backing the data as well. So if the dimension is known in advance, I don't see the real downside of using an array. That said, the task as presented in the question should be solved with neither approach IMHO.
0

As alternative approach:

var dosage = "2/3/5 mg";
int[] dosageIntArray = Regex.Matches(dosage, @"\d+")
                            .Select(m => int.Parse(m.Value))
                            .ToArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.