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

发布于 2024-08-21 11:46:44 字数 2806 浏览 8 评论 0原文

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

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

这是堆栈跟踪:

  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

I'm having trouble with some of my code and I really cant trouble shoot this error.

I'm using Interprolog(Java+Prolog) see here

here is the stacktrace:

  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)

and the code in question:

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

any ideas? I'm hitting a brickwall...

oh and line 24 is the second line of code i posted.

edit: content of userlist.p:

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

following on from some of the suggestions below, i moved loginhandler and related classes to their own package and also userlist.p to the package, and now i get this error:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掩于岁月 2024-08-28 11:46:44

我想是的,开源。它正在这样做:

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

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

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

哦,你可以获得来源。它位于从 zip 文件 链接中。 declarativa.com/interprolog/INSTALL.htm" rel="nofollow noreferrer">网站。

Yay for open source I suppose. It's doing this:

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

rc is your calling class. So I suppose your LoginHandler class is in the default package and that is tripping this code up? Try putting it in a package, i.e. add package blah; to the top of it and move it to a directory with that name.

It does this because it assumes the userlist.pl is actually in the same location as your code and it's trying to use the package name to find the location your code was loaded from. I don't think you can use consultFromPackage() the way you are trying to actually, since it supposedly can only parse a filename, not a full blown path.

Oh and you can get the source. It's in the zip file linked from the website.

飘然心甜 2024-08-28 11:46:44

路径中的第二个 \\ 是否会混淆?如果它正在执行任何操作来检查路径中的每个目录,它可能会被空字符串混淆,也许?

Is the second \\ in the path confusing it? If it's doing anything to examine each directory in the path it could be confused by an empty string, maybe?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文