Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response.take(Class<T>, int) throws a NullPointerException if the query returns no results #108

Open
emmanuel-keller opened this issue Feb 17, 2025 · 3 comments

Comments

@emmanuel-keller
Copy link
Contributor

          I noticed that `Response.take(Class<T>, int)` throws a `NullPointerException` if the query returns no results instead of returning null or an empty instance. This makes error handling more difficult in cases where no matching entry exists.

Would it be possible to handle this case more gracefully by returning null or an empty object instead of throwing an exception?

Here is my stack trace when this issue occurs:

java.lang.NullPointerException: Object instance not found at com.surrealdb.Value.getObject(Native Method) at com.surrealdb.Value.getObject(Value.java:109) at com.surrealdb.ValueClassConverter.convert(ValueClassConverter.java:175) at com.surrealdb.Value.get(Value.java:212) at dev.massi.ayase.database.model.AyaseUser.lambda$findById$0(AyaseUser.java:74)

Originally posted by @massikhr in #106 (comment)

@emmanuel-keller emmanuel-keller changed the title Response.take(Class<T>, int) throws a NullPointerException if the query returns no results instead of returning null or an empty instance. This makes error handling more difficult in cases where no matching entry exists Response.take(Class<T>, int) throws a NullPointerException if the query returns no results Feb 17, 2025
@massikhr
Copy link

I’m still experiencing the NullPointerException issue when using Response.take(Class<T>, int) If the query returns no results, it throws an exception instead of returning null or an empty instance.

Debugging steps:

  • Query execution works, and Response.toString() shows a pointer, but take() still fails.
  • Here’s my latest code:

`public static CompletableFuture findById(Long userId) {
return CompletableFuture.supplyAsync(() -> {
System.out.println("1: params map");
final HashMap<String, Long> map = new HashMap<>();
map.put("$userId", userId);

    System.out.println("2: execute query");
    Response response = Ayase.surreal().queryBind("SELECT * FROM users WHERE userId = $userId", map);

    System.out.println("3: extract user from response");
    System.out.println("Query Result: " + response);

    System.out.println("response: " + response.take(AyaseUser.class, 0));
    return response.take(AyaseUser.class, 0);
});

} `

  • My AyaseUser.java class:

`public class AyaseUser {
public RecordId id;
public Long userId;
public Long createdAt;

public AyaseUser() {}

public AyaseUser(Long userId, Long createdAt) {
    this.userId = userId;
    this.createdAt = createdAt;
}

@Override
public boolean equals(Object o) {
    if (o == null || getClass() != o.getClass()) return false;
    AyaseUser ayaseUser = (AyaseUser) o;
    return Objects.equals(id, ayaseUser.id) &&
           Objects.equals(userId, ayaseUser.userId) &&
           Objects.equals(createdAt, ayaseUser.createdAt);
}

@Override
public int hashCode() {
    return Objects.hash(id, userId, createdAt);
}

}`

  • Error message:

java.lang.NullPointerException: Object instance not found at com.surrealdb.Value.getObject(Native Method) at com.surrealdb.Value.getObject(Value.java:109) at com.surrealdb.ValueClassConverter.convert(ValueClassConverter.java:175) at com.surrealdb.Value.get(Value.java:212) at com.surrealdb.Response.take(Response.java:26) at dev.massi.ayase.database.model.AyaseUser.lambda$findById$0(AyaseUser.java:56) at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1812) at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1804) at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:507) at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1460) at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:2036) at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:189)

  • Question:

Should I check response.size() or use Value.isNone() before calling take()?
Is there a recommended way to handle this?

@massikhr
Copy link

Bad formatted sorry about that. 😃

@Maji1n
Copy link

Maji1n commented Mar 1, 2025

same here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants