java.lang.NoSuchFieldError – How to solve SuchFieldError
In this tutorial we will discuss about the NoSuchFieldError
in Java. This exception is thrown to indicate that an application tries to access or modify an object’s field, but that field no longer exists. This error can only occur during runtime, if the definition of a class has incompatibly changed.
The NoSuchFieldError
extends the IncompatibleClassChangeError
class, which is used to indicate that the definition of a class has unexpectedly changed. Furthermore, the IncompatibleClassChangeError
class extends the LinkageError
class, which is used to indicate those error cases, where a class has a dependency on some other class and that class has incompatibly changed after the compilation.
Moreover, the LinkageError
class extends the Error
class, which is used to indicate those serious problems that an application should not catch. A method may not declare such errors in its throw
clause, because these errors are abnormal conditions that shall never occur.
Finally, the NoSuchFieldError
exists since the 1.0 version of Java.
The Structure of NoSuchFieldError
Constructors
NoSuchFieldError()
NoSuchFieldError(String s)
Creates an instance of the NoSuchFieldError
class, setting null
as its message.
Creates an instance of the NoSuchFieldError
class, using the specified string as message. The string argument indicates the name of the class that threw the error.
The NoSuchFieldError in Java
As we have described, the NoSuchFieldError
error occurs at runtime, if the specified field does not exist. In this section we will describe how this error can appear.
First of all, we create two simple classes:
TestClass.java:
public class TestClass { public static String str = "Hello from Java Code Geeks!"; }
NoSuchFieldErrorExample.java:
public class NoSuchFieldErrorExample { public static void main(String[] args) { System.out.println(TestClass.str); } }
Inside the TestClass
we declare a static variable that can be accessed from any other class. Inside the NoSuchFieldErrorExample
we just print the variable’s value.
Using the terminal (Linux or Mac) or the command prompt (Windows), we navigate to the folder where these source files are located. Then, we execute the following commands:
javac NoSuchFieldErrorExample.java java NoSuchFieldErrorExample
A sample execution is shown below:
Hello from Java Code Geeks!
Now, let’s comment the variable’s definition:
//public static String str = "Hello from Java Code Geeks!";
Then, we compile only the TestClass
, but not the NoSuchFieldErrorExample
class, and we execute our program:
javac TestClass.java java NoSuchFieldErrorExample
A sample execution is shown:
Exception in thread "main" java.lang.NoSuchFieldError: str at NoSuchFieldErrorExample.main(NoSuchFieldErrorExample.java:3)
This error is thrown because we changed the definition of a specific class and we didn’t re-compile those classes that have a reference to that class. Thus, these classes contained an outdated view of the specified class.
How to deal with the NoSuchFieldError
In order to deal with this error, you shall clean all existing .class
files and compile everything from scratch. In this way, you can verify that each referenced class is compiled to its latest version.
However, if the error is still thrown during runtime, then you probably compile using one version of a library, but use another version at runtime. You must verify that your classpath contains the proper version of the specified library.
Download the Eclipse Project
This was a tutorial about the NoSuchFieldError
in Java.
You can download the full source code of this example here:NoSuchFieldErrorExample.zip.
helpful, well written. thanks.