# Persistent ClassCastExceptions using QueryResultWriters.write

**URL:** https://community.stardog.com/t/persistent-classcastexceptions-using-queryresultwriters-write/2133
**Category:** Support
**Created:** [December 5, 2019, 3:50am UTC](https://community.stardog.com/t/persistent-classcastexceptions-using-queryresultwriters-write/2133 "2019-12-05T03:50:37Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![conranc](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@conranc](https://community.stardog.com/u/conranc)
#### Post date: [December 5, 2019, 3:50am UTC](https://community.stardog.com/t/persistent-classcastexceptions-using-queryresultwriters-write/2133/1 "2019-12-05T03:50:37Z")

</div>

We're having persistent (but not necessarily consistent) ClassCastExceptions in our Java JBoss application when trying to write query results to a byte stream.

The trace is :

```auto
Unhandled exception from service: java.lang.ClassCastException: Cannot cast com.stardog.stark.query.io.json.SPARQLJSONBooleanResultWriter to com.stardog.stark.query.io.SelectQueryResultWriter
	at java.lang.Class.cast(Class.java:3369)
	at java.util.Optional.map(Optional.java:215)
	at com.stardog.stark.query.io.QueryResultWriters.write(QueryResultWriters.java:120)
	.... (our code line here)

```

The calls are generally of the form :

```auto
ByteArrayOutputStream os = new ByteArrayOutputStream();
QueryResultFormat format = QueryResultFormats.forMime("application/sparql-results+json");
SelectQuery squery = connection.select(...some query ...);
SelectQueryResult result = squery.execute();
QueryResultWriters.write(result, os, format.get());
String outResult = new String(os.toByteArray(), Charsets.UTF_8);

```

`outResult` is then returned in the HTTP Response with the correct MIME type encoding back to the calling RESTful client.

Similar code is used in a few methods and once the error occurs then all executions of this type of code throws the exception.

The queries are fairly simple "SELECT" queries that return mostly text values that have worked for the last year or so.  
The exceptions have only started since we've upgraded the Stardog server to 7.0.2.

Sometimes restarting the JBoss server (occasionally a few times might be required) will "clear" the error, but it often soon reappears.  
Restarting the Stardog server has no effect.  
There are no server errors logged to stardog.log.

We know that queries are working - other methods that run queries but don't use QueryResultWriters (ie build a bespoke result using .hasNext() ) work as expected.

Any thoughts on resolving this would be very much appreciated!

Thanks,  
Chris

---

<div class="post-metadata">

### Author: ![jess](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/jess/32/10_2.png) [@jess](https://community.stardog.com/u/jess)
#### Post date: [December 5, 2019, 4:17am UTC](https://community.stardog.com/t/persistent-classcastexceptions-using-queryresultwriters-write/2133/2 "2019-12-05T04:17:29Z")

</div>

Hi Chris,

It looks like this call is the problem:

```auto
QueryResultFormats.forMime("application/sparql-results+json")

```

This may return either SELECT or ASK formats. I'll bring this up internally but in the meantime you can use the constant `QueryResultFormats.JSON` which is the SELECT format.

Jess

---

<div class="post-metadata">

### Author: ![conranc](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@conranc](https://community.stardog.com/u/conranc)
#### Post date: [December 5, 2019, 4:31am UTC](https://community.stardog.com/t/persistent-classcastexceptions-using-queryresultwriters-write/2133/3 "2019-12-05T04:31:59Z")

</div>

Thanks Jess, I'll give that a try.

Chris

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)
#### Post date: [December 19, 2019, 4:32am UTC](https://community.stardog.com/t/persistent-classcastexceptions-using-queryresultwriters-write/2133/4 "2019-12-19T04:32:02Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
