Join me at Google+ ...

Problem

Last weekend I tried to to access a SharePoint list items choice column value and I got a NullReferenceException. But why?

Well, I created a document library and uploaded a document.

Lib1

After that I created a new column using the "Document Library Settings".

image Lib3

Finally I had a new column. 

image

It is important to mention that I did not edit my list item after creating the new column.

Then I tried to programmatically access the list items column value by using

string myChoice = listItem[columnDisplayName].ToString();

and I got the  

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

Solution

You will always get a NullReferenceException until you edit the list item and select a choice value.

object myObject = listItem[columnDisplayName];
if (myObject != null)
    //DoSomething

Using the code above you can check for null.

By the way: If you add a Lookup column to your list you can access the columns value by using:

SPFieldLookupValue value = new SPFieldLookupValue(listItem[columnDisplayName].ToString());
if (value.LookupValue != null)
    //DoSomething

or if you have a multiple lookup by using

SPFieldLookupValueCollection coll = new SPFieldLookupValueCollection(listItem[columnDisplayName].ToString());
foreach (SPFieldLookupValue itemValue in coll)
{
    //DoSomething
}

In this case "listItem[columnDisplayName].ToString()" doesn't throw a NullReferenceException ;)



Get updates

 

Comments (4) -

2/7/2009 5:03:01 PM #

zI have a problem with the SPLookUpField .. I think a class Lookup, then for each control of a document Library see what kind of control is.
If I lookup the following code:

private void FillDocProperties(SPList list)
{


try
{
htDocProperties = new Hashtable();

foreach (Indice i in indices)
{
string name = i.Name.Text;

if (!i.Value.Equals(string.Empty))
{
if (list.Fields.ContainsField(name))
{
if (i.Field.Type.ToString().Equals("Lookup"))
{
int id = int.Parse(i.Aux);
string value = i.Value;
htDocProperties[name] = id + ";" + value;
}
else
{
htDocProperties[name] = i.Value;
}

}
}

I created a HastTable because I have the document versioning, ie, there may be several versions of the same document so if I save the values in HastTable is easier. I can write in HastTable but can not store the lookup data in SharePoint.

Classe Lookup:
public WPLookUP(SPFieldLookup l, string defaultValue)
: base(l)
{

lookup = l;
dropDownList = new DropDownList();
bool exist = false;
SPList list;
defaultValue = "Escolha uma opção";
dropDownList.Items.Add(new ListItem(string.Empty, string.Empty));

using (SPSite site = new SPSite(WebSettings.SiteUrl + WebSettings.DCSite))
{
using (SPWeb web = site.OpenWeb())
{
list = web.Lists.GetList(new Guid(lookup.LookupList), false);

SPListItemCollection items = list.Items;

foreach (SPListItem spitem in items)
{

ListItem item = new ListItem();

string value = (string)spitem[lookup.Title];

SPFieldLookupValue lookupValue = (SPFieldLookupValue)lookup.GetFieldValue(Convert.ToString(spitem["ID"]) + ";#" + value);

item.Text = lookupValue.LookupValue;

item.Value = lookupValue.LookupValue;

item.Attributes.Add("LookupId", lookupValue.LookupId.ToString());

if (defaultValue != null)
{
if (defaultValue.Equals(spitem[lookup.LookupField].ToString()))
{
item.Selected = true;
exist = true;
}
}
dropDownList.Items.Add(item);

}
}
}

}
}

Thank you very much for your help .. Thanks

private void LoadIndices(SPList oneList)
{
string[] readOnlyFields = WebSettings.ReadOnlyFields.Split(',');
SPFieldCollection fields = oneList.Fields;
TextBox texto = new TextBox();
int i = 0;
foreach (SPField f in fields)
{
if (!f.Hidden && !f.ReadOnlyField && !f.Type.ToString().Equals("Invalid") && !CheckReadOnlyField(readOnlyFields, f.InternalName))
{
string choice = f.Type.ToString();


switch (choice)
{

case "Lookup":

SPFieldLookup lookup = (SPFieldLookup)f;
WPLookUP lookupIndice = new WPLookUP(lookup, lookup.DefaultValue);
indices.Add(lookupIndice);
lookupIndice.CreatWebObjectControl(pnlFields.Controls);
i++;
break;

}

Carla Portugal Reply

2/10/2009 1:15:33 PM #

Hi Carla,

this is difficult to say... where does the exception exactly occur? And did you try to debug your solution?

Andreas

Andreas Glaser Switzerland Reply

3/10/2010 7:18:00 AM #

We are getting "system.nullreferenceexception object reference not set to an instance of an object. at ASP._65520b6b_4e51_4831_a307_bb5a844d49b7_968217546.Page_Load(Object sender, EventArgs e)" frequently on our site on home page frequently. Some times on other pages also.

After restarting Office Sharepoint server search service and Sharepoint services Administration service, site is working fine. But after some hours it is coming again.

I checked code as per your solution. We have checked for null value.

Is there any other workaround for this issue ?

Thanks,
Dhaval

Dhaval Patel India Reply

5/19/2010 5:56:34 PM #

I have tried this code, but i still get the object reference error it isn't the same error as before

Object reference not set to an instance of an object.

Shareiq United Kingdom Reply

Add comment




  Country flag
biuquote
Loading