Can't find a codec for class [Ljava.lang.String

I am using Java version “1.8.0_131” and Maven from the command line. There is no IntelliJ in play.
I have edited MoviesDao with 1 line for the query specification.
When performing the search from the web page I see in the console,

org.bson.codecs.configuration.CodecConfigurationException: Can’t find a codec for class [Ljava.lang.String;.

The full stack is below

org.bson.codecs.configuration.CodecConfigurationException: Can’t find a codec for class [Ljava.lang.String;.
at org.bson.codecs.configuration.CodecCache.getOrThrow( ~[bson-3.9.1.jar:na]
at org.bson.codecs.configuration.ProvidersCodecRegistry.get( ~[bson-3.9.1.jar:na]
at org.bson.codecs.configuration.ChildCodecRegistry.get( ~[bson-3.9.1.jar:na]
at org.bson.codecs.DocumentCodec.writeValue( ~[bson-3.9.1.jar:na]
at org.bson.codecs.DocumentCodec.writeMap( ~[bson-3.9.1.jar:na]
at org.bson.codecs.DocumentCodec.encode( ~[bson-3.9.1.jar:na]
at org.bson.codecs.DocumentCodec.encode( ~[bson-3.9.1.jar:na]
at org.bson.BsonDocumentWrapper.getUnwrapped( ~[bson-3.9.1.jar:na]
at org.bson.BsonDocumentWrapper.isEmpty( ~[bson-3.9.1.jar:na]
at com.mongodb.operation.DocumentHelper.putIfNotNullOrEmpty( ~[mongodb-driver-core-3.9.1.jar:na]
at com.mongodb.operation.FindOperation.getCommand( ~[mongodb-driver-core-3.9.1.jar:na]
at com.mongodb.operation.FindOperation.access$200( ~[mongodb-driver-core-3.9.1.jar:na]
at com.mongodb.operation.FindOperation$ ~[mongodb-driver-core-3.9.1.jar:na]
at com.mongodb.operation.FindOperation$ ~[mongodb-driver-core-3.9.1.jar:na]
at com.mongodb.operation.OperationHelper.withConnectionSource( ~[mongodb-driver-core-3.9.1.jar:na]
at com.mongodb.operation.OperationHelper.withConnection( ~[mongodb-driver-core-3.9.1.jar:na]
at com.mongodb.operation.FindOperation.execute( ~[mongodb-driver-core-3.9.1.jar:na]
at com.mongodb.operation.FindOperation.execute( ~[mongodb-driver-core-3.9.1.jar:na]
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute( ~[mongodb-driver-sync-3.9.1.jar:na]
at com.mongodb.client.internal.MongoIterableImpl.execute( ~[mongodb-driver-sync-3.9.1.jar:na]
at com.mongodb.client.internal.MongoIterableImpl.iterator( ~[mongodb-driver-sync-3.9.1.jar:na]
at mflix.api.daos.MovieDao.getMoviesByCountry( ~[classes/:na]

If I then use an empty document for the query, but a valid document specification for the projection (as confirmed by viewing the output from running “mvn test -Dtest=ProjectionTest”)
I then see in the console

java.lang.NullPointerException: null
at mflix.api.daos.MovieDocumentMapper.mapToMovieTitle( ~[classes/:na]
at$3$1.accept( ~[na:1.8.0_131]
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining( ~[na:1.8.0_131]
at ~[na:1.8.0_131]
at ~[na:1.8.0_131]
at$ReduceOp.evaluateSequential( ~[na:1.8.0_131]
at ~[na:1.8.0_131]
at ~[na:1.8.0_131]

I would recommend going through the code and the changes you have made. Check if you are having return null in your function somewhere which you are editing.


Hi @Bill_89515,

Can you paste here

The nullpointer exception you are encountering is due to the projection you’ve defined. You are probably not projecting a on of the expected fields.

Take a second look to your projection.


HI Kanika, you’ll likely need to “moderate out” some of this content.
I copied in an original Here is the whole function

public List getMoviesByCountry(String… country) {
//Bson queryFilter = new Document();
Bson queryFilter = new Document(“countries”, country);
Bson projection = new Document();
List movies = new ArrayList<>();
return movies;

The projection line I would try later on was

Bson projection = new Document(“title”, 1).append("_id", 0);

To reiterate, I am not using IntelliJ, only Maven.

C:\mflix-java\mflix>java -version
java version “1.8.0_131”
Java™ SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot™ 64-Bit Server VM (build 25.131-b11, mixed mode)

C:\mflix-java\mflix>mvn --version
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T14:41:47-04:00)
Maven home: C:\maven.3.0.6\bin…
Java version: 1.8.0_131, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_131\jre
Default locale: en_US, platform encoding: Cp1252
OS name: “windows 10”, version: “10.0”, arch: “amd64”, family: “windows”

Path=C:\Program Files\Java\jdk1.8.0_131\bin;C:\maven.3.0.6\bin; . . .

Mongo is running on Atlas.

  • Bill

Hi @Bill_89515,

To reiterate, I am not using IntelliJ, only Maven.

No problem. IntelliJ is optional.

As I mentioned in my previous message, your projection needs some work. This is why you are hitting the null pointer exception in the mapper class.


I miss-read the lab re: what fields it expects. The updated query and projection are simply,

  Bson queryFilter = new Document("countries", country);
  Bson projection = new Document("title", 1);

When running, “mvn test -Dtest=ProjectionTest” I am still seeing

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class [Ljava.lang.String;.
at mflix.api.daos.ProjectionTest.testProjectionShape(
testFindMoviesByCountry(mflix.api.daos.ProjectionTest)  Time elapsed: 0.002 s  <<< ERROR!
org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class [Ljava.lang.String;.
at mflix.api.daos.ProjectionTest.testFindMoviesByCountry(

I would have expected a projection error to not result in a stack exception, but instead for the assertions
back in ProjectionTest.javato complain. For example, Assert.assertEquals(…).

FWIW, this set of query and projection definition statements did work

// Note: Need import com.mongodb.BasicDBObject;
BasicDBObject queryFilter = new BasicDBObject();
queryFilter.put("countries", new BasicDBObject("$in", country) );
BasicDBObject projection = new BasicDBObject();
projection.put("title", 1);

And also, this set of (likely preferred) statements did work as well,

Bson queryFilter ="countries", country);
Bson projection = Projections.fields(Projections.include("title") );

I must be repeatedly missing something fundamental when trying the Document class oriented approach.
It is unclear to me why the codec exception comes into play with that approach vs the other 2.

Thank you,

1 Like

I think the problem is not with the projection, but with the Document class.
Notice that you are passing an array of strings, not a string, nor a list, and Document does not know how to handle that.

Can someone know the solution to the problem, with “org.bson.codecs.configuration.CodecConfigurationException: Can’t find a codec for class [Ljava.lang.String;.” Thanks!

wow, wow, wow.
are you me?

The problem is with how the getMoviesByCountry function is declared. It accepts String…, a vararg which is Iterable, even though the name of the function would indicate that a single string is passed.

Check the com.mongodb.client.model.Filters documentation, note the difference between Filters.eq and Only one of those accepts the country parameter as passed to the function.


I hope this will help.
“The Document class currently supports only List, not native Java array, so just replace with:…”

1 Like

Thank you so much for the hint.
Initially I have implemented query filter as Bson queryFilter = eq(…) and got the same Exception. But as soon as I changed to Bson queryFilter = in(…) it worked as charm

An alternative approach would be to send the first element in country like so

moviesCollection.find(eq(“countries”, country[0]))

(This is based on the test case however better way of doing it is “in” ).