Blog |

How to Fix the Missing Format Argument Exception in Java?

How to Fix the Missing Format Argument Exception in Java?
Table of Contents

The MissingFormatArgumentException is an unchecked exception in Java that occurs when a format specifier does not have a corresponding argument or if an argument index points to an argument that does not exist.

Since the MissingFormatArgumentException is thrown at runtime, it does not need to be declared in the throws clause of a method or constructor.

 

What Causes MissingFormatArgumentException

The MissingFormatArgumentException is thrown when using a format specifier that does not have a corresponding argument or if an argument index refers to an argument that does not exist. For example, the %d format specifier requires an integer to be passed to it, and if no argument is passed, a MissingFormatArgumentException is thrown.

 

MissingFormatArgumentException Example

Here is an example of an MissingFormatArgumentException thrown when using a format specifier that does not have a corresponding argument:

public class MissingFormatArgumentExceptionExample {
    public static void main(String args[]) {
        String str = "Hello World";
        System.out.printf("%s" + str);
    }
}

Since the %s format specifier expects a corresponding string argument but no such argument exists, running the above code throws the MissingFormatArgumentException:

Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%s'
    at java.base/java.util.Formatter.format(Formatter.java:2672)
    at java.base/java.io.PrintStream.format(PrintStream.java:1209)
    at java.base/java.io.PrintStream.printf(PrintStream.java:1105)
    at MissingFormatArgumentExceptionExample.main(MissingFormatArgumentExceptionExample.java:4)

 

How to Fix MissingFormatArgumentException

To avoid the MissingFormatArgumentException, it should be ensured that format specifiers used in code should have corresponding arguments and that the arguments exist at the correct argument index.

In the above example, the exception can be resolved by replacing the + operator with , which will pass the string as an argument to the %s format specifier

public class MissingFormatArgumentExceptionExample {
    public static void main(String args[]) {
        String str = "Hello World";
        System.out.printf("%s", str);
    }
}

Running the above code produces the correct output as expected:

Hello World

 

Track, Analyze and Manage Errors With Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Java errors easier than ever. Sign Up Today!

Related Resources

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape