我设置了变量的值,但是当我使用get时,参数为null

时间:2014-03-08 04:16:14

标签: java get

想知道我的java程序可能出现什么问题。

我在java程序中的特定类中设置了“id”变量的值 我打印了变量的值以确保它已被设置。

我使用脚本在同一个java程序中运行一个类,然后获取“id”的值,但它返回null。

我的代码有什么问题?

谢谢!

所以我将ChargeParamData作为单例类,然后由ChargeFacade访问以设置/获取数据。

我创建的脚本将调用Chargefacade类的notifyAuthenticationStatus。但是当它被称为pluginId时为null。

这是get / set类

public class ChargeParamData {

    private static final ChargeParamData instance = new ChargeParamData();

    /**
     * Returns the instance of the class
     * 
     * @return instance of the class
     */
    public static ChargeParamData getInstance() {
        return instance;
    }

    /**
     * Constructor
     * 
     */
    private ChargeParamData() {
    }

    //for PluginId
    private static String pluginId;

    public static void setPluginId(String id) {
        pluginId = id;
        System.out.println("pluginId to set for Charge is " + pluginId);
    }

    public static String getPluginId() {
        System.out.println("pluginId to get for console test is " + pluginId);
        return pluginId;
    }
}

这是使用该类的代码:

public final class ChargeFacade {
    private static ChargeParamData chargeParam = ChargeParamData.getInstance();

    public static void chargePluginInit() {
        String pid = null;
        PluginDescriptor[] pdList = sample.listPlugin();
        pid = pdList[0].getId();
        createContext(pid);
    }

    private static void createContext(String pluginId) {
        chargeParam.setPluginId(pluginId);
        logger.info("Charge Service created for " + pluginId);
    }

    public static String notifyAuthenticationStatus(AuthenticationStatus authStat) throws DispatcherException {
        String pluginId = chargeParam.getPluginId(); // <-- this part is null
        try {
            JSONObject json = new JSONObject();
            authStat.putInto(json);
            retVal = sample.callFunction(pluginId, "notifyAuthenticationStatus", json.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return retVal;
    }
}

1 个答案:

答案 0 :(得分:0)

由于getter和setter方法在ChargeParamData

中定义
    public class ChargeParamData {

//for PluginId
private static String pluginId;

public static void setPluginId(String id) {
    pluginId = id;
    System.out.println("pluginId to set for Charge is " + pluginId);
}

public static String getPluginId() {
    System.out.println("pluginId to get for console test is " + pluginId);
    return pluginId;
}
}

this is the code that uses the class

public final class ChargeFacade {
private static ChargeParamData chargeParam = ChargeParamData.getInstance();

public static void chargePluginInit() {
    String pid = null;
PluginDescriptor[] pdList = sample.listPlugin();
    pid = pdList[0].getId();
    createContext(pid);
}

private static void createContext(String pluginId) {

    ChargeParamData .setPluginId(pluginId);
    logger.info("Charge Service created for " + pluginId);
}


public static String notifyAuthenticationStatus(AuthenticationStatus authStat) throws DispatcherException {
    String pluginId = ChargeParamData .getPluginId(); // <-- this part is null
    try {
        JSONObject json = new JSONObject();
        authStat.putInto(json);
        retVal = sample.callFunction(pluginId, "notifyAuthenticationStatus",
                json.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return retVal;
}
}