Search code examples
javaerror-handlingfortify

Fortify Test Error: never uses the initial value it assigns to the variable


I get this following error: The method getCDsByToken() in CDsImplTl.java never uses the initial value it assigns to the variable paymentCDApp on line 141

and this here is my code:

public GetCDsByTokenResponse getCDsByToken(String token) throws Exception {
        apiKey.setKey();
        IfPaymentCDApp paymentCDApp = new IfPaymentCDApp();

        try {
            String customerId = getCustomerIdByCDId(token);
            RetrieveCDCommand retrieveCDCommand = getRetrieveCDCommand(customerId, token);
            CD cD = null;
            cD = retrieveCDCommand.execute();
            paymentCDApp = AppUtils.mapStripeCDToExtCD(cD);
        }

I don't understand the error message so far, what do I have to look up? I mean everything is used what is not correct here?


Solution

  • IfPaymentCDApp paymentCDApp = new IfPaymentCDApp();
    

    In the above code, you are assigning paymentCDApp with an object. But, later in the try block, you are assigning the same variable another value;

    paymentCDApp = AppUtils.mapStripeCDToExtCD(cD);
    

    The value you assigned before (given by IfPaymentCDApp paymentCDApp = new IfPaymentCDApp();) is never used between that line and the line which contains paymentCDApp = AppUtils.mapStripeCDToExtCD(cD);. That's why you are getting such an error.

    You can initialize the variable to null at the beginning which is a much better practice.

    apiKey.setStripeApiKey();
    IfPaymentCDApp paymentCDApp = null;
    
    try {
        String customerId = getCustomerIdByCDId(token);
        RetrieveCDCommand retrieveCDCommand = getRetrieveCDCommand(customerId, token);
        CD cD = null;
        cD = retrieveCDCommand.execute();
        paymentCDApp = AppUtils.mapStripeCDToExtCD(cD);
    }
    

    You may need to do a null check after the try block in order to determine whether the correct value was assigned. I cannot give any suggestion since you haven't put the code after the try block. However, initializing the variable to null will solve your problem.