I have a map in which I am storing total number of bytes as the key and count as the value. I need to increment the count by 1 if the key is already present in my map. If the key is not present then start with 1.
I have the below code -
Map<Integer, Integer> userList = new HashMap<Integer, Integer>();
for (String userId : listOfUsers) {
String sql = "select * from test_table where user_id='"+ userId + "';";
try {
SimpleStatement query = new SimpleStatement(sql);
ResultSet res = session.execute(query);
int totalBytes = 0;
Iterator<Row> rows = res.iterator();
while (rows.hasNext()) {
Row r = rows.next();
ByteBuffer client_value = r.getBytes("client_value");
int returned = client_value.remaining();
totalBytes = totalBytes + returned;
}
// does this look right?
userList.put(totalBytes, userList.get(totalBytes) + 1);
} catch (Exception e) {
// log an error
}
}
Whenever I am running for the first time, I am getting NPE on my userList.put
command. Is there anything wrong I am doing? This code will be in single thread.