Crashing With IndexOutOfBoundsException

    //My intention in this code is to get the sum of integer values in the NumberOfViews Column for each iteration of the loop
    //ie for each vehicle model in the dataPointsArray
    //but it crashes when it gets to this line
    //grandTotal = (int) response.get(0).get("grandTotal");
    //with this error
    //java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    //at java.util.ArrayList.get(ArrayList.java:437)

        String startDate = "05/05/2021";
        String endDate = "05/22/2021";

        String[] dataPointsArray = new String[] {"Toyota Corolla","Aston Martin V8 Vantage","Alfa Romeo 8C Competizione"};
        for (int i = 0; i < dataPointsArray.length; i++) {

            String whereClause = "AdvertTitle LIKE'" + "%" + dataPointsArray[i] + "%'"+"AND updated >='"+startDate+"'AND updated <='"+endDate+"'";
            DataQueryBuilder queryBuilder = DataQueryBuilder.create();
            queryBuilder.setProperties("Sum(NumberOfViews) as grandTotal");
            queryBuilder.setWhereClause(whereClause);

            int index = i;
            Backendless.Data.of("Master").find(queryBuilder, new AsyncCallback<List<Map>>() {
                @Override
                public void handleResponse(List<Map> response) {

                    int grandTotal;
                    Log.d(getClass().getName(), "Response Size:" + response.size());
                    Log.d(getClass().getName(), "Response object:"+response.toString());

                    grandTotal = (int) response.get(0).get("grandTotal");

                }

                @Override
                public void handleFault(BackendlessFault fault) {

                    Log.d(getClass().getName(), "Fault:" + fault.getMessage());

                }
            });
        }

Hello @Nkekere_Tommy_Minimann

We’ll investigate your issue as soon as possible.

Regards,
Inna

@Nkekere_Tommy_Minimann

I was unable to reproduce the error in my application. Please provide your APP ID so that I can try to reproduce the error with your data.

Regards,
Inna

C2AE98FF-1A1D-BF10-FF3E-64CD97C40D00
SDK version 6.2.1

@Nkekere_Tommy_Minimann

Thanks for the details. Now I was able to reproduce. The internal BKNDLSS-25344 ticket has been created. We will let you know as soon as the problem is fixed.

Regards,
Inna

Please what is the status of this issue?

Hello @Nkekere_Tommy_Minimann ,

the issue is still in progress and will be ready asap.
We will let you know when it’s released.
Thank you for your patience!

Regards,
Stanislaw

Hello @Nkekere_Tommy_Minimann

Please replace your line:

grandTotal = (int) response.get(0).get("grandTotal");

to the:

if( !response.isEmpty() )
{
    Object grandObject = response.get(0).get("grandTotal");
    if( grandObject == null )
    {
        grandTotal = 0;
    }
    else
    {
        grandTotal = (int) grandObject;
    }
}

and check if the problem is reproducible.
If the problem is reproducible after the changes, then write the logs and stacktrace so that I can more easily identify the problem.

It can also be helpful:
How to cast an Object to an int
IndexOutOfBoundsException

grandObject is always null. Even though my database suggests otherwise.

Hello @Nkekere_Tommy_Minimann

Your applicationId=C2AE98FF-1A1D-BF10-FF3E-64CD97C40D00 - ?
In your query:

queryBuilder.setProperties("Sum(NumberOfViews) as grandTotal");

But in your Master table do not contains column NumberOfViews, this why return error:

BackendlessFault{ code: '1054', message: 'Column 'NumberOfViews' does not exist in table 'Master'', detail: 'Column 'NumberOfViews' does not exist in table 'Master'', extendedData: '{}' }

Please add the NumberOfViews column to your Master table or change your query.
After that, please write what result you get.

The column name is ViewCount. I’ve updated my code to
queryBuilder.setProperties(“Sum(ViewCount) as grandTotal”);
and I still get the error.

Hello @Nkekere_Tommy_Minimann

In your code 3 iteration:
1.
whereClause = AdvertTitle LIKE’%Toyota Corolla%'AND updated >='05/05/2021’AND updated <=‘05/22/2021’
result: grandTotal = 9
Since only one object from the Master table meets the specified criteria (objectId = 08C332A8-66EE-40D5-83DE-FD9797825E0B), which has the ViewCount field value = 9.
2.
AdvertTitle LIKE’%Aston Martin V8 Vantage%'AND updated >='05/05/2021’AND updated <=‘05/22/2021’
There are no objects in your Master table that match these search criteria, so grandTotal = null
3.
AdvertTitle LIKE’%Alfa Romeo 8C Competizione%'AND updated >='05/05/2021’AND updated <=‘05/22/2021’
There are no objects in your Master table that match these search criteria, so grandTotal = null

