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.

  • 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? – dBeltowski Aug 18 '16 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. – SFDCRookie Aug 18 '16 at 14:50
  • 4
    Does the error give you a line number? Nothing I see in this code should give this error. – dBeltowski Aug 18 '16 at 14:52
  • Try to run the query in query editor and see what you get. Do you get any records? – SfdcBat Aug 18 '16 at 14:59
  • @SfdcBat- Yes , records are shown in Query editor – SFDCRookie Aug 18 '16 at 16:19
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.

  • 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. – Adrian Larson Aug 18 '16 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. – crmprogdev Aug 18 '16 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. – Adrian Larson Aug 18 '16 at 18:31
  • 1
    @crmprogdev-Adding another observation . If I change the method to static one , it's executing perfectly . Any reason why so? – SFDCRookie Aug 18 '16 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. – crmprogdev Aug 18 '16 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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