I am facing this error when doing tutorial on step saving Expense.

Uncaught error in actionCallback : Cannot read property 'namespace__Amount__c' of null

Also other related question is here but no answer yet.Here my code form.cmp

 <aura:component controller="ExpenseController">
    <ltng:require styles="/resource/bootstrap"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <!-- Other aura:attribute tags here -->
    <!-- Other code here-->
    <aura:attribute name="expenses" type="Expenses__c[]"/>
    <aura:attribute name="newExpense" type="Expenses__c"
    default="{ 'sobjectType': 'Expenses__c',
    'Name': '',
    'Amount__c': 0,
    'Client__c': '',
    'Date__c': '',
    'Reimbursed__c': false
    }"/>
    <!-- Attributes for Expense Counters -->
    <aura:attribute name="total" type="Double" default="0.00" />
    <aura:attribute name="exp" type="Double" default="0" />
    <!-- Input Form using components -->
    <div class="bootstrap-sf1">
    <div class="container">
    <form>
    <fieldset>
    <ui:inputText aura:id="expname" label="Expense Name"
    class="form-control"
    value="{!v.newExpense.Name}"
    placeholder="My Expense" required="true"/>
    <ui:inputNumber aura:id="amount" label="Amount"
    class="form-control"
    value="{!v.newExpense.unid__Amount__c}"
    placeholder="20.80" required="true"/>
    <ui:inputText aura:id="client" label="Client"
    class="form-control"
    value="{!v.newExpense.Client__c}"
    placeholder="ABC Co."/>
    <ui:inputDateTime aura:id="expdate" label="Expense Date"
    class="form-control"
    value="{!v.newExpense.Date__c}"
    displayDatePicker="true"/>
    <ui:inputCheckbox aura:id="reimbursed" label="Reimbursed?"
    class="checkbox"
    value="{!v.newExpense.Reimbursed__c}"/>
    <ui:button label="Submit" press="{!c.createExpense}"/>
    </fieldset>
    </form>
    </div><!-- ./container-->
    <!-- Expense Counters -->
    <div class="container">
    <div class="row">
    <div class="col-sm-6">
    <!-- Make the counter red if total amount is more than 100 -->
    <div class="{!v.total >= 100 ? 'alert alert-danger' : 'alert
    alert-info'}">
    <h3>Total Expenses</h3>$<ui:outputNumber value="{!v.total}"
    format=".00"/>
    </div>
    </div>
    <div class="col-sm-6">
    <div class="alert alert-info">
    <h3>No. of Expenses</h3><ui:outputNumber value="{!v.exp}"/>
    </div>
    </div>
    </div>
    </div>
    <!-- Display expense records -->
    <div class="container">
    <div id="list" class="row">
    <aura:iteration items="{!v.expenses}" var="expense">
        <unid:expenseList expense="{!expense}"/>
    </aura:iteration>
    </div>
    </div>
    </div><!--./bootstrap-sf1-->
    </aura:component>

formController.js

({
    doInit : function(component, event, helper) {
        //Update expense counters
        helper.getExpenses(component);
    },//Delimiter for future code
    createExpense : function(component, event, helper) {
        var amtField = component.find("amount");
        var amt = amtField.get("v.value");
        if (isNaN(amt)||amt==''){
        amtField.setValid("v.value", false);
        amtField.addErrors("v.value", [{message:"Enter an expense amount."}]);
        }
        else {
        amtField.setValid("v.value", true);
        var newExpense = component.get("v.newExpense");
        helper.createExpense(component, newExpense);
            $A.log("log one arg"+newExpense);
        }
    },//Delimiter for future code

})

Anyone face this before.

One of the lines that may be causing the error is below

 <ui:inputNumber aura:id="amount" label="Amount"
class="form-control"
value="{!v.newExpense.unid__Amount__c}"
placeholder="20.80" required="true"/>

Replace to have proper field name

 <ui:inputNumber aura:id="amount" label="Amount"
class="form-control"
value="{!v.newExpense.Amount__c}"
placeholder="20.80" required="true"/>

Note namespace is not essential for lightning components now and previously it was needed .So some part of code from the previous releases may be irrelevant and causing issues

Use the latest developer guide

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/

  • I already did that...but still not working – unidha Oct 2 '15 at 5:32
  • You will have to remove namespace reference everywhere. – Mohith Shrivastava Oct 2 '15 at 11:29
  • everywhere mean in the whole org?i m thinking to just remove the whole apps and start all over again – unidha Oct 6 '15 at 0:45
  • In the whole code ..Yes start from scratch if you can – Mohith Shrivastava Oct 6 '15 at 2:24

Support for the c namespace in organizations that have set a namespace prefix is incomplete. Source: Using Your Organization's Namespace

You'll have to update the code wherever you're referencing the expense object and its fields. Change np to your own namespace prefix.

<aura:attribute name="expenses" type="np.Expense__c[]"/> 
<aura:attribute name="newExpense" type="np__Expense__c"
                default="{ 'sobjectType': 'np__Expense__c',
                         'Name': '',
                         'np__Amount__c': 0,
                         'np__Client__c': '',
                         'np__Date__c': '',
                         'np__Reimbursed__c': false
                        }"/>

Update the expressions in the input fields as well.

value="{!v.newExpense.np__Amount__c}"
value="{!v.newExpense.np__Client__c}"
value="{!v.newExpense.np__Date__c}"
value="{!v.newExpense.np__Reimbursed__c}"/>

In formHelper.js, make sure you have total += e.np__Amount__c;too. Follow the code in the tutorial and it should work.

You can resolve this error by,

changing the code

total += e.Amount__c;

to

 if( expenses[i] != null ) {
            total += expenses[i].Amount__c;
 }

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.