0

I use the following method, but every time i use it i got error. I cant figure out why because i perfrom this checking

if(unWanted == null || unWanted[0] == null)

The error is in this code:

unWanted[0] == null

but if i do only

 if(unWanted == null)

It doest not see unWaned as null.

Thank for helping :)

the error code:

05-12 06:24:41.293: E/AndroidRuntime(24373): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.workoutlog/com.example.workoutlog.AddWorkOutPage}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0

My method:

public void checking(){

        DataBaseMain data = new DataBaseMain(this);
        data.open();
        String[] unWanted = data.getAllUnwantedExercies();  
        data.close();

        if(unWanted == null || unWanted[0] == null)
                    Toast.makeText(this, "good", Toast.LENGTH_LONG).show();
            else
        Toast.makeText(this, "bad", Toast.LENGTH_LONG).show();

The method to get the String array from my DB.

   public String[] getAllUnwantedExercies() {

        Cursor c = ourDatabase.query(true, TABLE_NAME, new String[] {COLUMN_NOT_ON_LIST_EXERCISE}, null, null, COLUMN_NOT_ON_LIST_EXERCISE, null, null, null);

        int dayExercise = c.getColumnIndex(COLUMN_NOT_ON_LIST_EXERCISE);

        if(c.getCount() < 1)
            return null;

        int f = 0;

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){

            if(c.getString(dayExercise) != null && c.getString(dayExercise).equals("") == false)
                f++;
        }

        String[] list = new String[f];

        int j = 0;
        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            if(c.getString(dayExercise) != null && c.getString(dayExercise).equals("") == false){
            list[j] = c.getString(dayExercise);
            j++;
            }
        }

        return list;
    }

5 Answers 5

3
unWanted[0] == null

It's clear that your array has no values in it. Attempting to reference the first index of an array of length 0, as explained in your stack trace, is a run time error.

unWanted == null

This doesn't work because the array object itself is not null.

A work around

A simple solution here is, at the end of your function, check the length of the array. If it is 0, you know it has no values, and you can return null.

if(list.length == 0)
{
    return null;
}
else
{
    return list;
}

or more concisely:

return list.length == 0? null:list;

Then when you get your array back from your function, all you need to do is test to check if the array is null.

if(unWanted == null)
{
    // Array is empty.
}
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe just change this line:

if(unWanted == null || unWanted[0] == null)

By this one:

if(unWanted.length <= 0)

Comments

0
unWanted[0] == null

Here you are trying to check the first position of your array is null or not. Instead of this check your array size is 0 or not.

if(unWanted.length==0){
   // your code
}

I hope this will help you.

Comments

0

You need to check that the length is greater than 0. It is not null because you are returning a list albeit an empty list. So it isn't null but also doesn't have a length

Comments

0

unWanted is not null does not mean you can reference it is first element by using unWanter[0] because it might be an empty array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.