Android AWS DynamoDB "No hash key condition" - android

I have a DynamoDB table that has some data. There is a hashkey of "class_id" and a rangekey of "message_timestamp".
In my android code I am attempting to query for messages that are newer than the last message received.
int lastMessageTimestamp = GetNewestTimestamp();
DynamoChatData messagesToFind = new DynamoChatData();
Log.i(TAG,String.valueOf(class_id));
messagesToFind.SetClassId(class_id); // Set to 2 in the debugger at runtime
Condition rangeKeyCondition = new Condition();
rangeKeyCondition.withComparisonOperator(ComparisonOperator.GT.toString());
AttributeValue attributeValue = new AttributeValue();
attributeValue.withN(String.valueOf(lastMessageTimestamp));
rangeKeyCondition.withAttributeValueList(attributeValue);
DynamoDBQueryExpression<DynamoChatData> query = new DynamoDBQueryExpression<>();
query.withHashKeyValues(messagesToFind);
query.withRangeKeyCondition("message_timestamp", rangeKeyCondition);
query.withConsistentRead(false);
PaginatedQueryList result = objectMapper.query(DynamoChatData.class, query);
The DynamoChatData class:
#DynamoDBTable(tableName = "scriyb_chat")
public class DynamoChatData {
private int class_id;
private int message_timestamp;
private String user_name;
private String user_full_name;
private String message_content;
private int message_visible;
private int message_underage;
#DynamoDBRangeKey(attributeName = "message_timestamp")
public int GetMessageTimestamp(){
return message_timestamp;
}
public void SetMessageTimestamp(int _message_timestamp){
message_timestamp = _message_timestamp;
}
#DynamoDBHashKey(attributeName = "class_id")
public int GetClassId(){
return class_id;
}
public void SetClassId(int _class_id){
class_id = _class_id;
}
#DynamoDBAttribute(attributeName = "user_name")
public String GetUsername(){
return user_name;
}
public void SetUsername(String _user_name){
user_name = _user_name;
}
#DynamoDBAttribute(attributeName = "user_full_name")
public String GetUserFullName(){
return user_full_name;
}
public void SetUserFullName(String _user_full_name){
user_full_name = _user_full_name;
}
#DynamoDBAttribute(attributeName = "message_content")
public String GetMessageContent(){
return message_content;
}
public void SetMessageContent(String _message_content){
message_content = _message_content;
}
#DynamoDBAttribute(attributeName = "message_visible")
public int GetMessageVisible(){
return message_visible;
}
public void SetMessageVisible(int _message_visible){
message_visible = _message_visible;
}
#DynamoDBAttribute(attributeName = "message_underage")
public int GetMessageUnderage(){
return message_underage;
}
public void SetMessageUnderage(int _message_underage){
message_underage = _message_underage;
}
}
I followed the basic example outlined here and have read a bunch of posts on this site as well. Not sure why I get the
java.lang.IllegalArgumentException: Illegal query expression: No hash key condition is found in the query
error.
Any insight is appreciated.

Try using refactoring your getter/setter names so that they start with a lowercase letter as is standard Java convention. I believe the mapper looks for getters as methods that start with "get" and I think it's missing yours since they start with a capitol G.
Let me know if this resolves your problem!
Weston

Related

Amazon DynamoDB PaginatedQueryList null object reference error

I get a null object reference error when I try to Query data from my Amazon DynamoDB table like this:
mapper = new DynamoDBMapper(ddbClient);
DataMapperClass dataMapperClass = new DataMapperClass();
dataMapperClass.setHash("theHashValueIset");
String queryString = String.valueOf("theRangeValueIset");
Condition rangeKeyCondition = new Condition()
.withComparisonOperator(ComparisonOperator.BEGINS_WITH.toString())
.withAttributeValueList(new AttributeValue().withS(queryString.toString()));
DynamoDBQueryExpression <DataMapperClass> queryExpression = new DynamoDBQueryExpression<DataMapperClass>()
.withHashKeyValues(dataMapperClass)
.withRangeKeyCondition("rangeAttributeOnTable", rangeKeyCondition)
.withConsistentRead(false);
PaginatedQueryList<DataMapperClass> result = mapper.query(DataMapperClass.class, queryExpression);
My DataMapperClass.class:
#DynamoDBTable(tableName = "myTableName")
public class DataMapperClass {
private String hash;//based on a set of categories I chose
private String objectID; //auto generated
//Hash
#DynamoDBHashKey(attributeName = "hashNameOnMyTable")
public String getHash() { return hash; }
public void setHash(String hash) { this.hash = hash;}
//ObjectID
#DynamoDBRangeKey(attributeName = "rangeAttributeOnTable")
#DynamoDBAutoGeneratedKey
public String getObjectID() {return objectID;}
public void setObjectID(String objectID) { this.objectID = objectID; }
/* other #DynamoDBAttribute*/
}
How can I solve this error I get on the "PaginatedQueryList" code line :
Attempt to invoke interface method 'com.amazonaws.services.dynamodbv2.model.QueryResult com.amazonaws.services.dynamodbv2.AmazonDynamoDB.query(com.amazonaws.services.dynamodbv2.model.QueryRequest)' on a null object reference
Problem solved!
I realised I did not do this in my onCreate():
ddbClient = new AmazonDynamoDBClient(identityManager.getCredentialsProvider());

