0

Guys I have a string in Model.Products[i].Description. Of unknown length.

I want to have only the first 60 characters of it in a variable.

  var data = Model.Products[i].Description;
  var temp = string.Join(string.Empty,data.Take(60));

Gives me a null value exception error. What did I do wrong ?

Any help ?

Nishant Rambhojun
  • 77
  • 1
  • 2
  • 10
  • All the answers here are ignoring the problem. `data` is `null` here. Debug your program and make sure that `Model.Products[i].Description` is _not null_. – Chris Sinclair May 02 '14 at 11:12
  • :) The data is not null. – Nishant Rambhojun May 02 '14 at 11:15
  • 1
    Can you post the _actual full exception_ then? Perhaps `Model` is `null`, `Products` is `null`, or `Products[i]` is `null` (or depending on what type `Products` is, perhaps `i` is `null`) – Chris Sinclair May 02 '14 at 11:17
  • This question is pretty vague. There are all kinds of simplifications you could make in order to present a simpler problem. Replace `Model.Products[i].Description` with `"abcdefghijklmnop"`. Replace 60 with 6. Does it still give you a null exception? You shouldn't be asking a question here until you have exhausted all your ideas, then ask the simplest question possible. Don't be lazy, this is basic debugging. – demoncodemonkey May 02 '14 at 11:24

6 Answers6

5

Use Substring

Note: you need to check you input's length

string data = Model.Products[i].Description;
string temp = string.Empty;
if(!string.IsNullOrEmpty(data) && data.Length >= 60)
   temp = data.Substring(0,60);
else
   temp = data;
Hassan
  • 5,360
  • 2
  • 22
  • 35
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
4

You can use

var temp = data.Substring(0, Math.Min(data.Length, 60));

If the length of the string is less then 60 it will take the length of the string.

bkardol
  • 1,258
  • 1
  • 20
  • 32
1

Wow, you actually used the method name as a tag on your question, you were so close ;)

var trim = data.Length > 60 ? 60 : data.Length;
data.Substring(0, trim);
Jeremy Hutchinson
  • 1,975
  • 16
  • 26
1

All the answers here are ignoring the problem. data is null here. Debug your program and make sure that Model.Products[i].Description is not null.

Your current method to get the first 60 characters is a bit obtuse, but correct. The problem is you're retrieving invalid data before you even start trying to get the 60 characters.

If a null value is considered valid for you (that is, you expect it to be null sometimes) then you need to do a null check first then decide what you want to do. Do you want temp to be null or an empty string?


Here is sample code performing the check, not ideal code but designed to demonstrate the problem:

string data = Model.Products[i].Description;
string temp;
if (data == null)
{
    temp = //? what do you want "temp" to be if data is null?
}
else
{
    temp = string.Join(string.Empty,data.Take(60));
}
Chris Sinclair
  • 22,858
  • 3
  • 52
  • 93
0
var temp = data.Substring(0, 60);

NOTE: check if string (i.e. data) is null or empty and its length prior to calling Substring method.

Following is example (As stated in other valid answers)

string temp = string.Empty;
if(!string.IsNullOrEmpty(data) && data.Length >= 60)
   temp = data.Substring(0,60);
else
   temp = data;
Hassan
  • 5,360
  • 2
  • 22
  • 35
  • this should not work if length is less then 60 then it shoule give exception like Index and length must refer to a location within the string. – Dhaval Patel May 02 '14 at 11:10
0

you are using string.Empty which is null, try:

if (data.Length>=60)
{
  temp=data.Substring(0,60);
}
apomene
  • 14,282
  • 9
  • 46
  • 72