#Trying to solve NullPointerException happening within JAVA class involving DynamoDB Tables

1 messages · Page 1 of 1 (latest)

tough lake
#

I have this method and if someone wants to connect I can hop on a call as well, but overall I'm trying to delete a book from the table and here is the code I wrote so far. IntelliJ is saying the .put method for the Hashmap might result in a NullPointer but I have no idea how to prevent it.

''JAVA

public boolean deleteBook(String bookId) {
Book book = new Book(Book.builder());
book.setBookId(bookId);

    DynamoDBDeleteExpression deleteExpression = new DynamoDBDeleteExpression();
    Map <String, ExpectedAttributeValue> expectedAttributeValueMap = new HashMap<>();
    expectedAttributeValueMap.put("isActive",
            new ExpectedAttributeValue()).
            withValue(new AttributeValue().withBOOL(false));
    deleteExpression.setExpected(expectedAttributeValueMap);

    try {
        dynamoDbMapper.delete(book, deleteExpression);
        return true;
    } catch (ConditionalCheckFailedException e) {
        System.out.println(e);
        return false;
    }
}

""

novel pewter
#
 public boolean deleteBook(String bookId) {
        Book book = new Book(Book.builder());
        book.setBookId(bookId);

        DynamoDBDeleteExpression deleteExpression = new DynamoDBDeleteExpression();
        Map <String, ExpectedAttributeValue> expectedAttributeValueMap = new HashMap<>();
        expectedAttributeValueMap.put("isActive",
                new ExpectedAttributeValue()).
                withValue(new AttributeValue().withBOOL(false));
        deleteExpression.setExpected(expectedAttributeValueMap);

        try {
            dynamoDbMapper.delete(book, deleteExpression);
            return true;
        } catch (ConditionalCheckFailedException e) {
            System.out.println(e);
            return false;
        }
    }
#
public boolean deleteBook(final String bookId) 
{
    try 
    {
        if (bookId == null || bookId.isEmpty())
        {
           System.err.println("Invalid bookId=" + bookId);
           return false;
        }

        final Book book = new Book(Book.builder());
        book.setBookId(bookId);

        final DynamoDBDeleteExpression deleteExpression = new DynamoDBDeleteExpression();

        final HashMap<String, ExpectedAttributeValue> expectedAttributeValueMap = new HashMap<String, ExpectedAttributeValue>();

/*
        expectedAttributeValueMap.put("isActive",
                new ExpectedAttributeValue()
        ). // WRONG ?
                withValue(new AttributeValue().withBOOL(false));
*/
       final ExpectedAttributeValue expected = new ExpectedAttributeValue()
.withValue( new AttributeValue().withBOOL(false) );

        expectedAttributeValueMap.put("isActive", expected);

        deleteExpression.setExpected(expectedAttributeValueMap);

        try
        {
            dynamoDbMapper.delete(book, deleteExpression);
            return true;
        } catch (ConditionalCheckFailedException ex) {
            ex.printStackTrace();
        }
    }
    catch(Exception ex)
    {
       ex.printStackTrace();
    }

    return false;
}