I'm trying to insert a custom object record which contains various custom fields and also a lookup field. Is it possible to insert the record without specifying the lookup field value? I've tried inserting an empty value or null on the lookup field but it throws an System.NullPointerException: exception error.

carts.Code__c --> The Lookup field

if(carts == null) carts = new Cart__c();
      carts.rStatus__c = 'Saved';
      carts.Price__c = 125;

     if (code != null || code.length() != 0){
     carts.Code__c = [SELECT Name, code__c FROM Code__c WHERE code__c =:code].Id;
       }   else{
          carts.Code__c = '';

      insert carts;

   pageRef = new PageReference('/apex/out'); 

    }catch(DmlException e) {
     ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'Error' + e);

    } catch(Exception e) {
    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'Please be patience, Your request will be......' + e);

    return pageRef;
  • Is Code__c a required lookup field? Commented Mar 22, 2015 at 18:12
  • It's not a required lookup field @DanielBallinger
    – fb69
    Commented Mar 22, 2015 at 18:14
  • What is the exception that you are getting? Commented Mar 22, 2015 at 18:15
  • It's System.NullPointerException: Attempt to de-reference a null object @DanielBallinger
    – fb69
    Commented Mar 22, 2015 at 18:23
  • Which line in your code is given that exception? If it is coming from a DML operation like insert carts; then it may be a trigger expecting the Code__c field to be populated. Commented Mar 22, 2015 at 19:13

3 Answers 3


Currently the error message is System.QueryException: List has no rows for assignment to SObject.

This will most likely be coming from the line: (please indicate in your responses which line is throwing the exception)

carts.Code__c = [SELECT Name, code__c FROM Code__c WHERE code__c =:code].Id;

There are a couple of problems with this line:

  1. You are using a short hand syntax that assumes exactly one Code__c record will always be returned by this query. If no records match then you will get the QueryException for no rows.
  2. You are querying the Name and Code__c fields and then using the Id field, which isn't part of the SOQL query.

Instead, try something like:

List<Code__c> codes = [Select Id from Code__c where code__c = :code limit 1];
if(codes.size() > 0) {
    carts.Code__c = codes[0].Id;
} else {
    carts.Code__c = null;

Check if the field is required in the profile then if this is not required you could skip the field when you are creating the record. Be sure that field is not an a master detail because by default it is required.

  • noted. Lookup field is not required. it's Lookup Field. Not master details. Thanks.
    – fb69
    Commented Mar 22, 2015 at 19:09

I suspect the SOQL query inside your code is not correct. Your are querying a wrong object.

carts.Code__c = [SELECT Name, code__c FROM Code__c WHERE code__c =:code].Id;

I believe it should be something like this according to the Carts object in your instance.

carts.code__C = [Select name from Code__c where name=:code].id
  • Sadly no. My problem is related to inserting an empty value in my lookup field. The solution you gave me is unrelated to my question. Thanks anw.
    – fb69
    Commented Mar 22, 2015 at 19:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .