How do I get the first n characters of a string without checking the size or going out of bounds?

Here’s a neat solution: String upToNCharacters = s.substring(0, Math.min(s.length(), n)); Opinion: while this solution is “neat”, I think it is actually less readable than a solution that uses if / else in the obvious way. If the reader hasn’t seen this trick, he/she has to think harder to understand the code. IMO, the code’s meaning … Read more

JavaFX ComboBox change value causes IndexOutOfBoundsException

In JavaFX, you cannot change the contents of an ObservableList while a change is already in progress. What is happening here is that your listeners (any of the ones you try) are being fired as part of the box.getSelctionModel().getSelectedItems() ObservableList changing. So basically, you cannot change the selection while a selection change is being processed. … Read more

Vector assignment crashing

In addition to what @Saurav posted, newMatrix is empty so you cannot assign values to newMatrix[i][j]. You can fix this by initializing the vectors with a given size: vector< vector<int> > resizeVector(vector< vector<int> > m) { vector< vector<int> > newMatrix(m.size()); int i,j; for (i = 0; i < m.size(); i++) { newMatrix[i].resize(m[i].size()); for(j = 0; … Read more

Understand Arraylist IndexOutOfBoundsException in Android

java.util.ArrayList.throwIndexOutOfBoundsException: Invalid index 3, size is 3 It means you have ArrayList which having 3 elements where you can get each element like 0,1,2 positions. And you are trying to read 4th element which does not exists into ArrayList. java.util.ArrayList.throwIndexOutOfBoundsException: Invalid index 0, size is 0 It means you have a empty ArrayList, and you … Read more

Why doesn’t list have safe “get” method like dictionary?

Ultimately it probably doesn’t have a safe .get method because a dict is an associative collection (values are associated with names) where it is inefficient to check if a key is present (and return its value) without throwing an exception, while it is super trivial to avoid exceptions accessing list elements (as the len method … Read more

RecyclerView: Inconsistency detected. Invalid item position

I had a (possibly) related issue – entering a new instance of an activity with a RecyclerView, but with a smaller adapter was triggering this crash for me. RecyclerView.dispatchLayout() can try to pull items from the scrap before calling mRecycler.clearOldPositions(). The consequence being is that it was pulling items from the common pool that had … Read more

Why does ArrayIndexOutOfBoundsException occur and how to avoid it in Android? [closed]

This exception is thrown when you try to access an array item that doesn’t exist: String [] myArray = new String[2]; myArray[2] = “something”; // Throws exception myArray[-1] = “something”; // Throws exception You should check that your index is not negative and not higher than the array length before accessing an array item.

No out of bounds error

C doesn’t check array boundaries. A segmentation fault will only occur if you try to dereference a pointer to memory that your program doesn’t have permission to access. Simply going past the end of an array is unlikely to cause that behaviour. Undefined behaviour is just that – undefined. It may appear to work just … Read more