function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jblon1973jblon1973 

Cannot Display results: Attempt to de-reference a null object

I have written a controller extension and test class that covers 86% of it. The controller woks very well in the sanbox without issue. The problem I have is when it is moved to production. I get the dreaded Attempt to Dereference a Null Object where the graph is supposed to be. There is no configuration difference between the two environments other than an Appexchange package that doe not even interact with the object the controller affects. My code is below:

 

Class

public with sharing class TestResultGraphConroller {

  
    public TestResultGraphConroller(ApexPages.StandardController stdController){
    }
    
    
    public String getTimeVolume(){
        return getTimeVolume(null);
    }
    
    public String getTimeVolume(List<Id> ids){
        GoogleViz gv = new GoogleViz();              
        gv.cols = new list<GoogleViz.col> { 
            new GoogleViz.Col('col2','Interval','number'),
            new GoogleViz.Col('col1','Channel Volume','number')
            };
        decimal interval = 0;
        for(Trial__c tv : [Select Id, Channel_Volume__c from Trial__c])
        {
          Integer v;
          List<String> channelvolumevalues = tv.Channel_Volume__c.split(' ');
          for(String cv: channelvolumevalues)
          {
            GoogleViz.row r = new GoogleViz.row();
            r.cells.add ( new GoogleViz.cell( interval));//interval
            r.cells.add ( new GoogleViz.cell( cv ));//X-Axis
            gv.addRow( r ); 
            interval = interval + .01;
            }
        }
        return gv.toJsonString();
    }
    
    public String getFlowVolume(){
        return getFlowVolume(null);
    }
    
  public String getFlowVolume(List<Id> ids){
        GoogleViz gv = new GoogleViz();              
        gv.cols = new list<GoogleViz.col> { 
            new GoogleViz.Col('col1','Channel Volume','number'),
            new GoogleViz.col('col2','Channel Flow','number')    
            };              
               
        for(Trial__c tfv : [Select Id, Channel_Volume__c, Channel_Flow__c From Trial__c])
        {                                                          
            List<String> channelvolumevalues = tfv.Channel_Volume__c.split(' ');
            List<String> channelflowvalues = tfv.Channel_Flow__c.split(' ');
            for(integer i=0; i< channelvolumevalues.size(); i++) 
            {
              String cv = channelvolumevalues[i]; 
              String cf = channelflowvalues[i]; 
              GoogleViz.row r = new GoogleViz.row();
              r.cells.add ( new GoogleViz.cell( cv ));//Y-Axis
              r.cells.add ( new GoogleViz.cell( cf ));//X-Axis
              gv.addRow( r ); 
            }
        }

        return gv.toJsonString();
    } 
}

 And here is my test class

@isTest
private class testTestResultGraphConroller {

    static testMethod void testConroller() {
    	PageReference pageRef = Page.TimeVolumeLine;
    	Test.setCurrentPage(pageRef);
    	
    	Trial_Session__c tts1 = new Trial_Session__c(); 
    	
    	insert tts1;
    	
    	Trial__c tt1 = new Trial__c();
    	tt1.Channel_Flow__c = '-0.01114792 0.0204 0.051 0.0714 0.0918';
    	tt1.Channel_Volume__c = '0.9639643 0.9641174 0.9645764 0.9652394';
    	tt1.Trial_Session__c = tts1.Id; 
    	
    	insert tt1;    
    	
    	ApexPages.currentPage().getParameters().put('retURL','/'+tt1.id);
    	
    	TestResultGraphConroller TRGC = new TestResultGraphConroller(new ApexPages.StandardController(tt1));
    	
    	TRGC.getTimeVolume(null);
    	TRGC.getFlowVolume(null); 
    	
    }
}

 Anyone have any bright ideas that they can help me with?

Best Answer chosen by Admin (Salesforce Developers) 
jblon1973jblon1973

Thank you for the response. I actually solved it on my own by ensuring that I was capturing the correct record ID in my select statements. It was actually selecting the oncorrect records.

 

The solution: Capture the record ID of the page and assign it, then in my select compare the record ID on the page with what I am trying to query to get the correct one with data.

 

JP

All Answers

Noam.dganiNoam.dgani

Couple of questions:

 

1. What line fires the exception?

2.What's integer v for?

3.Why the ID lists as arguments? Are they used?

jblon1973jblon1973

Thank you for the response. I actually solved it on my own by ensuring that I was capturing the correct record ID in my select statements. It was actually selecting the oncorrect records.

 

The solution: Capture the record ID of the page and assign it, then in my select compare the record ID on the page with what I am trying to query to get the correct one with data.

 

JP

This was selected as the best answer