0

I'm currently working on an implementation of a jar file in delphi via the JNI. Here's my problem: I'm unable to create an object via the java implemented builder.

Here's the java code:

public class MyObject {
   // .. code
   public static class Builder {
       protected MyObject instance;

       public Builder() {
          this.instance = new MyObject
       }

       public Builder withString(String s) {
          instance.string = s;
          return this;
       }

       public MyObject Build() {
          // check object values
          return instance;
       }
   }
}

As you can see, the builder class is a subclass of the object.

In delphi, I use this code to access the builder:

jClass := JNIEnv.FindClass('package/MyObject$Builder');
jMid := JNIEnv.GetMethodID(jClass, '<init>', '()V');
jObj := JNIEnv.NewObjectV(jClass, jMid, '');

_jMid := JNIEnv.GetMethodID(jClass, 'withString', '(Ljava/lang/String;)package/MyObject$Builder;');
_jObj := JNIEnv.CallObjectMethodV(jObj, _jMid, 'string');

But I can't seem to get this working, I always get an access violation.

Thanks in advance!

  • 1
    I do not know what's wrong with your Java-Delphi integration but I can strongly recommend you to avoid using standard java identifiers as yours. Especially calling your custom class `Object`. Start from renaming it and moving it into package. Then probably the things will be clearer. – AlexR Nov 17 '14 at 07:38
  • Oh sorry, my object's name is of course not "Object", I just changed it for here. Gonna change it back. – steinsieker Nov 17 '14 at 07:41
  • 1
    Did you try to call package level class first? Inner classes add complexity. And what kind of exception do you get? – AlexR Nov 17 '14 at 07:46
  • 1
    Actually the builder in this code is not a subclass but a static inner class - see http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class – mjn Nov 17 '14 at 08:30
  • Thanks for the help mjn, but I still have no idea how to access this static inner class. – steinsieker Nov 17 '14 at 08:54
  • Which line in the Delphi code exactly causes the AV? – mjn Nov 17 '14 at 10:50

0 Answers0