Realm-java: ArrayIndexOutOfBoundsException

Created on 10 Feb 2016  路  10Comments  路  Source: realm/realm-java

I'm getting the error java.lang.ArrayIndexOutOfBoundsException: columnIndex is less than 0. It appears it is being caused by my RecyclerViewAdapter. The constructor the error links can is -

    public RecyclerViewAdapter(
            android.content.Context context,
            RealmResults<dbItem> realmResults,
            boolean automaticUpdate,
            boolean animateIdType) {
        super(context, realmResults, automaticUpdate, animateIdType);
    }

This is my first time using Realm in a project. Am I calling something wrong?

T-Help

Most helpful comment

Hi @HarrisDoug
Thank you for the project, I can reproduce the issue now. It seems to be crashing inside the RecyclerViewAdapter, which is unfortunately not managed by us, so there is not much we can do about it. You should create an issue here instead: https://github.com/thorbenprimke/realm-recyclerview

The root cause seem to be that you are missing primary keys in your model classes you want to use with the RecyclerViewAdapter though.

All 10 comments

columnIndex is less than 0 is an internal error that means that a field doesn't exists. I am a bit surprised that such an error propagate all the way up to you as it should have been formatted to be easier to understand.

It is hard to tell from the context why you are getting it. Can you show what the code looks like around the place where you call that constructor. It would be even better if you could reproduce it in a small sample project.

package com.example.harris.school;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.zip.Inflater;

import io.realm.RealmBasedRecyclerViewAdapter;
import io.realm.RealmResults;
import io.realm.RealmViewHolder;
import io.realm.internal.Context;

/**
 * Created by Harris on 2/6/2016.
 */



public class RecyclerViewAdapter extends RealmBasedRecyclerViewAdapter<dbItem,
        RecyclerViewAdapter.ViewHolder> {
    private static final String TAG = "MyLog";
    public RecyclerViewAdapter(
            android.content.Context context,
            RealmResults<dbItem> realmResults,
            boolean automaticUpdate,
            boolean animateIdType) {
        super(context, realmResults, automaticUpdate, animateIdType);
    }

    public class ViewHolder extends RealmViewHolder {

        public TextView subject;
        public TextView Date;
        public TextView details;


        public ViewHolder(LinearLayout container) {
            super(container);
            this.subject = (TextView) container.findViewById(R.id.subject);
            this.Date = (TextView) container.findViewById(R.id.date);
            this.details = (TextView) container.findViewById(R.id.details);

        }
    }

    @Override
    public ViewHolder onCreateRealmViewHolder(ViewGroup viewGroup, int viewType) {
        View v = inflater.inflate(R.layout.view_item, viewGroup, false);
        ViewHolder vh = new ViewHolder((LinearLayout) v);
        return vh;
    }

    @Override
    public void onBindRealmViewHolder(ViewHolder viewHolder, int position) {
        final dbItem nyTimesStory = realmResults.get(position);
        viewHolder.subject.setText(nyTimesStory.getSubject());
        viewHolder.Date.setText(nyTimesStory.getDate());

        viewHolder.details.setText(nyTimesStory.getDetails());
        Log.d(TAG, "It got to the onBindViewHolder with the values: ");
        Log.d(TAG, nyTimesStory.getSubject());
    }

    @Override
    public ViewHolder convertViewHolder(RealmViewHolder realmViewHolder) {
        return null;
    }
}

Hi @HarrisDoug
Sorry for the late reply. There is nothing in the above code that indicate something is wrong.
One think you can try is checking if nyTimesStory.isValid(). If it returns false, it means the object has been deleted.

Here is a 'smaller' version of the project that replicates the issue. I hope I've uploaded it right, I'm pretty new to GitHub. https://github.com/HarrisDoug/columnIndex-Problem

Hi @HarrisDoug
Thank you for the project, I can reproduce the issue now. It seems to be crashing inside the RecyclerViewAdapter, which is unfortunately not managed by us, so there is not much we can do about it. You should create an issue here instead: https://github.com/thorbenprimke/realm-recyclerview

The root cause seem to be that you are missing primary keys in your model classes you want to use with the RecyclerViewAdapter though.

In my case the problem was due to no primary keys defined using @PrimaryKey, as pointed by @cmelchior .

@ahmadalibaloch I m having this same problem and figured out that it is due to the primary key. Can you please suggest me the way to tackle this problem.
Thanks in Advance,

There must be a primary key. So you will add @PrimaryKey to one of your class member variables.Like
{ @PrimaryKey public long Id; }

@ahmadalibaloch I tried it. But realm doenot support auto Increment. So i did,

int key;
       try {
            key = realm.where(CreatePurchaseOrderRow.class).max("Id").intValue() + 1;
           Log.v("TESTKEY","KEY: "+key);
        } catch(ArrayIndexOutOfBoundsException ex) {
            key = 1; // when there is no object in the database yet
        }

this does not work. It throws the same error.
Can you please help on this

I am developing an offline app, so I need negative Ids for my offline records to easily get posted on server. Here is a realmUtils class method to generate next Id.

public static int getNextId(Class clazz, Realm realm) {
        if (realm == null) realm = Realm.getDefaultInstance();
        try {
            int Id = realm.where(clazz).min("Id").intValue() - 1;
            if (Id >= 0)
                return -1;
            else
                return Id;
        } catch (Exception ex) {
            Log.w("Realm Utils Ex", "Exception in generating Id for " + clazz.toString());
            return -1;
        }
    }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

CNyezi picture CNyezi  路  3Comments

wyvern610 picture wyvern610  路  3Comments

jjorian picture jjorian  路  3Comments

bryanspano picture bryanspano  路  3Comments

mithrann picture mithrann  路  3Comments