Parcelable Object return BadParcelableException: ClassNotFoundException when obj = getIntent().getParcelableExtra("name")

I have a problem when I write pracelable object and receive from another application that I wrote.
Everytime when I tried to get the object it always turned out to be BadParcelableException: ClassNotFoundException when unmarshalling.
I've been looking around and wonder if it turned out bad when I send to another application.
Or it's simply I had error in my codes.
Please tell me where did I go wrong.
my sending lines:
ParcelableObject myObject = new ParcelableObject();
myObject = new ParcelableObject(result_name, resut_lv, result_race,
result_job, result_nation, result_guild, result_mission);
intent.putExtra("ACTION_DATA_TRANSFER", myObject);
intent.setClassName(target_app, target_app+".ReceiveMain");
startActivity(intent);
my receive lines:
ParcelableObject myObject = new ParcelableObject();
myObject = getIntent().getParcelableExtra("ACTION_DATA_TRANSFER"); //always crashed at this line
if (getIntent().hasExtra("ACTION_DATA_TRANSFER")) {
Log.w("****CHECKING****", myObject.getName());
item1.setText("The Character's name is: " + myObject.getName());
} else {
Toast.makeText(getApplicationContext(), "There is not data passed yet",
Toast.LENGTH_LONG).show();
}
my ParcelableObject class:
private String name;
private int lv;
private String race;
private String job;
private String nation;
private String guild;
public String mission;
public void setName(String name) {
this.name = name;
}
public void setLv(int lv) {
this.lv = lv;
}
public void setRace(String race) {
this.race = race;
}
public void setJob(String job) {
this.job = job;
}
public void setNation(String nation) {
this.nation = nation;
}
public void setGuild(String guild) {
this.guild = guild;
}
public void setMission(String mission) {
this.mission = mission;
}
public ParcelableObject(String name, int lv, String race, String job, String
nation, String guild, String mission) {
// TODO Auto-generated constructor stub
this.name = name;
this.lv = lv;
this.race = race;
this.job = job;
this.nation = nation;
this.guild = guild;
this.mission = mission;
}
public String getName() {
return name;
}
public int getLv() {
return lv;
}
public String getRace() {
return race;
}
public String getJob() {
return job;
}
public String getNation() {
return nation;
}
public String getGuild() {
return guild;
}
public String getMission() {
return mission;
}
#Override
public int describeContents() {
// TODO Auto-generated method stub
Log.w("****PO CHECKING****", "describeContents called");
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
Log.w("****PO CHECKING****", "writeToParcel called");
dest.writeString(name);
dest.writeString(job);
dest.writeInt(lv);
dest.writeString(race);
dest.writeString(guild);
dest.writeString(nation);
dest.writeString(mission);
}
public ParcelableObject() {
// TODO Auto-generated constructor stub
super();
Log.i("**check if exist**", "start parcel");
}
public static final Parcelable.Creator<ParcelableObject> CREATOR =
new Parcelable.Creator<ParcelableObject>() {
#Override
public ParcelableObject createFromParcel(Parcel in) {
// TODO Auto-generated method stub
Log.w("checking out", "createFromParcel");
ParcelableObject pObject = new ParcelableObject();
pObject.name = in.readString();
pObject.job = in.readString();
pObject.lv = in.readInt();
pObject.race = in.readString();
pObject.guild = in.readString();
pObject.nation = in.readString();
pObject.mission = in.readString();
return pObject;
}
#Override
public ParcelableObject[] newArray(int size) {
// TODO Auto-generated method stub
return new ParcelableObject[size];
}
};
EDIT:
To clarify before some people think I am a lazy noob, I have been looking for answer to solve the cause for this but without any luck. I looked up many people's code and tested out many times but still can't get my app right. So that is why I am here to ask help. If some people still think I am a lazy noob, I guess I am too lazy for some people to draw that conclusion. I should reconsider of my action again.
My situation is:
I wrote 2 apps and one is sending a parcelable object to another application. First app it's working correctly to send the object to another. However, as the second app receive it, it goes wrong and gives out the BadParcelableException as I run the process. I am not using bundle because I saw many examples that it is not necessary to do so. If I have been wrong please correct me.
I found the answer that related to this topic...
Is using Parcelable the right way to send data between applications?
Thank you all for the comments from above....
I've solved my case by using serializable instead of Parcelable.
but in your case here try parsing the object like this
myObject = (ParcelableObject)getIntent().getParcelableExtra("ACTION_DATA_TRANSFER");

