Solved

Null Array

Posted on 2014-12-16
3
94 Views
Last Modified: 2014-12-17
I have a value that I am splitting into an array. When the value is null, it gives me an error message. How can I prevent the error message when the value is null.
                        string PhoneNum = reader["PhoneNumber"].ToString();
                        string[] strArr = PhoneNum.Split('-');
                        PersonPhoneNumber1.Value = strArr[0];
                        PersonPhoneNumber2.Value = strArr[1];
                        PersonPhoneNumber3.Value = strArr[2];   

Open in new window

0
Comment
Question by:RecipeDan
3 Comments
 
LVL 36

Assisted Solution

by:Miguel Oz
Miguel Oz earned 250 total points
ID: 40503529
I assume PhoneNum is empty.
If it is, please modify code to check for empty as follows:
  string PhoneNum = reader["PhoneNumber"].ToString();
  if (!String.IsNullOrEmpty(PhoneNum))
  {
	String[] Strarr = Phonenum.Split('-');
	Personphonenumber1.Value = Strarr[0];
        if (Strarr.Length > 1) 
	     Personphonenumber2.Value = Strarr[1];
        if (Strarr.Length > 2) 
	     Personphonenumber3.Value = Strarr[2];
  }
 //else set default values

Open in new window

0
 
LVL 33

Accepted Solution

by:
it_saige earned 250 total points
ID: 40503654
Another possibility is that reader["PhoneNumber"] represents a null value in which case you would use the following in conjunction with Miguels code:
string PhoneNum = reader["PhoneNumber"] != null ? reader["PhoneNumber"].ToString() : string.Empty;
if (!String.IsNullOrEmpty(PhoneNum))
{
	String[] Strarr = Phonenum.Split('-');
	Personphonenumber1.Value = Strarr[0];
	if (Strarr.Length > 1) 
		Personphonenumber2.Value = Strarr[1];
	if (Strarr.Length > 2) 
		Personphonenumber3.Value = Strarr[2];
}
//else set default values

Open in new window


-saige-
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 40504947
Thank you both for your assistance.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question