AutoIncrement Realm failed

Riki Rikmen

I create an activity and want to insert a new realm object it always keep on error http null if i put getNextKey();

here is my activity

package com.example.rikirikmen.billsplit;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.rikirikmen.billsplit.Adapter.BillListAdapter;
import com.example.rikirikmen.billsplit.Adapter.MenuAdapter;
import com.example.rikirikmen.billsplit.Model.Bill;
import com.example.rikirikmen.billsplit.Model.DetailMenu;
import com.example.rikirikmen.billsplit.Model.DetailPerson;
import com.example.rikirikmen.billsplit.Model.PersonInMenu;
import com.example.rikirikmen.billsplit.Model.PersonMenuObj;

import org.w3c.dom.Text;

import java.util.ArrayList;

import io.realm.Realm;
import io.realm.RealmChangeListener;
import io.realm.RealmList;
import io.realm.RealmObject;

public class DialogActivity extends AppCompatActivity {
Realm realm;
private RealmList<DetailPerson> person;
private RealmList<PersonInMenu> pim;
private ListView listView;
private MenuAdapter adapter;
private TextView menuName;
private TextView menuPrice;
private TextView menuQty;
private String bill;
private Button btnOke;
private Button btnCancel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_add_menu);
    realm = Realm.getDefaultInstance();
    bill = getIntent().getStringExtra("bill_ID");
    btnOke = (Button) findViewById(R.id.btnOkMenu);
    btnCancel = (Button) findViewById(R.id.btnCancelMenu);
    menuName = (TextView) findViewById(R.id.txtMenuName);
    menuPrice = (TextView) findViewById(R.id.txtMenuPrice);
    menuQty = (TextView) findViewById(R.id.txtMenuQuantity);
    listView = (ListView) findViewById(R.id.listViewMenu);

    Toast.makeText(this, getNextKey(), Toast.LENGTH_SHORT).show();

    person = realm.where(Bill.class).equalTo("Bill_ID", bill).findFirst().getDetailperson();

        if (person.isEmpty()){


        }
        else{
            adapter = new MenuAdapter(this,R.layout.row_menu, person,bill);
            listView.setAdapter(adapter);
        }

    btnOke.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (menuName.getText().length()==0 || menuPrice.getText().length()==0|| menuQty.getText().length()==0){
                Toast.makeText(DialogActivity.this, "Please fill the menu detail", Toast.LENGTH_SHORT).show();

            }
            else {
                CharSequence priceText = menuPrice.getText();
                int Price = Integer.parseInt(priceText.toString());

                CharSequence qtyText = menuQty.getText();
                int Qty = Integer.parseInt(qtyText.toString());

                realm.beginTransaction();
                DetailMenu menu = realm.createObject(DetailMenu.class);
                menu.setMenuID(getNextKey());
                menu.setMenuName(String.valueOf(menuName.getText()));
                menu.setMenuPrice(Price);
                menu.setQuantity(Qty);

                    for (int i = 0; i < adapter.getPersonMenuObjList().size(); i++) {
                        PersonInMenu pim = realm.createObject(PersonInMenu.class);
                        pim.setPersonID(adapter.getPersonMenuObjList().get(i).getPersonID());
                        pim.setStatus(adapter.getPersonMenuObjList().get(i).isStatus());
                        menu.personInMenus.add(pim);
                    }


                realm.copyToRealmOrUpdate(menu);
                realm.commitTransaction();
                realm.close();


            }
        }
    });



}
int getNextKey()
{
    return realm.where(DetailMenu.class).max("MenuID").intValue() + 1;
}

}

here is my DetailMenu.class

package com.example.rikirikmen.billsplit.Model;


import io.realm.RealmList;
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
import io.realm.annotations.Required;

/**
 * Created by rikirikmen on 4/24/2016.
 */
public class DetailMenu extends RealmObject {

    @PrimaryKey
    private int MenuID;
    private int Quantity;
    private String MenuName;
    private int MenuPrice;
    public RealmList<PersonInMenu> personInMenus;

    public RealmList<PersonInMenu> getPersonInMenus() {
        return personInMenus;
    }

    public void setPersonInMenus(RealmList<PersonInMenu> personInMenus) {
        this.personInMenus = personInMenus;
    }

    public DetailMenu() {
        super();
    }

    public int getMenuPrice() {
        return MenuPrice;
    }

    public void setMenuPrice(int menuPrice) {
        MenuPrice = menuPrice;
    }

    public int getMenuID() {
        return MenuID;
    }

    public void setMenuID(int menuID) {
        MenuID = menuID;
    }

    public String getMenuName() {
        return MenuName;
    }

    public void setMenuName(String menuName) {
        MenuName = menuName;
    }

    public int getQuantity() {
        return Quantity;
    }

    public void setQuantity(int quantity) {
        Quantity = quantity;
    }


}

i dont know why is my getNextKey() is not work, because i used the same way in other activity and it works. Does anyone here have the same problem with me ?

Christian Melchior

max returns a Number or null if no objects exists. So most likely no DetailMenu objects exist and you get a NullPointerException.

Use this instead:

Number max = realm.where(DetailMenu.class).max("MenuID");
return (max != null) ? max.intValue() + 1 : 0;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Realm Update failed - Android

From Dev

Realm database decryption failed

From Dev

Failed to find: io.realm:realm-android:0.71.0

From Dev

Unable to catch `RealmFileException: Realm file decryption failed` exception

From Dev

Unable to catch `RealmFileException: Realm file decryption failed` exception

From Java

PostgreSQL Autoincrement

From Dev

AUTOINCREMENT not working

From Dev

Insert with AutoIncrement

From Dev

Unrecoverable error. mremap(): failed: Out of memory in io_realm_internal_SharedGroup.cpp line 188

From Dev

Retrofit 2.0 response failed and return null for throwback if used Realm object encapsulation

From Dev

Autoincrement field support in dynamoDB

From Dev

Entityframework disable autoincrement on purpose

From Dev

Liquibase not working with mysql autoincrement

From Dev

Autoincrement ID with a prefix

From Dev

Autoincrement id increase by 2

From Dev

Create column with Year and autoincrement

From Dev

perl multithreading issue for autoincrement

From Dev

Entity Framework autoincrement key

From Dev

Add the Autoincrement _id column

From Dev

Javascript autoincrement index

From Dev

Autoincrement a meteor simpleschema member

From Dev

SQL insert into autoincrement

From Dev

Create column with Year and autoincrement

From Dev

SQL injection on autoincrement

From Dev

JPA, GeneratedValue autoincrement with 50

From Dev

AUTOINCREMENT issue in PHP

From Dev

jQuery append autoincrement

From Dev

Hibernate and Postgresql autoincrement not syncing

From Dev

SQlite AUTOINCREMENT with Statement Binding