[Solved] java.util.MissingFormatArgumentException

In this short post, we will learn about java.util.MissingFormatArgumentException in Java. Just like java.util.IllegalFormatConversionException, java.util.MissingFormatArgumentException is an Unchecked exception. It is thrown when there is a format specifier which does not have a corresponding argument or if an argument index refers to an argument that does not exist. Just like always, first, we will produce the java.util.MissingFormatArgumentException before moving on to the solution.

Read Also: [Solved] java.util.IllegalFormatPrecisionException in Java

[Fixed] java.util.MissingFormatArgumentException

Example 1: Producing the exception by using a format specifier which does not have a corresponding argument


We can easily produce this exception by having a format specifier which does not have a corresponding argument as shown below:

public class MissingFormatArgumentExceptionExample {
    public static void main(String args[]) {
      //First, we will initialize variable str    
      String str = "100";
      /* Below line will throw MissingFormatArgumentException
      as we have a format specifier %s that does not have a
      corresponding argument */                 
System.out.printf(" %s "+ str);
} }

Output:
Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%s'


Explanation:

The root cause of this exception is that format specifier %s which does not have a corresponding argument. This exception also occurs if an argument index refers to an argument that does not exist, but that is not the case here.

Solution:

The above runtime exception can be resolved by replacing + operator with , as shown below:

public class MissingFormatArgumentExceptionExample {
    public static void main(String args[]) {
      //First, we will initialize variable str with value 100    
      String str = "100";
      /* Below line will not throw MissingFormatArgumentException
      as we have replaced + operator with , */                       
System.out.printf(" %s ", str);
} }

Output:
100


That's all for today. Please mention in the comments in case you are still facing the exception java.util.MissingFormatArgumentException in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry