Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

id = null after create new record #12

Closed
sigeje opened this issue Dec 11, 2012 · 4 comments
Closed

id = null after create new record #12

sigeje opened this issue Dec 11, 2012 · 4 comments

Comments

@sigeje
Copy link

sigeje commented Dec 11, 2012

I'm confused with this issue and don't know how to resolve it.
I write the code like this (some not related code was erased) :

 public void onReceive(Context context, Intent intent) {

        Conversations conv = null;
        conv = saveToDb(context, sender, body, cat, "R");

        pushNotification(context, conv);

}

public static Conversations saveToDb(Context context, 
        String recipient, String body, String category, String type) {
    String snippet = (body.length() > 30 ) ? body.substring(0, 30) + "..." : body;
    String date = System.currentTimeMillis() + "";

    List<Conversations> result = Conversations.find(
            Conversations.class, "RECIPIENT = ?", new String[]{recipient});

    Conversations conv = null;

    if( result.size() == 0) {
        conv = new Conversations(
                context, snippet, recipient, date);
    } else {
        conv = result.get(0);
        conv.recipient = recipient;
        conv.snippet = snippet;
        conv.date = date;
    }
    conv.save();

    message = new Messages(context, body, date, category, type, conv);
    message.save();

    return conv;
}

private void pushNotification(Context context, Conversations c) {

    Intent resultIntent = new Intent(context, MessagesActivity.class);
    Bundle b = new Bundle();
    b.putLong("conv_id", c.id); // c.id = null, then throw an nullpointerexception
    b.putString("recipient", c.recipient);
    resultIntent.putExtras(b);

}

In function saveToDb, if result.size() == 0 I create new record of Conversation class and then save it and return this object. And then I use the returned object to be parameter for function pushNotification. When function pushNotification read id of the object (c.id), it has value 0 and I get an error. I also have tried to debug this object value in each function and it shown that id has value 0 and others field have been filled with data I want to store in. In saveToDb function, I also save a new record of Messages class, and it has no error.
But if result.size is not 0, this all function is successfully done. what's wrong with my code ? why save a new record of Message class was successful but not for Conversation class ? :-/

@satyan
Copy link
Collaborator

satyan commented Dec 12, 2012

Can you post your Conversation class too.. or see what's the difference between those two classes..

@sigeje
Copy link
Author

sigeje commented Dec 12, 2012

ah, I see the difference, I declare an attribute called id as Long in Conversations class, but I didn't declare it in Messages class. I've try to remove public Long id, and it's successfully work. Why it's error if we declare Long id in SugarRecord subclass Sir ?

Message class :
public class Messages extends SugarRecord {

public String body;
public String date;
public String category;
public String type;

public Conversations conv;

public Messages(Context context) {
    super(context);
}

public Messages(Context context, String body,
        String date, String category, String type, Conversations conv) {
    super(context);
    this.body = body;
    this.date = date;
    this.category = category;
    this.type = type;
    this.conv = conv;
}

}

Conversations class :
public class Conversations extends SugarRecord {

//public Long id; <- it causes an error
public String snippet;
public String recipient;
public String date;

public Conversations(Context context) {
    super(context);
}

public Conversations(Context context, String snippet, String recipient, String date) {
    super(context);
    this.snippet = snippet;
    this.recipient = recipient;
    this.date = date;
}

}

@satyan
Copy link
Collaborator

satyan commented Dec 12, 2012

ok. There's a similar property "Long id" defined in the SugarRecord class too. During reflection, it seems to cause a conflict.

@sigeje
Copy link
Author

sigeje commented Dec 12, 2012

thanks Sir :)

@sigeje sigeje closed this as completed Dec 12, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants