0

I try to add objects from 'accbetween' to 'idlist' (see the code below):

public class SearchTestApex {

    public list <Fund__c> idlist {get;set;}

    public list <Fund__c> accbetween  = new list<Fund__c>();

    public String idsaved {get;set;}

    public String searchKey {get;set;}

    public SearchTestApex( ) {
    }

    public void search(){

        String searchquerybetween='SELECT Id, Product__r.Id, Product__r.Name, Date__c FROM Fund__c WHERE Code__c LIKE \'%'+searchKey+'%\' ORDER BY Product__r.Id, Date__c DESC';

        accbetween= Database.query(searchquerybetween);

        //Then, I simply try to add the first value of accbetween to idlist:

        idlist.add(accbetween[0]);

        }
    }

There is a VisualForce code which is supposed to display 'idlist' but it returns the error as follow:

Attempt to de-reference a null object

I assume I might have to initialize 'idlist' or there could be an incompatibility between these types of list. Why do I get this error? How can I fix it?

2 Answers 2

2

Your problem is you never construct idList, so when you call idList.add(...), you are really calling null.add(...). This situation leads to an attempt to de-reference a null variable.

If you want for some reason to only add one result from your SOQL to the list, then add LIMIT 1 to your String, and simply assign the query result to your property.

String soql = 'SELECT ... FROM Fund__c WHERE ... ORDER BY ... LIMIT 1';
idList = Database.query(soql);

If you don't like that approach, then you should instead instantiate a new list:

List<Fund__c> records = Database.query(...);
idList = new List<Fund__c>();
idList.add(records[0]);
7
  • Hello Adrian and thank you for your answer. I wanna add more values to idlist but it was just a simple example to show that I don't know how to assign a value to this kind of list. Also, I understand your second solution could work but with this type of list I won't be able to call it in a Visualforce code, right? Commented Jun 11, 2019 at 15:51
  • What do you mean you wouldn't be able to call it in Visualforce? The only factor in whether or not you can access it there is that it has a pubic getter (e.g. { get; set; }.
    – Adrian Larson
    Commented Jun 11, 2019 at 16:05
  • Sorry, I am not making myself clear. When I implement 'public list <Fund__c> idlist = new list<Fund__c>();' then, idlist.add(accbetween[0]); doesn't return any problem. The problem being that I cannot call idlist in a Visualforce environment. Commented Jun 11, 2019 at 16:08
  • That's because you changed the code in the wrong place and remove the very getter I mention above. You need to instantiate the list within the method, before adding elements to it.
    – Adrian Larson
    Commented Jun 11, 2019 at 16:12
  • So as far as I understood, I should get the code below - but it's not returning anything (there is no error displayed though). Commented Jun 11, 2019 at 16:27
0

i think Database.query(searchquerybetween) returns no records, so you have to check weather query returns records or not. please update search method with below code.

 public void search(){

    String searchquerybetween='SELECT Id, Product__r.Id, Product__r.Name, Date__c FROM Fund__c WHERE Code__c LIKE \'%'+searchKey+'%\' ORDER BY Product__r.Id, Date__c DESC';

    accbetween= Database.query(searchquerybetween);
     idlist = new List<Fund__c>();
    //Then, I simply try to add the first value of accbetween to idlist:
 if(accbetween.size() > 0){idlist.add(accbetween[0])};

    }
2
  • You're thinking of the wrong error.
    – sfdcfox
    Commented Jun 11, 2019 at 15:00
  • thank you @sfdcfox to find my mistake. updated
    – sdandamud1
    Commented Jun 11, 2019 at 15:06

You must log in to answer this question.

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