Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Overview

In this article, we'll demonstrate the reason behind NoSuchFieldError and discover how to resolve it.

2. NoSuchFieldError

As the name suggests, NoSuchFieldError occurs when a specified field doesn't exist. NoSuchFieldError extends the IncompatibleClassChangeError class and is thrown when the application tries to access or modify a field of an object or a static field of a class but the object or class no longer has that field.

IncompatibleClassChangeError class extends the LinkageError class and occurs when we perform incompatible class definition changes. Finally, LinkageError extends Error and shows that a class has some dependency on another incompatibly changed class.

Let's see this error in action with the help of an example. As a first step, let's create a Dependency class:

public class Dependency {
    public static String message = "Hello Baeldung!!";
}

Then we'll create a FieldErrorExample class that refers to a field of our Dependency class:

public class FieldErrorExample {
    public static String getDependentMessage() {
        return Dependency.message;
    }
}

Let's also add code to check whether we're getting a message from the Dependency class:

public static void fetchAndPrint() {
    System.out.println(getDependentMessage());
}

Now, we can compile these files using the javac command, and upon execution of the FieldErrorExample class using the java command, it will print the specified message.

However, if we comment out, remove, or change the attribute name in the Dependency class and recompile it, then we'll run into our error.

For example, let's change the attribute name in our Dependency class:

public class Dependency {
    public static String msg = "Hello Baeldung!!";
}

Now, if we recompile only our Dependency class, and then execute FieldErrorExample again, we'll encounter the NoSuchFieldError:

Exception in thread "main" java.lang.NoSuchFieldError: message

The above error occurred because the FieldErrorExample class still refers to the static field message of the Dependency class, but it no longer exists — we've made an incompatible change to the Dependency class.

3. Resolving the Error

To avoid this error, we need to clean and compile the existing files. We can do this using the javac command or with Maven by running mvn clean install. By performing this step, we'll have all the latest compiled files, and we'll avoid running into the error.

If the error persists, then the problem might be multiple JAR files: one while compiling, and another while running. This often happens when the application depends on external JARs. Here, we should validate the order of the JARs in the build path to identify the inconsistent JAR.

If we have to investigate further, it's helpful to run the application with -verbose: class option to check the loaded classes. This can help us identify the outdated class.

Sometimes a third-party JAR might be internally referring to another version, which results in NoSuchFieldError. If this happens, we can use mvn dependency:tree -Dverbose. This generates the maven dependency tree and helps us in identifying the inconsistent JAR.

4. Conclusion

In this short tutorial, we have shown why NoSuchFieldError occurs and looked at how we can resolve it.

As always, the code is available over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!