Java help - StringIndexOutOfBoundsException - String index out of range: -1

2.2k views Asked by At

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
2

There are 2 answers

4
wds On BEST ANSWER

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.

0
Alex Poole On

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?