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(CodecCache.java:46) ~[bson-3.9.1.jar:na]
at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63) ~[bson-3.9.1.jar:na]
at org.bson.codecs.configuration.ChildCodecRegistry.get(ChildCodecRegistry.java:51) ~[bson-3.9.1.jar:na]
at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:184) ~[bson-3.9.1.jar:na]
at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:199) ~[bson-3.9.1.jar:na]
at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:141) ~[bson-3.9.1.jar:na]
at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:45) ~[bson-3.9.1.jar:na]
at org.bson.BsonDocumentWrapper.getUnwrapped(BsonDocumentWrapper.java:195) ~[bson-3.9.1.jar:na]
at org.bson.BsonDocumentWrapper.isEmpty(BsonDocumentWrapper.java:115) ~[bson-3.9.1.jar:na]
at com.mongodb.operation.DocumentHelper.putIfNotNullOrEmpty(DocumentHelper.java:43) ~[mongodb-driver-core-3.9.1.jar:na]
at com.mongodb.operation.FindOperation.getCommand(FindOperation.java:1001) ~[mongodb-driver-core-3.9.1.jar:na]
at com.mongodb.operation.FindOperation.access$200(FindOperation.java:84) ~[mongodb-driver-core-3.9.1.jar:na]
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:703) ~[mongodb-driver-core-3.9.1.jar:na]
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:696) ~[mongodb-driver-core-3.9.1.jar:na]
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:462) ~[mongodb-driver-core-3.9.1.jar:na]
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:406) ~[mongodb-driver-core-3.9.1.jar:na]
at com.mongodb.operation.FindOperation.execute(FindOperation.java:696) ~[mongodb-driver-core-3.9.1.jar:na]
at com.mongodb.operation.FindOperation.execute(FindOperation.java:83) ~[mongodb-driver-core-3.9.1.jar:na]
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:179) ~[mongodb-driver-sync-3.9.1.jar:na]
at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:132) ~[mongodb-driver-sync-3.9.1.jar:na]
at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:86) ~[mongodb-driver-sync-3.9.1.jar:na]
at mflix.api.daos.MovieDao.getMoviesByCountry(MovieDao.java:130) ~[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(MovieDocumentMapper.java:25) ~[classes/:na]
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_131]
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374) ~[na:1.8.0_131]
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_131]
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_131]
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_131]
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_131]
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[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.

Kanika

Hi @Bill_89515,

Can you paste here

MovieDao.java:130

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.

N.

HI Kanika, you’ll likely need to “moderate out” some of this content.
I copied in an original MovieDao.java. 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<>();
moviesCollection
.find(queryFilter)
.projection(projection)
.iterator()
.forEachRemaining(movies::add);
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
2 Likes

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.

N.

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(ProjectionTest.java:53)
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(ProjectionTest.java:38)

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 = Filters.in("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,
Bill

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 Filters.in. Only one of those accepts the country parameter as passed to the function.

1 Like

I hope this will help.
https://jira.mongodb.org/browse/JAVA-1894.
“The Document class currently supports only List, not native Java array, so just replace with:…”

1 Like