1

I think I am missing something basic. I have two select lists: Region and District. Based on region selection, the district values should get updated.

Following is the code for VF:

<apex:page Controller="UserStatsController">
<apex:sectionHeader title="Goal Revision Dashboard" subtitle="District View"/>
<apex:pageBlock title="User Stats">
<apex:form >
 <apex:outputLabel value="Region" />
    <apex:selectList value="{!regn}" size="1" id="Region" required="true">
        <apex:selectOptions value="{!Valuesr}" />
        <apex:actionSupport event="onchange" reRender="districtSelect"/>
    </apex:SelectList>
<apex:outputPanel id="districtSelect">
    <apex:outputLabel value="District"/>
    <apex:selectList value="{!dist}" size="1" id="dist" required="true">
        <apex:selectOptions value="{!Values}"/>
    </apex:selectList>
    </apex:outputPanel>
</apex:page>

And following is the controller

public with sharing class UserStatsController {

public string regn {get; set;}
public string dist {get; set;}    

public list <SelectOption> getValuesr()
{
    list <selectoption> lsreg = new List<selectoption> ();
    lsreg.add(new SelectOption('', '--None--'));
    for (User_Stats__C Rgn : [SELECT Region__c FROM User_Stats__C where Region__c != null and Region__c != 0 and District__c != null and District__c != '0' limit 10])
    {
        lsreg.add(new selectoption(string.valueOf(Rgn.Region__c), string.valueOf(Rgn.Region__c)));
    }
    return lsreg;
}

public list <SelectOption> getValues()
{
    list <selectoption> ls = new List<selectoption> ();
    ls.add(new SelectOption('', '--None--'));
    Integer regnInt = (regn!=null) ? Integer.valueOf(regn) : -1;
    for (User_Stats__C dist : [SELECT District__c FROM User_Stats__C where Region__c =: regnInt and District__c != '' and Region__c != null and Region__c != 0 limit 10])
    {
        ls.add(new selectoption(dist.District__c, dist.District__c));
    }
    return ls;
}
}

Thanks.

3 Answers 3

1

The error is coming from your query for dist, specifically the where clause:

Region__c =: integer.valueof(apexpages.currentpage().getparameters().get('Regn'))

'Regn' is null when the page loads. Additionally, there appears to be a problem with your page DOM. The way it is structured, I don't think you'll get your getters and setters working in tandem they way you want. I've refactored your code a bit. The VF may not be entirely correct for your case since we're only seeing a snippet. But with this refactor, you can leverage the setter for regn properly and rerender the dist picklist using actionsupport. Also, I've added in a blank initial value for regn and dist so that the user is forced to make a selection, thus firing the onchange event.

VF

<apex:page Controller="UserStatsController">
    <apex:sectionHeader title="Goal Revision Dashboard" subtitle="District View"/>
    <apex:form >
        <apex:pageBlock title="User Stats">
            <apex:pageBlockSection>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Region" />
                    <apex:selectList value="{!regn}" size="1" id="region" required="true">
                        <apex:selectOptions value="{!valuesr}" />
                        <apex:actionSupport id="actionSupport" event="onchange" reRender="districtSelect"/>
                    </apex:SelectList>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageBlockSection id="districtSelect">
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="District"/>
                    <apex:selectList value="{!dist}" size="1" id="dist">
                        <apex:selectOptions value="{!Values}"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller

public with sharing class UserStatsController {

    public string regn {get; set;}
    public string dist {get; set;}    

    public list <SelectOption> getValuesr()
    {
        list <selectoption> lsreg = new List<selectoption> ();
        lsreg.add(new SelectOption('', '--None--'));
        Set<String> regionSet = new Set<String>();
        for (User_Stats__C Rgn : [SELECT Region__c FROM User_Stats__C where Region__c != null and Region__c != 0 and District__c != null and District__c != '0' limit 10])
        {
            String region = string.valueOf(Rgn.Region__c);
            if(!regionSet.contains(region)) lsreg.add(new selectoption(region, region));
            regionSet.add(region);
        }
        return lsreg;
    }

    public list <SelectOption> getValues()
    {
        list <selectoption> ls = new List<selectoption> ();
        ls.add(new SelectOption('', '--None--'));
        Integer regnInt = (regn!=null) ? Integer.valueOf(regn) : -1;
        Set<String> distSet = new Set<String>();
        for (User_Stats__C dist : [SELECT District__c FROM User_Stats__c where Region__c =: regnInt and District__c != null and Region__c != null and Region__c != 0 limit 10])
        {
            if(!distSet.contains(dist.District__c)) ls.add(new selectoption(dist.District__c, dist.District__c));
            distSet.add(dist.District__c);
        }
        return ls;
    }
}
9
  • Thanks a lot Tim! it solved most of the problem. The nullpointer error is gone! However, now when I select a value in Region, the district select list does not have any value to select and refers to only "None"
    – Robin
    Commented Jan 4, 2014 at 21:14
  • Can you post your full VF page? Maybe still a dom issue.
    – Tim Smith
    Commented Jan 4, 2014 at 21:17
  • I have edited the main post to show the full VF page. Thanks.
    – Robin
    Commented Jan 4, 2014 at 21:53
  • 1
    @Robin - If this answer worked for you, you can accept it by checking the little checkmark near the down arrow and upvote it by checking the up arrow. Check out the tour page for more information on how the SFSE works. Thanks! :-) Commented Jan 5, 2014 at 12:49
  • 1
    I accepted the answer. This is my first post so I am really new to this. I tried voting it up.but it says i dont have enough reputation point (minimum 15 required. :P) will do it soon, once I am eligible to.
    – Robin
    Commented Jan 5, 2014 at 17:45
0

Looks like dist.District__c is null.

You might want to add condition to the SOQL query:

and District__c != '' and District__c != null
1
  • Tried. Still the same error!
    – Robin
    Commented Jan 4, 2014 at 19:04
0

Line 20 points to the NullPointerException being in your SOQL Query. I've extracted it below:

SELECT District__c 
FROM User_Stats__C 
where Region__c =: integer.valueof(apexpages.currentpage().getparameters().get('Regn')) 
and District__c != '' 
and Region__c != null 
and Region__c != 0 limit 10

It is likely that the apexpages.currentpage().getparameters().get('Regn') is returning null, meaning it is not found. Then the Integer.valueOf(null) is throwing the exception.

If you want to use the selected value from your page then you can access it directly without using the apexpages.currentPage().getParameters().get('Regn'). You should change the variable name of your loop iteration variable, because it has the same name as the Regn String controller.

Then in your query:

for (User_Stats__C RegnUserStats : [
    SELECT District__c 
    FROM User_Stats__C 
    where Region__c = :Regn
    and District__c != '' 
    and Region__c != null 
    and Region__c != 0 limit 10
]) {
   // process it
}

You must log in to answer this question.

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