When passing 2 identical Parcable Classes, 1 is null

i have a weird issue.
I'm trying to pass 2 parcable classesfrom one activity to another.
I define both of them the exact same way, but of them is null.
The parcable class :
class Friends implements Parcelable {
private ArrayList<Integer> ids = new ArrayList<Integer>();
private ArrayList<String> names = new ArrayList<String>();
private ArrayList<Bitmap> images = new ArrayList<Bitmap>();
public void addId(Integer id)
{
ids.add(id);
}
public void addName(String name){
names.add(name);
}
public void addImage(Bitmap img){
images.add(img);
}
public ArrayList<Integer> getIds() {
return ids;
}
public ArrayList<String> getNames() {
return names;
}
public ArrayList<Bitmap> getImages() {
return images;
}
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(ids);
dest.writeList(names);
dest.writeList(images);
}
public static final Parcelable.Creator<Friends> CREATOR = new Parcelable.Creator<Friends>() {
public Friends createFromParcel(Parcel in) {
return new Friends(in);
}
public Friends[] newArray(int size) {
return new Friends[size];
}
};
public Friends(Parcel in){
in.readList(ids, null);
in.readList(names, null);
in.readList(images, null);
}
public Friends(Integer id, String name, Bitmap img) {
ids.add(id);
names.add(name);
images.add(img);
}
public Friends(){
}
The Sending part :
for(Integer position : selectedIds)
{
String name = a.getItem(position).getFriendName();
int id = a.getItem(position).getFriendId();
Bitmap img = a.getItem(position).getFriendImage();
Log.e("ID",String.valueOf(id));
selectedFriends.addId(new Integer(id));
selectedFriends.addName(name);
selectedFriends.addImage(img);
}
for(int position=0;position<list.getCount(); position++)
{
String name = a.getItem(position).getFriendName();
int id = a.getItem(position).getFriendId();
Bitmap img = a.getItem(position).getFriendImage();
Log.e("All IDs",String.valueOf(id));
allFriends.addId(new Integer(id));
allFriends.addName(name);
allFriends.addImage(img);
}
b.putParcelable("selecet_friends", selectedFriends);
b.putParcelable("all_friends", allFriends);
data.putExtras(b);
Both of the loops are being runned ( i can see the logs), all variables you don't see are being initialized correctly, everything is fine.
The Reciving part :
i define both as null :
private Friends selectedFriends = null;
private Friends allFriends = null;
And handle the onResult like this :
Log.e("Result","yessss");
Friends all_friends = (Friends)data.getParcelableExtra("all_friends");
Friends selected_friends = (Friends)data.getParcelableExtra("selected_friends");
allFriends = all_friends;
selectedFriends = selected_friends;
if(selectedFriends != null){
Log.e("is null","No");
}
if(allFriends != null){
Log.e("is all null","No");
}
Does anyone know how come the selectedFriends is null when allFriends is not?
EDIT:
Just a thought, but maybe it's because i put 2 parcables on a Bundle?
just i just add 2 bundles?
In the sending method you have a typo in this line:
b.putParcelable("selecet_friends", selectedFriends);
try this instead:
b.putParcelable("selected_friends", selectedFriends);
Also, you should use more specific names for the keys. The documentation for putExtras() says:
Add a set of extended data to the intent. The keys must include a
package prefix, for example the app com.android.contacts would use
names like "com.android.contacts.ShowAll

