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.
Best Answer
Following is the correct syntax
Corrected completed code