4

Method : This just one method of the class I am testing from Dev console

public Employee__c getEmployeeDetails()
{
    Employee__c emp = null;
    emp = [SELECT field1__c, field2__c, field3__c FROM Employee__c WHERE Accountid__c ='001L000000ogtry' limit 1];

    return emp;
}

Dev console :

 classname cc = new Classname();
 cc.getEmployeeDetails();

The SELECT query returns records when queried individually.

Any help on why it's occurring. Account id is being taken from url . Here I have hardcoded just to test.

12
  • 1
    Does your code with the Hardcoded Account Id work? Nothing in this code should cause this error as far as I can tell. Perhaps the code you were using to get the Account Id from the URL is the real culprit? Commented Aug 18, 2016 at 14:45
  • @doug The actual code is -- emp = [SELECT field1__c, field2__c, field3__c FROM Employee__c WHERE Accountid__c =: currentRecordId limit 1]; I am getting value of currentRecordId in system.debug. So , that shouldn't be an issue. Commented Aug 18, 2016 at 14:50
  • 4
    Does the error give you a line number? Nothing I see in this code should give this error. Commented Aug 18, 2016 at 14:52
  • Try to run the query in query editor and see what you get. Do you get any records? Commented Aug 18, 2016 at 14:59
  • @SfdcBat- Yes , records are shown in Query editor Commented Aug 18, 2016 at 16:19

2 Answers 2

0

If you're running this from dev console, before running your query, you're essentially saying:

sObject sOb = null; 

rather than:

sObject sOb = new sObject();

It appears to me that you're then assigning the results of a query to a null sObject. Now, if you removed the assignment of the null and simply wrote:

 sObject sOb = [yourquerystring with valid recordID];

then I'd expect you wouldn't be having the problem that's occurring from having initialized your object to null. Instead, you'd be initializing it to the results of the query as it should be. If your query didn't return a record, you'd still get an exception, but it wouldn't be a null pointer exception.

I believe the above explains why you're getting the null pointer exception.

5
  • Using sOb is a bit of a humorous abbreviation. Not sure if that's intentional...at least is's not as bad as std for StandardController but still. Commented Aug 18, 2016 at 18:14
  • @AdrianLarson It's a collaborative community. Feel free to edit if you'd like. ;) I'm just not especially fond of Mr foo and Ms bar. Commented Aug 18, 2016 at 18:27
  • I prefer the more descriptive record. I would only edit your post if you have a factual error, or perhaps if your code wouldn't compile for an obvious reason. That said, double assignment should be fine barring the involvement of final, I'd think. Commented Aug 18, 2016 at 18:31
  • 1
    @crmprogdev-Adding another observation . If I change the method to static one , it's executing perfectly . Any reason why so? Commented Aug 18, 2016 at 18:36
  • In the context you're calling it, it probably should be. From Apex Docs "A static method is used as a utility method, and it never depends on the value of an instance member variable." You created a new instance of your class just before you called the method from Dev Console. Commented Aug 18, 2016 at 18:52
-1

This issue was caused due to one field value being null for one of the object records. Thank you all for your inputs.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.