File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using java object in LHS of a Jess rule failed

 
Hope Zhu
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I try to use a property of a javabean in LHS of a rule, it failed with an error message "Can't call method on nil reference: size.". Below is my code. Anyone knows what I did wrong or how should I do it please please help.

public class JessData {
private ArrayList list;

public void setList(ArrayList lst) {
list = lst;
}

public ArrayList getList() {
return list;
}
...
}

In Jess:

(import java.util.*)
(defclass data JessData)
(bind ?x (new JessData))
(definstance data ?x static)

(defrule test_rule
(data (list ?y&: (> (?y size) 0)))
=>
(printout t "test_rule fired" crlf))

I also tried using (test (> (?y size) 0)) I got the same error.

Thanks a lot in advance.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The message (which would have included some more information on subsequent lines) says that you asked Jess to call the method "size" on a nil (i.e., null) reference. Therefore ?y is apparently null. Looking at the code for the "JessData" object, it looks as though the "list" property starts out as null, and doesn't become non-null until somebody calls setList() (which never happens here.)

I believe that the JavaBeans spec prohibits properties from having null values. Jess will allow it, but of course if you actually try to access the null property, as here, there may be problems.
[ September 17, 2004: Message edited by: Ernest Friedman-Hill ]
 
Hope Zhu
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest, the problem solved. Just as you said "the JavaBeans spec prohibits properties from having null values. Jess will allow it", so I added init for list in the javabean code, like
public class JessData {
private ArrayList list = new ArrayList(); ...
}
it works. You are great!
Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic