Salesforce:尝试取消引用null对象

时间:2015-08-05 13:48:35

标签: salesforce apex

我已经搜索过并发现了几篇关于此错误的文章,但我是APEX和Visualforce的新手,所以我无法弄清楚如何将解决方案应用到我的工作中继续努力。任何帮助,将不胜感激。我在下面的代码中尝试做的是创建一个嵌入在Opportunity记录上的visualforce页面,该页面显示2个多选选项列表,其中一个依赖于在第一个选择的选项。

public class WatermelonOppController {

//added an instance varaible for the standard controller
private ApexPages.StandardController controller {get; set;}
// the actual opportunity
private Opportunity o;

public String[] parentPicklistVal {public get; public set;}
public String[] childMultiPicklistVal {public get; public set;}
public String childSinglePicklistVal {public get; public set;}

// maps to hold your dependencies between picklists
private Map<String, List<String>> parentDepMap;
private Map<String, List<String>> childDepMap;

private String[] parentOpts = new String[] { 'parent option 1', 'parent option 2' };
private String[] childMultiOpts = new String[] { 'child multi 1', 'child multi 2', 'child multi 3' };
//private String[] childSingleOpts = new String[] { 'child single 1', 'child single 2', 'child single 3' };

public WatermelonOppController(ApexPages.StandardController controller) {

    //initialize the stanrdard controller
    this.controller = controller;
    //this.o = (Opportunity)controller.getRecord();

}

public WatermelonOppController() {
    // init dependency maps
    parentDepMap = new Map<String, List<String>>();
   // childDepMap = new Map<String, List<String>>();

    // pick which child options to display for which parent value
    parentDepMap.put(parentOpts[0], (new String[]{childMultiOpts[0], childMultiOpts[1]}));
    parentDepMap.put(parentOpts[1], (new String[]{childMultiOpts[1], childMultiOpts[2]}));

    // pick which single-select options to display for which child value
    //childDepMap.put(childMultiOpts[0], (new String[]{childSingleOpts[0], childSingleOpts[2]}));
    //childDepMap.put(childMultiOpts[1], (new String[]{childSingleOpts[1], childSingleOpts[2]}));
    //childDepMap.put(childMultiOpts[2], childSingleOpts);    // or if you want to show them all?
}

public List<SelectOption> getParentPicklistOptions() {
    List<SelectOption> selectOpts = new List<SelectOption>();
    for ( String s : parentOpts )
        selectOpts.add(new SelectOption(s, s));
    return selectOpts;
}

public List<SelectOption> getChildMultiPicklistOptions() {
    List<SelectOption> selectOpts = new List<SelectOption>();
    if ( parentPicklistVal != null && parentPicklistVal.size() > 0 ) {
        // build a set of values to avoid dupes, since there may be overlapping dependencies
        Set<String> possibleOpts = new Set<String>();
        for ( String val : parentPicklistVal )
            possibleOpts.addAll(parentDepMap.get(val));
        for ( String s : possibleOpts )
            selectOpts.add(new SelectOption(s, s));
    }
    return selectOpts;
}

/*
public List<SelectOption> getChildSinglePicklistOptions() {
    List<SelectOption> selectOpts = new List<SelectOption>();
    if ( childMultiPicklistVal != null && childMultiPicklistVal.size() > 0 ) {
        // build a set of values to avoid dupes, since there may be overlapping dependencies
        Set<String> possibleOpts = new Set<String>();
        for ( String val : childMultiPicklistVal )
            possibleOpts.addAll(childDepMap.get(val));
        for ( String s : possibleOpts )
            selectOpts.add(new SelectOption(s, s));
    }
    return selectOpts;
}
*/
public PageReference actionUpdatePicklistVals() {
    // this doesn't really need to do anything, since the picklists should be updated when their getters call after returning
    return null;
}

}

Visualforce页面:

<apex:page standardController="Opportunity" extensions="WatermelonOppController">
<apex:form >
    <apex:outputPanel id="panel1">
        <apex:selectList value="{!parentPicklistVal}" multiselect="true" size="3">
            <apex:selectOptions value="{!parentPicklistOptions}" />
            <apex:actionSupport event="onchange" action="{!actionUpdatePicklistVals}" rerender="panel1" />
        </apex:selectList>
        <apex:selectList value="{!childMultiPicklistVal}" multiselect="true" size="3">
            <apex:selectOptions value="{!childMultiPicklistOptions}" />
            <apex:actionSupport event="onchange" action="{!actionUpdatePicklistVals}" rerender="panel1" />
        </apex:selectList>

    </apex:outputPanel>
</apex:form>

1 个答案:

答案 0 :(得分:0)

您应该将所有逻辑从默认构造函数移动到参数化构造函数。由于该类是扩展,因此调用标准控制器的参数化构造函数。