fix many issues: "Array access results in a null pointer dereference" and "Null pointer passed as an argument to a nonnull parameter"