0

Can anyone help with this error?

    System.NullPointerException: Attempt to de-reference a null object

    Class.productdata.getMapOfContent: line 24, column 1

Here is the Apex Class. "Line 24" refers to "if(LoopContentData.TagCsv.contains(getTag())) {"

public class productdata {

    LIST<ContentVersion> allContentData= [select Available_To__c, IsLatest,ContentModifiedDate, Title, ReasonForChange, VersionNumber, RatingCount, Description, ContentDocumentId, TagCsv, ContentDocument.ParentId FROM ContentVersion WHERE IsLatest=TRUE];
    LIST<CONTENTWORKSPACE> WorkspaceObjects = [SELECT Name, ID FROM CONTENTWORKSPACE];
    MAP<string,LIST<ContentVersion>> MyMapOfContent = new MAP<string,LIST<ContentVersion>>();
    //string prod = [SELECT Name FROM Product2 WHERE Id = :ApexPages.currentPage().getParameters().get('id')].Name;
    string prod;
    product2 myProduct;

    public productdata(ApexPages.StandardController controller) {
        this.myProduct = (Product2)controller.getRecord();
    }

    public String getTag(){
        return prod = [SELECT Name FROM Product2 WHERE Id = :myProduct.ID].Name;
    }


    public MAP<string,LIST<ContentVersion>> getMapOfContent(){
        for(CONTENTWORKSPACE loopIds : WorkspaceObjects){
            LIST<ContentVersion> theContentVersionMapLoop = new LIST<contentversion>();         
            for(ContentVersion LoopContentData : allContentData){
                if(LoopContentData.ContentDocument.ParentId == loopIds.ID){
                    if(LoopContentData.TagCsv.contains(getTag())) {
                        theContentVersionMapLoop.add(LoopContentData);
                    }
                }
            }
            MyMapOfContent.put(loopIds.Name,theContentVersionMapLoop);
        }
        return MyMapOfContent;
    }

}

I know it has something to do with getTag method but I have no idea why. Thanks!

  • Can you tell us what line 24 is? – Michael Welburn Jan 14 '14 at 18:42
  • "Line 24" refers to "if(LoopContentData.TagCsv.contains(getTag())) {" THANKS! – dmarx Jan 14 '14 at 18:43
  • 1
    Is TagCsv null for any of those records? – Michael Welburn Jan 14 '14 at 18:44
  • Yeah, it very well could be. I'll test that and escape the next few rows if that is the case. Thanks! – dmarx Jan 14 '14 at 19:01
  • Note: you have SOQL (getTag()) inside of nested loops. You could just reference myProduct.Name directly instead of querying for it every iteration. – Mike Chale Jan 14 '14 at 19:04
2

Did you check to see if TagCsv is null for any of those rows? If you attempt an instance method call on a null value, you'll get the "Attempt to De-Reference a Null Object" error.

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.