Information on setters and getters

I am learning extensively about getters and setters but I seem not to be having my way.
I have a class called Apps_Info which contains my setters and getters and I have my main activity FavouriteApps that has a list which uses the class Apps_Info.
I am trying to get the name of the package from the List in FavouriteApps but I am still getting null.
Please can someone tell what to do? Below is the code in this order: class Apps_Info and FavouriteApps activity
public class Apps_Info {
private Bitmap bIcon;
private String sName;
private String sPacks_Name;
public Apps_Info(Bitmap icon, String name, String Packs_Name) {
bIcon = icon;
sName = name;
sPacks_Name = Packs_Name;
}
public void setIcon(Bitmap icon) {
bIcon=icon;
}
public Bitmap getIcon() {
return bIcon;
}
public void setName(String name) {
sName=name;
}
public String getName() {
return sName;
}
public void setPacks_Name(String Packs_Name) {
this.sPacks_Name=Packs_Name;
}
public String getPacks_Name() {
return sPacks_Name;
}
}
FavouriteApps Activity code (part)
String packname, packsname, apps_names;
Bitmap app_icon;
Resources res = getResources();
List<Apps_Info> ListApps_Info = new ArrayList<Apps_Info>();
ListApps_Info.add(new Apps_Info(BitmapFactory.decodeResource(res, R.drawable.browser_app), "Browser", "com.browser"));
ListApps_Info.add(new Apps_Info(BitmapFactory.decodeResource(res, R.drawable.clock_app), "Alarm Clock", "com.alarm.clock"));
ListApps_Info.add(new Apps_Info(BitmapFactory.decodeResource(res, R.drawable.threegplus), "3G Secure Connection", "threeg.secureconnect"));
mGridView.setAdapter(new Apps_Info_Adapter(this, ListApps_Info));
Apps_Info packinfo = new Apps_Info(app_icon, apps_names, packname);
packsname = packinfo.getPacks_Name();
apps_names = packinfo.getName();
Log.i("The Pack_Name is " + packsname, "Pack Name");
You should do like this.
for (int i = 0; i < ListApps_Info.size(); i++) {
Apps_Info packinfo = ListApps_Info.item(i);
packsname = packinfo.getPacks_Name();
apps_names = packinfo.getName();
Log.i("The Pack_Name is " + packsname, "Pack Name");
}
It's because you never initialize the packname variable in your code.
String packname,packsname,apps_names;
When you do this :
Apps_Info packinfo=new Apps_Info(app_icon, apps_names,packname);
packsname=packinfo.getPacks_Name();
your getter is doing well and this is normal if it returns null.

Android - How to create a basic class and use it?

I have defined a class that contains properties of a specific answer object
The class look like this and is defined inside the class that is trying to use it
protected class Answer {
String QuestionId = "";
String AnswerValue = "";
String Correct = "";
public String getQuestionId() {
return QuestionId;
}
public void setQuestionId(String arg) {
QuestionId = arg;
}
public String getAnswerValue() {
return AnswerValue;
}
public void setAnswerValue(String arg) {
AnswerValue = arg;
}
public String getCorrect() {
return Correct;
}
public void setCorrect(String arg) {
Correct = arg;
}
}
Not sure if the above is OK
When I try to use the class I get null pointer errors
I'm using it like this
ArrayList<Answer> answerList = new ArrayList<Answer>();
for(int a=0;a<answers.getLength(); a++){
Element eAnswer = (Element) answers.item(a);
Answer anAnswer = new Answer;
NodeList answer_nodes = eAnswer.getChildNodes();
for (int ian=0; ian<answer_nodes.getLength(); ian++){
Node ans_attr = answer_nodes.item(ian);
String tag_name = ans_attr.getNodeName();
if(tag_name.equalsIgnoreCase("answer")){
anAnswer.setAnswerValue(ans_attr.getTextContent());
}
}
answerList.add(anAnswer);
}
Answer anAnswer = new Answer; gives a compilation error
All I'm trying to do is to create a list of answers which have a name value pair for a number of properties
Any guidance on this greatly appreciated - Especially if there is a better way
Answer anAnswer = new Answer();

Categories

Resources