• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using java object in LHS of a Jess rule failed

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic