gpt4 book ai didi

Java 帮助 - StringIndexOutOfBoundsException - 字符串索引超出范围 : -1

转载 作者:行者123 更新时间:2023-12-02 08:34:41 31 4
gpt4 key购买 nike

我的一些代码遇到了问题,我真的无法解决这个错误。

我正在使用 Interprolog(Java+Prolog) 请参阅 here

这是堆栈跟踪:

  Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
at com.declarativa.interprolog.AbstractPrologEngine.copyToTemp(AbstractPrologEngine.java)
at com.declarativa.interprolog.AbstractPrologEngine.consultFromPackage(AbstractPrologEngine.java)
at LoginHandler.actionPerformed(LoginHandler.java:24)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

以及有问题的代码:

      myEngine = new NativeEngine();
myEngine.consultFromPackage("C:\\interprolog212a\\userlist.p", LoginHandler.class);
boolean x = myEngine.deterministicGoal("hasAccess(user,pass)");

有什么想法吗?我碰壁了...

哦,第 24 行是我发布的第二行代码。

编辑:userlist.p的内容:

hasAccess(tom,123).
hasAccess(bob,456).

根据下面的一些建议,我将 loginhandler 和相关类移至它们自己的包中,并将 userlist.p 移至该包中,现在我收到此错误:

com.declarativa.interprolog.util.IPException: Problem consulting from package         archive:C:\Users\Keval\AppData\Local\Temp\IP_5283895338735856757\userlist.p

最佳答案

我认为开源是好的。它正在这样做:

String className = rc.getName();    
String packageName = className.substring(0,className.lastIndexOf('.'));

rc 是您的调用类。所以我想您的 LoginHandler 类位于默认包中,这会导致此代码出错吗?尝试将其放入包中,即将 package blah; 添加到其顶部并将其移动到具有该名称的目录。

它这样做是因为它假设 userlist.pl 实际上与您的代码位于同一位置,并且它尝试使用包名称来查找加载代码的位置。我认为您不能按照您实际尝试的方式使用 consultFromPackage() ,因为它应该只能解析文件名,而不是完整的路径。

哦,你可以获得来源。它位于 zip file链接自 the website .

关于Java 帮助 - StringIndexOutOfBoundsException - 字符串索引超出范围 : -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2252657/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com