Home » Java Basics » exceptions » java.lang.NoSuchFieldError – How to solve SuchFieldError

About Sotirios-Efstathios Maneas

Sotirios-Efstathios Maneas
Sotirios-Efstathios (Stathis) Maneas is a PhD student at the Department of Computer Science at the University of Toronto. His main interests include distributed systems, storage systems, file systems, and operating systems.

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()
  • Creates an instance of the NoSuchFieldError class, setting null as its message.

  • NoSuchFieldError(String s)
  • Creates an instance of the NoSuchFieldErrorclass, 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.

Download
You can download the full source code of this example here:NoSuchFieldErrorExample.zip.
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of