标签: indexoutofboundsexception

未捕获并忽略ArrayIndexOutOfBoundsException

我想捕获并忽略和ArrayIndexOutOfBoundsException错误(基本上它不是我可以控制的东西,所以我需要我的程序来保持一致).

但是我的try/catch对似乎没有捕获异常而忽略它.希望你能找出我做错了什么.

此行发生异常

content = extractor.getTextFromPage(page);

这是我的代码:

for(int page=1;page<=noPages;page++){
    try{
        System.out.println(page);           
        content = extractor.getTextFromPage(page);
        }
    }   
    catch (ArrayIndexOutOfBoundsException e){
    System.out.println("This page  can't be read");
    }    
}
Run Code Online (Sandbox Code Playgroud)

线程"main"中的异常java.lang.ArrayIndexOutOfBoundsException:来自com的com.lowagie.text.pdf.CMapAwareDocumentFont.decode(未知来源)的com.lowagie.text.pdf.CMapAwareDocumentFont.decodeSingleCID(未知来源)中的索引:02无效.lowagie.text.pdf.parser.PdfContentStreamProcessor.decode(Unknown Source)at com.lowagie.text.pdf.parser.PdfContentStreamProcessor.displayPdfString(Unknown Source)at com.lowagie.text.pdf.parser.PdfContentStreamProcessor $ ShowText.invoke (未知来源)com.lowagie.text.pdf.parser.PdfContentStreamProcessor.invokeOperator(未知来源)位于com.lowagie.text.pdf.parser的com.lowagie.text.pdf.parser.PdfContentStreamProcessor.processContent(未知来源) com.pdfextractor.main.Extractor.main上的.pdfTextExtractor.getTextFromPage(未知来源)(Extractor.java:64)

编辑:我已将try/catch放在for循环中
并添加了堆栈跟踪
并删除了index = 1

java exception-handling indexoutofboundsexception

5
推荐指数
1
解决办法
1万
查看次数

为什么我不能用"." 作为split()函数的分隔符?

我必须输入一个输入文件,并在其末尾添加一个数字作为输出文件.为此,我使用以下代码:

String delimiter = ".";
String[] splitInput = inputLocation.split(delimiter);
String outputLocation = splitInput[0];
Run Code Online (Sandbox Code Playgroud)

我得到以下异常:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
Run Code Online (Sandbox Code Playgroud)

我添加了以下语句来检查splitInput数组的长度,并得到0作为输出.

System.out.println(splitInput.length);
Run Code Online (Sandbox Code Playgroud)

后来,我使用".x"作为分隔符(我的文件是.xls).我可以使用".x"来实现我的目的,但我很好奇为什么不会"." 工作?

java xls exception delimiter indexoutofboundsexception

5
推荐指数
1
解决办法
4776
查看次数

IndexOutofBounds使用Java的读取字节

我在Java 6中使用RandomAccessFile但在读取字节时有一些奇怪的行为.

使用以下代码,在哪里offsetdata适当初始化:

int offset;
byte data[];
randFile.readFully(data, offset, data.length);
Run Code Online (Sandbox Code Playgroud)

我得到以下堆栈跟踪:

null
java.lang.IndexOutOfBoundsException
    at java.io.RandomAccessFile.readBytes(Native Method)
    at java.io.RandomAccessFile.read(RandomAccessFile.java:355)
    at java.io.RandomAccessFile.readFully(RandomAccessFile.java:414)
Run Code Online (Sandbox Code Playgroud)

但与相同的价值观offsetdata,下面的(貌似相同)的代码工作正常!

randFile.seek(offset);

for (int i = 0; i < (data.length); i += 1) {
    data[i] = randFile.readByte();
}
Run Code Online (Sandbox Code Playgroud)

有没有人能够了解为什么会这样?

java indexoutofboundsexception randomaccessfile

5
推荐指数
1
解决办法
2473
查看次数

在java中创建树数据结构?

我试图在java中创建一个树数据结构,其中每个父节点只能有三个子节点,但在节点至少有一个子节点但少于3个子节点的情况下,我一直坚持在树上添加一个节点.我不确定是否应该使用迭代器来迭代我当前节点的节点列表.我尝试使用一个每次add()调用方法时都会增加的变量.这是我的代码:Node class:

public class Node {

    int keyValue;
    int nodeLabel;
    ArrayList<Node> nodeChildren;

    private static int count;

    Node(int _keyValue)
    {
        this.nodeLabel = count;
        this.keyValue = _keyValue;
        this.count++;
        nodeChildren = new ArrayList<Node>();
    }

    public String toString()
    {
        return "Node " + nodeLabel + " has the key " + keyValue;
    }

}
Run Code Online (Sandbox Code Playgroud)

