Search code examples
javablockchainaion

InvocationTargetException: Dapp call failed when using Aion Embedded AVM


I'm getting the following message when I call my contract using maven and the embedded AVM:

[ERROR] Failed to execute goal org.aion4j:aion4j-maven-plugin:0.5.0-beta1:call (default-cli) on project supercontract: Method call failed: InvocationTargetException: Dapp call failed. Code: FAILED, Reason: null -> [Help 1]

I just edited the HelloAvM contract that you get when you mvn initialize the project. Here's the whole contract:

public class HelloAvm {
    // Voting App
    // 1. Users call a function to find out what the question is.
    // 2. They then call another function with their choice.
    // 3. Any use can call the results() function to find out what the current votes are.

    public static int questionNumber = 1;
    public static String questionString = "Is Haggis a real animal?";

    public String getQuestion() {
        return "Question #" + questionNumber + ": " + questionString;
    }

    public void testContract() {
        // This function just returns a string to show that the contract is working.
        BlockchainRuntime.println("This contract is working.");
    }

    public static byte[] main() {
        return ABIDecoder.decodeAndRunWithClass(HelloAvm.class, BlockchainRuntime.getData());
    }
}

I'm not sure why it's failing, and the error message isn't much use. I've looked at other InvocationTargetException errors on StackOverflow, but they don't seem to help this issue.


Solution

  • Figured it out myself. Apparently you need to make each function static for Java contracts. So getQuestion and testContract above should look like this:

    public static String getQuestion() {
            return "Question #" + questionNumber + ": " + questionString;
        }
    
        public static void testContract() {
            // This function just returns a string to show that the contract is working.
            BlockchainRuntime.println("This contract is working.");
        }