# Expected null bulk string (“$-1\\r\\n”), got BULK\_STRING (#YZ1)

**URL:** <https://forum.codecrafters.io/t/expected-null-bulk-string-1-r-n-got-bulk-string-yz1/284>\
**Category:** Challenges\
**Tags:** challenge:redis\
**Created:** [May 5, 2024, 10:38am UTC](https://forum.codecrafters.io/t/expected-null-bulk-string-1-r-n-got-bulk-string-yz1/284 "2024-05-05T10:38:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![saivasanth528](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/saivasanth528/32/246_2.png) [@saivasanth528](https://forum.codecrafters.io/u/saivasanth528)\
**Post date:** [May 5, 2024, 10:38am UTC](https://forum.codecrafters.io/t/expected-null-bulk-string-1-r-n-got-bulk-string-yz1/284/1 "2024-05-05T10:38:15Z")

</div>

I’m stuck on stage 7 expiry.

I have implemented the expiry logic, as per the instructions in java, even though i am sending the null bulk string, it is failing. Can you guys please help me where i am missing.

Here are my logs:

```auto
remote: [stage-7] Received bytes: "+OK\r\n"
remote: [stage-7] Received RESP value: "OK"
remote: [stage-7] Received "OK"
remote: [stage-7] Received OK at 10:29:33.576
remote: [stage-7] Fetching key "apple" at 10:29:33.576 (should not be expired)
remote: [stage-7] $ redis-cli GET apple
remote: [stage-7] Sent bytes: "*2\r\n$3\r\nGET\r\n$5\r\napple\r\n"
remote: [stage-7] Received bytes: "$6\r\nbanana\r\n"
remote: [stage-7] Received RESP value: "banana"
remote: [stage-7] Received "banana"
remote: [stage-7] Sleeping for 101ms
remote: [stage-7] Fetching key "apple" at 10:29:33.690 (should be expired)
remote: [stage-7] $ redis-cli GET apple
remote: [stage-7] Sent bytes: "*2\r\n$3\r\nGET\r\n$5\r\napple\r\n"
remote: [stage-7] Received bytes: "$5\r\n$-1\r\n\r\n"
remote: [stage-7] Received RESP value: "$-1\r\n"
remote: [stage-7] Expected null bulk string ("$-1\r\n"), got BULK_STRING
remote: [stage-7] Test failed
remote: [stage-7] Terminating program
remote: [stage-7] Program terminated successfully

```

And here’s a snippet of my code:

```java

public class Get implements Command {

    @Override
    public void execute(BufferedReader inputStream, OutputStream outputStream) throws IOException {
        int length = Integer.parseInt(inputStream.readLine().substring(1));
        String key = inputStream.readLine();
        String result = InMemoryCache.getValue(key);
        result = (result == null) ? RedisParser.nullBulkStringResponse() : result;
        outputStream.write(
               RedisParser.stringFormatting(result).getBytes(StandardCharsets.UTF_8)
        );
    }
}

```

---

<div class="post-metadata">

**Author:** ![warpftl](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/warpftl/32/156_2.png) [@warpftl](https://forum.codecrafters.io/u/warpftl)\
**Post date:** [May 5, 2024, 4:18pm UTC](https://forum.codecrafters.io/t/expected-null-bulk-string-1-r-n-got-bulk-string-yz1/284/2 "2024-05-05T16:18:55Z")

</div>

The response it expects is the null bulk string:

```auto
$-1\r\n

```

Your logs suggest that the value you are sending is:

```auto
"$5\r\n$-1\r\n\r\n"

```

which is a bulk string wrapping the value `$-1\r\n`.

* * *

It seems this is due to these lines:

```java
result = (result == null) ? RedisParser.nullBulkStringResponse() : result;
outputStream.write(
    RedisParser.stringFormatting(result).getBytes(StandardCharsets.UTF_8)
);

```

Here you are setting the result as the null bulk string when the key is not found and then in the next line you apply the `RedisParser.stringFormatting()` function on it, which I am assuming transforms the null string into a bulk string.

Try directly returning the value of `RedisParser.nullBulkStringResponse()` when the key is not found.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [May 10, 2024, 4:19pm UTC](https://forum.codecrafters.io/t/expected-null-bulk-string-1-r-n-got-bulk-string-yz1/284/3 "2024-05-10T16:19:34Z")

</div>

Closed on May 10, 2024, 4:19pm UTC

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [June 4, 2024, 3:32am UTC](https://forum.codecrafters.io/t/expected-null-bulk-string-1-r-n-got-bulk-string-yz1/284/4 "2024-06-04T03:32:51Z")

</div>

Note: I’ve updated the title of this post to include the stage ID (#YZ1). You can learn about the stages rename here: [Upcoming change: Stages overhaul](https://forum.codecrafters.io/t/upcoming-change-stages-overhaul/375).