树类:add()方法

Node rootNode;
    int incrementor = 0;

    public void addNode(int nodeKey)
    {
        Node newNode = new Node(nodeKey);

        if (rootNode == null)
        {
            rootNode = newNode;
        } …
Run Code Online (Sandbox Code Playgroud)

java tree nodes data-structures indexoutofboundsexception

5
推荐指数
1
解决办法
1万
查看次数

array[i] = java.lang.IndexOutOfBoundsException:无效的数组范围 10 到 10

我的问题是我在调试过程中不断收到标题中的错误。

array[i] = java.lang.IndexOutOfBoundsException : Invalid array range 10 to 10
Run Code Online (Sandbox Code Playgroud)

以下是for循环。我输入了 values people = 10, payer = 4, rest = 6

int[] array = new int[people];
Random rand = new Random();
int rest = people-payer;

for(int i=0; i<people; i++) {
    if(payer<=0) { /*Payers are already decided*/
        array[i] = 0;
        continue;
    }
    if(rest<=0) {
        array[i] = 1;
        continue;
    }
    else {
        int randomNumber = rand.nextInt(2);
        array[i] = randomNumber;
        if (randomNumber == 1)
            payer--;
        if (randomNumber == 0)
            rest--;
    } …
Run Code Online (Sandbox Code Playgroud)

java arrays random android indexoutofboundsexception

5
推荐指数
1
解决办法
3688
查看次数

在 numpy 数组之外索引时的默认值,即使是非平凡的索引

是否可以从 nd 数组中查找条目而不抛出IndexError?

我希望是这样的:

>>> a = np.arange(10) * 2
>>> a[[-4, 2, 8, 12]]
IndexError
>>> wrap(a, default=-1)[[-4, 2, 8, 12]]
[-1, 4, 16, -1]

>>> wrap(a, default=-1)[200]
-1
Run Code Online (Sandbox Code Playgroud)

或者可能更像 get_with_default(a, [-4, 2, 8, 12], default=-1)

有没有一些内置的方法来做到这一点?我可以要求 numpy 不要抛出异常并返回垃圾,然后我可以用我的默认值替换它吗?

numpy indexoutofboundsexception

5
推荐指数
1
解决办法
1100
查看次数

查找数组中最小元素的索引(Java)

我正在尝试编写一个将整数数组作为参数并返回数组中最小元素的索引的代码块。此外,如果列表是空列表,该函数应返回 -1。

测试代码块。

到目前为止,我有,

public static int indexOfSmallest(int[] array){
    int index = 0;
    int min = array[index];

    for (int i = 1; i < array.length; i++){
        if (array[i] <= min){
        min = array[i];
        index = i;
        }
    }
        return index;
}
Run Code Online (Sandbox Code Playgroud)

但是,我收到此错误并且不确定我需要修复什么。

在此处输入图片说明

任何帮助将非常感激。谢谢你。

java arrays indexoutofboundsexception

5
推荐指数
1
解决办法
1万
查看次数

Spark与众不同,然后加入给定IndexOutOfBoundsException

试图联接两个数据帧A和B。B在联接之前有一个独特的操作。同样,B中的一列与A中的两列相连。这种特定情况给出了IndexOutOfBoundsException。有人遇到过这种情况吗?

详细信息如下。提前致谢!

环境:

spark-shell standalone mode
Spark version 2.3.1
Run Code Online (Sandbox Code Playgroud)

码:

val df1 = Seq((1, "one", "one"), (2, "two", "two")).toDF("key1", "val11", "val12")
val df2 = Seq(("one", "first"), ("one", "first"), ("two", "second")).toDF("key2", "val2")
val df3 = df2.distinct
val df4 = df1.join(df3, col("val11") === col("key2") and col("val12") === col("key2"))
df4.show(false)
Run Code Online (Sandbox Code Playgroud)

例外:

java.lang.IndexOutOfBoundsException: -1
  at scala.collection.LinearSeqOptimized$class.apply(LinearSeqOptimized.scala:65)
  at scala.collection.immutable.List.apply(List.scala:84)
  at org.apache.spark.sql.execution.exchange.EnsureRequirements$$anonfun$reorder$1.apply(EnsureRequirements.scala:233)
  at org.apache.spark.sql.execution.exchange.EnsureRequirements$$anonfun$reorder$1.apply(EnsureRequirements.scala:231)
  at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
  at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48)
  at org.apache.spark.sql.execution.exchange.EnsureRequirements.reorder(EnsureRequirements.scala:231)
  at org.apache.spark.sql.execution.exchange.EnsureRequirements.org$apache$spark$sql$execution$exchange$EnsureRequirements$$reorderJoinKeys(EnsureRequirements.scala:255)
  at org.apache.spark.sql.execution.exchange.EnsureRequirements$$anonfun$org$apache$spark$sql$execution$exchange$EnsureRequirements$$reorderJoinPredicates$1.applyOrElse(EnsureRequirements.scala:277)
  at org.apache.spark.sql.execution.exchange.EnsureRequirements$$anonfun$org$apache$spark$sql$execution$exchange$EnsureRequirements$$reorderJoinPredicates$1.applyOrElse(EnsureRequirements.scala:273)
  at org.apache.spark.sql.catalyst.trees.TreeNode$$anonfun$transformUp$1.apply(TreeNode.scala:289)
  at org.apache.spark.sql.catalyst.trees.TreeNode$$anonfun$transformUp$1.apply(TreeNode.scala:289)
  at org.apache.spark.sql.catalyst.trees.CurrentOrigin$.withOrigin(TreeNode.scala:70)
  at org.apache.spark.sql.catalyst.trees.TreeNode.transformUp(TreeNode.scala:288)
  at org.apache.spark.sql.execution.exchange.EnsureRequirements.org$apache$spark$sql$execution$exchange$EnsureRequirements$$reorderJoinPredicates(EnsureRequirements.scala:273) …
Run Code Online (Sandbox Code Playgroud)

scala indexoutofboundsexception apache-spark

5
推荐指数
1
解决办法
229
查看次数

RecyclerView:检测到不一致。无效的项目位置 - 为什么?

我们的 QA 检测到一个错误:发生了以下与 RecyclerView 相关的崩溃:`

java.lang.IndexOutOfBoundsException:检测到不一致。无效的项目位置 2(offset:2).state:3

一个残酷的解决方法可能是在异常发生时捕获异常并从头开始重新创建 RecyclverView 实例,以避免留下损坏的状态。

但是,如果可能的话,我想更好地理解这个问题(也许从源头上解决它),而不是掩盖它。

该错误很容易重现滚动继续快速创建此崩溃,但它发生时是致命的。

我的工作代码是:

public class MoviesGridRecyclerViewDataAdapter  extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

public static final int VIEW_ITEM = 1;
public static final int VIEW_PROG = -1;
private static final int FADE_DURATION = 500;
int pastVisiblesItems, visibleItemCount, totalItemCount;
private List<VideoEntity> dataList;
private FragmentActivity activity;
private int visibleThreshold = 1;
//private int lastVisibleItem, totalItemCount;
//private boolean loading = false;
private OnLoadMoreListener onLoadMoreListener;
private RecyclerView.OnScrollListener recyclerciewScrollListener;
private RecyclerView recyclerView;
private boolean followLargeItemOnDataRender;
private boolean loading = …
Run Code Online (Sandbox Code Playgroud)

java android indexoutofboundsexception android-recyclerview

5
推荐指数
1
解决办法
1231
查看次数

Compose BottomSheetScaffold IndexOutOfBoundsException 应用程序崩溃

我搜索了与此问题相关的解决方案,但一无所获。我解决了这个问题,所以我在这里发布问题和解决方案,目的是帮助某人。

问题描述

将 Jetpack Compose 从版本更新到 后1.2.0-beta021.2.0-rc02屏幕上的某些内容导致应用程序崩溃。

此特定屏幕上的代码在版本之间没有更改,因此崩溃必须是由我正在使用的 Compose 组件之一引起的。我设法通过反复试验的方法找到了崩溃的根源,并发现问题出在BottomSheetScaffold组件上(带有空,有点content)。

导致崩溃的代码:

BottomSheetScaffold(
        sheetContent = { Text(text = "Some sheet content") }
    ) {
        AlertDialog(
            onDismissRequest = {
                // no-op
            },
            buttons = {
                Text(text = "here goes a button")
            }
        )
    }
Run Code Online (Sandbox Code Playgroud)

崩溃日志:

2022-07-01 10:02:07.185 16257-16257/my.example.app E/AndroidRuntime: FATAL EXCEPTION: main
    Process: my.example.app, PID: 16257
    java.lang.IndexOutOfBoundsException: Index 0 is out of bounds. The list has 0 elements.
        at androidx.compose.runtime.collection.MutableVectorKt.checkIndex(MutableVector.kt:1135)
        at androidx.compose.runtime.collection.MutableVectorKt.access$checkIndex(MutableVector.kt:1)
        at androidx.compose.runtime.collection.MutableVector$MutableVectorList.get(MutableVector.kt:940) …
Run Code Online (Sandbox Code Playgroud)

android indexoutofboundsexception android-jetpack-compose android-bottomsheetdialog

5
推荐指数
1
解决办法
1749
查看次数