user2709885
user2709885

Reputation: 423

Unable to save parseobject in Android

Please help. I am very new to Android development and Parse. I am trying to create a list of objects. So I created a class called "listObject" that extends "ParseObject" and I am trying to save listObjects. But I get this runtime error when I try to add object from Android Emulator.:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference
            at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:746)
            at com.parse.ParseObject.<init>(ParseObject.java:158)
            at com.parse.ParseObject.<init>(ParseObject.java:121)
            at com.parse.starter.listObject.<init>(listObject.java:10)
            at com.parse.starter.CreateList.post(CreateList.java:75)
            at com.parse.starter.CreateList.access$000(CreateList.java:29)
            at com.parse.starter.CreateList$1.onClick(CreateList.java:59)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Here's my code:

public class CreateList extends Activity {

    // UI references.
    private EditText postEditBrand;
    private EditText postEditProduct;
    private EditText postEditPrice;
    private TextView characterCountTextView;
    private Button postButton;

    private int maxCharacterCount = 200;

    private ParseGeoPoint geoPoint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_create_list);

        Intent intent = getIntent();

        postEditBrand = (EditText) findViewById(R.id.enterBrand);
        postEditProduct = (EditText) findViewById(R.id.enterProduct);

        postButton = (Button) findViewById(R.id.post_button);
        postButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                post();
            }
        });
    }

    private void post () {
        String brand = postEditBrand.getText().toString().trim();
        String product = postEditProduct.getText().toString().trim();

        // Set up a progress dialog
        final ProgressDialog dialog = new ProgressDialog(CreateList.this);
        dialog.setMessage(getString(R.string.progress_post));
        dialog.show();

        // Create a post.
        listObject post = new listObject();

        post.setBrand(brand);
        post.setProduct(product);
        ParseACL acl = new ParseACL();

        // Give public read access
        acl.setPublicReadAccess(true);
        post.setACL(acl);

        // Save the post
        post.saveInBackground(new SaveCallback() {

            @Override
            public void done(com.parse.ParseException e) {
                dialog.dismiss();
                finish();
            }
        });
    }
}

public class listObject extends ParseObject {
    public String getBrand() {
        return getString("brand");
    }

    public void setBrand(String value) {
        put("brand", value);
    }

    public String getProduct() {
        return getString("product");
    }

    public void setProduct(String value) {
        put("product", value);
    }

    public int getPrice() {
        return getInt("price");
    }

    public void setPrice(Integer value) {
        put("price", value);
    }

    public static ParseQuery<listObject> getQuery() {
        return ParseQuery.getQuery(listObject.class);
    }
}

Upvotes: 2

Views: 1571

Answers (1)

Leonardo
Leonardo

Reputation: 3191

You forgot some points in your code:

You must annotate the class name in your DAO class that extends ParseObject :

@ParseClassName("ListObject") //THIS
public class listObject extends ParseObject {
public String getBrand() {
    return getString("brand");
}

You must add your class before you initialize Parse (Application class):

Call ParseObject.registerSubclass(listObject.class) in your Application constructor before calling Parse.initialize().

Check the docs here

By the way, you should use Java Standards to declare classes, in your case, ListObject. (capital L)

Hope it helps !

Upvotes: 3

Related Questions