简体   繁体   中英

IndexOutOfBoundsException in LinkedList

I'm getting IndexOutOfBoundsException, which looks like kind of impossible to me.

Code:

public class SomethingCalculator {

    @Nullable
    private Config mConfig;
    @Nullable
    private Long mTime;
    final private List<Long> mLinkedList = new LinkedList<>();


    public synchronized void setupWithConfiguration(Config config, Long time) {
        //config and time are non null always
        mTime = time;
        mConfig = config;
        generatLookUp();
    }


    public synchronized void reset() {
        mConfig = null;
        mTime = null;
        mLinkedList.clear();
    }


    @Nullable
    public synchronized Long getTheValue(long ms) {
        if (mConfig == null)
            return null;

        // getting exception here
        if (ms > mLinkedList.get(mLinkedList.size() - 1)) {
            return 0l; // something
        }
        return 0l; // something
    }


    private synchronized void generatLookUp() {
        mLinkedList.clear();
        if (mConfig == null || mTime==null)
            return;
        // mLinkedList will always have size > 0 after executing this
        // based upon config this method add elements to mLinkedList

        // adding dummy values
        mLinkedList.add(1L);
        mLinkedList.add(2L);
    }
}

getting an exception while calling SomethingCalculator.getTheValue()

Exception:

Fatal Exception: java.lang.IndexOutOfBoundsException: Index: 4, Size: 0
 at java.util.LinkedList.checkElementIndex(LinkedList.java:565)

Not sure if mMylinkedList.size() returns 4 then how mMylinkedList.get(4) throws IndexOutOfBoundsException.

After looking into generated AAR it was found that the synchronized keyword was stripped thus this exception can occur. The synchronized keyword was stripped due to a proguard optimization.

Removed this:

-optimizations !method/marking/synchronized

Now i can see synchronized keyword in the generated AAR.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM