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

时间:2010-02-12 15:01:38

标签: java prolog interprolog

我的部分代码出现问题,我真的无法解决此错误。

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

这是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)

和有问题的代码:

      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

2 个答案:

答案 0 :(得分:2)

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

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

rc是您的通话类。所以我认为你的LoginHandler类在默认包中并且正在使这个代码绊倒?尝试将其放入包中,即将package blah;添加到其顶部并将其移动到具有该名称的目录。

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

哦,你可以得到消息来源。它位于zip file链接的the website

答案 1 :(得分:0)

路径中的第二个\\是否令人困惑?如果它正在做任何事情来检查路径中的每个目录,它可能会被一个空字符串混淆,也许?