0

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
false
  • 10,264
  • 13
  • 101
  • 209
KP65
  • 13,315
  • 13
  • 45
  • 46
  • What does userList.pl look like? My only guess is that somehow hasAccess(user,pass) doesn't play nice with whatever's in that file. – Daniel Bingham Feb 12 '10 at 15:04
  • hi there, thanks for the reply. I have edited the original post and you can now see the contents of userlist.pl. cheers – KP65 Feb 12 '10 at 15:10
  • Do you have access to the source code for NativeEngine to see what it is trying to get a substring of and how it is getting the index for the substring? – justkt Feb 12 '10 at 15:16
  • i do, i think but how would i do that? break point? – KP65 Feb 12 '10 at 15:24
  • actually it looks like i do not have access to the source. – KP65 Feb 12 '10 at 15:27

2 Answers2

2

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.

wds
  • 31,873
  • 11
  • 59
  • 84
  • hi there. I have also tried ConsultFromPackage(userlist.pl, class) as the pl file is indeed in the same package/dir as the java files/code location. – KP65 Feb 12 '10 at 15:37
  • @keval You might have missed my second edit. Try putting `LoginHandler` in a package. I assume it's currently in the default package (i.e. no package declaration at the top). – wds Feb 12 '10 at 15:40
  • I have moved it to its own package now, will let you know what happens. – KP65 Feb 12 '10 at 15:46
  • Right, moved both the consulting file(userlist.pl) and the loginhandler to its own package, now getting this error: com.declarativa.interprolog.util.IPException: Problem consulting from package archive:C:\Users\Keval\AppData\Local\Temp\IP_5283895338735856757\userlist.p – KP65 Feb 12 '10 at 15:54
0

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?

Alex Poole
  • 183,384
  • 11
  • 179
  • 318