What does the error you are still getting look like?
Have you made the code changes that I wrote about?
Do you have grandTotal = null for all three iterations?
By what criteria do you get grandTotal = null?
When you get grandTotal = null, are there any objects in your table that match the criteria for your search?

Am not meaning to ask u to do my own coding for me, but I’ve been stumped by this problem big time :slight_smile: . Maybe I don’t understand your API as well as I should.
I want to provide a top 10 list. For example: The automotive table has an integer field called ViewCount. And a string field called AdvertTitle. (AdvertTitle is concatenation of Colour, Make, Model, and Year of manufacture) Let’s say in the records there is “black Toyota Corolla 2013” with ViewCount =10 and there is “blue Toyota Corolla 2010” with ViewCount =7 .Summing all ViewCount fields with “Toyota Corolla” in their AdvertTitle fields gives 10+7 =17. I want to determine the Top 10 car models based on summation of ViewCount.
But if I can’t achieve it this way then I’ll like to achieve it by simply getting the top 10 car models based on number of records. ie Count(objectid) where AdvertTitle Like “%Toyota corolla%,” How do I get a list of the top 10 of this count? Thank you :blush:

Hello @Nkekere_Tommy_Minimann

Request:

Master?where=AdvertTitle LIKE '%Toyota Corolla%'&property=Sum(ViewCount)

Result

[
    {
        "___class": "Master",
        "sum": 16
    }
]

Request:

Master?where=AdvertTitle LIKE '%Toyota Corolla%'&property=AdvertTitle&property=ViewCount&sortBy=`ViewCount` desc

Result:

[
	{
		"___class": "Master",
		"AdvertTitle": "White 2005 Toyota Corolla Car",
		"ViewCount": 9
	},
	{
		"___class": "Master",
		"AdvertTitle": "Gold 2005 Toyota Corolla Car",
		"ViewCount": 7
	},
	{
		"___class": "Master",
		"AdvertTitle": "Brown 2004 Toyota Corolla Car",
		"ViewCount": 0
	}
]

At the moment, with your data structure, it is not possible to group by car model, since you do not have a car model field, but only AdvertTitle (combining color, brand, model and year of manufacture).
Now you can find out the 10 most viewed AdvertTitle:
Request:

Master? PageSize = 10 & property = AdvertTitle & property = ViewCount & sortBy = ViewCount desc

Result:

[
	{
		"___class": "Master",
		"AdvertTitle": "Black 2000 Aston Martin V8 Vantage Car",
		"ViewCount": 11
	},
	{
		"___class": "Master",
		"AdvertTitle": "White 2005 Toyota Corolla Car",
		"ViewCount": 9
	},
	{
		"___class": "Master",
		"AdvertTitle": "Brown 2001 Alfa Romeo 8C Competizione Car",
		"ViewCount": 9
	},
	{
		"___class": "Master",
		"AdvertTitle": "Red 2010 Toyota Camry Car",
		"ViewCount": 9
	},
	{
		"___class": "Master",
		"AdvertTitle": "Black 2009 Lexus ES 350 Car",
		"ViewCount": 9
	},
	{
		"___class": "Master",
		"AdvertTitle": "Red 2005 Honda Accord Car",
		"ViewCount": 8
	},
	{
		"___class": "Master",
		"AdvertTitle": "Silver 2012 Honda CR-V Car",
		"ViewCount": 8
	},
	{
		"___class": "Master",
		"AdvertTitle": "Gold 2014 Ford Explorer Car",
		"ViewCount": 7
	},
	{
		"___class": "Master",
		"AdvertTitle": "Gold 2005 Toyota Corolla Car",
		"ViewCount": 7
	},
	{
		"___class": "Master",
		"AdvertTitle": "Mixed Breed Akbash Dog",
		"ViewCount": 6
	}
]

In order to receive the data that you want, you need to add a model field to the Master table, after that you can:
1.

Find out the total number of views of the model and get the 10 most viewed
Request:

Master?pageSize=10&property=model&property=Sum(ViewCount) as grandTotal&sortBy=grandTotal desc&groupBy=model

Number of model variations
Request:

Master?property=model&property=count(AdvertTitle) as modelVariations&groupBy=model

Request:

Master?pageSize=10&property=model&property=count(AdvertTitle) as modelVariations&groupBy=model&sortBy=modelVariations desc

Documentation:

Thank you very much, this is more than enough for me :slight_smile: