3

I'm trying to perform a sort on a list of custom sObjects using the 'implements comparable' wrapper method. I'm running into some error that it seems isn't affecting anyone else. My custom object looks like so:

gAlert:{
    title: text,
    published: date
}

I'm trying to sort by published.

I tried implementing the following:

public class gAlertWrapper implements Comparable
{
    public Integer compareTo(Object compareTo) 
    {
        gAlertWrapper gAlertWrapper = (gAlertWrapper) compareTo;
        if (gAlert__c.published__c == gAlertWrapper.gAlert__c.published__c) return 0;
        if (gAlert__c.published__c > gAlertWrapper.gAlert__c.published__c) return 1;
        return -1;        
    }
}

I get the following error:

Variable does not exist: gAlert__c

from the following line:

if (gAlert__c.published__c == gAlertWrapper.gAlert__c.published__c) return 0;

The problem seems to be in gAlertWrapper.gAlert__c.published_c

What am I doing incorrectly?

I tried implementing a constructor, like so:

public gAlert__c gA;
public gAlertWrapper(gAlert__c g) {
        gA = g;
    }

But that didn't seem to work either... How can I piece this together?

Thanks!

EDIT: Everything seemed to be working fine, as there were no compiling errors. But at run time there was a

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

I know it's not the xml I'm parsing in - it's the same stuff from before.

Here is the code that is trying to save values to entry.gA:

public static list<gAlert__c> getAlerts(string url){
        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setMethod('GET');

        Http http = new Http();
        HttpResponse res = http.send(req);
                Dom.Document doc = res.getBodyDocument();
                Dom.XMLNode address = doc.getRootElement();

                string title = '';
                integer count = 1;

        list<gAlert__c> results = new list<gAlert__c>();
        gAlertWrapper entry = new gAlertWrapper();      
        for(Dom.XMLNode child:address.getChildElements()){
            string childElement = child.getName();
            if(childElement == 'title'){
                title = child.getText();
            }
            else if(childElement == 'entry'){
                for(Dom.XmlNode grandChild : child.getChildElements()){
                        string grandChildElement = grandChild.getName();
                    if(grandChildElement == 'title'){
                        entry.gA.title__c = grandChild.getText();
                    }
                    else if(grandChildElement == 'link'){
                        entry.gA.link__c = grandChild.getAttributeValue('href', null);
                    }
                    else if(grandChildElement == 'published'){
                        entry.gA.published__c = grandChild.getText().replace('Z','').replace('T',' ');
                    }
                    else if(grandChildElement == 'content'){
                        entry.gA.content__c = grandChild.getText();
                    }
                    else if(grandChildElement == 'author'){ //this is the last element in the tree
                        results.add(entry.gA.clone(false,false,false,false));
                        entry.gA.clear();
                    }
                }
            }
        }
        return results;
    }
    public class gAlertWrapper implements Comparable{
        public gAlert__c gA;
        //public gAlertWrapper(gAlert__c g) {
        //gA = g;
        //}
        public Integer compareTo(Object compareTo) {
            gAlertWrapper gAlertWrapper = (gAlertWrapper) compareTo;
            if (gA.published__c == gAlertWrapper.gA.published__c) return 0;
            if (gA.published__c > gAlertWrapper.gA.published__c) return 1;
            return -1; 
        }
    }

I'm getting the error at the line:

entry.gA.title__c = grandChild.getText();

I know it isn't the grandVhild.getText(), since that hasn't changed. What is strange is that I would imagine the null pointer would reflect the right side of the equation no?

I guess I'm really fumbling my way around learning how to use this comparable class..

Thank you so much for your patience and help. Happy to upvote everything you've written - it all deserves it.

2
  • Can you post your wrapper class? Mar 11, 2015 at 19:30
  • I had thought the Public Class gAlertWrapper is the wrapper class... ?
    – ZAR
    Mar 11, 2015 at 20:07

1 Answer 1

5
public class gAlertWrapper implements Comparable{
   public Integer compareTo(Object compareTo) {
    gAlertWrapper gAlertWrapper = (gAlertWrapper) compareTo;
    if (gA.published__c == gAlertWrapper.gA.published__c) return 0;
    if (gA.published__c > gAlertWrapper.gA.published__c) return 1;
    return -1;        
  }
}

Following is the correct syntax

Corrected completed code

public static list<gAlert__c> getAlerts(string url){
    HttpRequest req = new HttpRequest();
    req.setEndpoint(url);
    req.setMethod('GET');

    Http http = new Http();
    HttpResponse res = http.send(req);
            Dom.Document doc = res.getBodyDocument();
            Dom.XMLNode address = doc.getRootElement();

            string title = '';
            integer count = 1;

    list<gAlert__c> results = new list<gAlert__c>();
    gAlert__c gAlert=new gAlert__c();
    gAlertWrapper entry = new gAlertWrapper(gAlert);      
    for(Dom.XMLNode child:address.getChildElements()){
        string childElement = child.getName();
        if(childElement == 'title'){
            title = child.getText();
        }
        else if(childElement == 'entry'){
            for(Dom.XmlNode grandChild : child.getChildElements()){
                    string grandChildElement = grandChild.getName();
                if(grandChildElement == 'title'){
                    entry.gA.title__c = grandChild.getText();
                }
                else if(grandChildElement == 'link'){
                    entry.gA.link__c = grandChild.getAttributeValue('href', null);
                }
                else if(grandChildElement == 'published'){
                    entry.gA.published__c = grandChild.getText().replace('Z','').replace('T',' ');
                }
                else if(grandChildElement == 'content'){
                    entry.gA.content__c = grandChild.getText();
                }
                else if(grandChildElement == 'author'){ //this is the last element in the tree
                    results.add(entry.gA.clone(false,false,false,false));
                    entry.gA.clear();
                }
            }
        }
    }
    return results;
}
public class gAlertWrapper implements Comparable{
    public gAlert__c gA;
    public gAlertWrapper(gAlert__c g) {
    gA = g;
    }
    public Integer compareTo(Object compareTo) {
        gAlertWrapper gAlertWrapper = (gAlertWrapper) compareTo;
        if (gA.published__c == gAlertWrapper.gA.published__c) return 0;
        if (gA.published__c > gAlertWrapper.gA.published__c) return 1;
        return -1; 
    }
}
8
  • So that seems to have worked.. But Now I'm getting another error: Constructor Not Defined [apexclass.gAlertWrapper].<Constructor>(). I'm probably not putting the constructor in the right place. I've added a section to my question with the new code
    – ZAR
    Mar 11, 2015 at 20:36
  • 1
    return gA.published__c.daysBetween(gAlertWrapper.ga.published__c); Here's a little trick-- you don't need to return 0, 1, or -1. Just a number between -MAX_INT and +MAX_INT-1, where MAX_INT is 2,147,483,648.
    – sfdcfox
    Mar 11, 2015 at 21:13
  • 1
    public gAlertWrapper(gAlert__c g) { gA = g; } Please delete this if you dont need constructor . Mar 11, 2015 at 21:16
  • Ok, deleted constructor... But now the object doesn't have all the fields that gAlert had. For instance, gAlert has a title field. When I create a new gAlertWrapper object, it doesn't seem to have these fields. I've updated the above question to reflect the problem
    – ZAR
    Mar 11, 2015 at 21:41
  • 1
    Its entry.gA.title__c = grandChild.getText(); please mark as answered .Thanks Mar 11, 2015 at 21:46

You must log in to answer this question.

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