此代码中出现Variable does not exist: bk
错误:
String MCLU1 = 'BK2200 0015 -- 41';
if (MCLU1 != null || MCLU1 != '') {
String bk = null;
String bk_full = null;
String bk_extension = '--';
String [] bkAfterSplit = null;
bk_full = MCLU1;
if (bk_full.contains(bk_extension)) {
bkAfterSplit = bk_full.split(' -- ');
bk = bkAfterSplit[0];
System.debug(LoggingLevel.DEBUG, 'bk inside my if : ' + bk);
} else {
bk = bk_full;
System.debug(LoggingLevel.DEBUG, 'bk inside my else : ' + bk);
}
} else {
System.debug(LoggingLevel.DEBUG, 'MCLU1__c is empty');
}
String external_id = 'FRA-BLIP-' + bk;
System.debug(LoggingLevel.DEBUG, 'bk outside my if/else : ' + bk);
System.debug(LoggingLevel.DEBUG, 'external_id : ' + external_id);
找不到第23行上的我的bk
变量。但是,当我使用Java在Eclipse中尝试(几乎)相同的代码时,一切正常。波纹管示例代码:
public class BkDoesNotExist {
public static void main(String[] args) {
String MCLU1__c = "BK2200 0015 -- 41";
// String MCLU1__c = null;
String bk = null;
String bk_full = null;
String bk_extension = "--";
String [] bkAfterSplit = null;
if (MCLU1__c != null || MCLU1__c != "") {
bk_full = MCLU1__c;
if (bk_full.contains(bk_extension)) {
bkAfterSplit = bk_full.split(" -- ");
bk = bkAfterSplit[0];
System.out.println("bk inside my if : " + bk);
} else {
bk = bk_full;
}
} else {
System.out.println("MCLU1__c is empty");
}
String external_id = "FRA-BLIP-"+bk;
System.out.println("bk outside my if : " + bk);
System.out.println("external_id : " + external_id);
}
}
控制台显示:
bk inside my if : BK2200 0015
bk outside my if : BK2200 0015
external_id : FRA-BLIP-BK2200 0015
我在这里想念什么?
答案 0 :(得分:0)
OMG ...我在正确的位置下仅将变量定义为1级...
代替
String MCLU1 = 'BK2200 0015 -- 41';
if (MCLU1 != null || MCLU1 != '') {
String bk = null;
String bk_full = null;
String bk_extension = '--';
String [] bkAfterSplit = null;
bk_full = MCLU1;
必须是
String MCLU1 = 'BK2200 0015 -- 41';
String bk = null;
String bk_full = null;
String bk_extension = '--';
String [] bkAfterSplit = null;
if (MCLU1 != null || MCLU1 != '') {
bk_full = MCLU1;