1

From an answer in this my previous question: how to create in best way an instance from the class object

I'm trying to do what he suggests but I don't know how to fix the error.

Code:

import java.lang.reflect.*;

class Foo {
   public <T> T create(float x, float y, Class<T> myClass) 
   throws Exception {
      Constructor<T> toCall = myClass.getConstructor(float.class, float.class);
      return toCall.newInstance(x, y);
  }
}

class Dog {
   public Dog(float x, float y) {
      print(x);
      print(y);
   } 
}

Foo foo = new Foo();

try {
   foo.create(10.0f, 10.0f, Dog.class);
 } catch (Exception e) {
   print(e);
}

Exception:

java.lang.NoSuchMethodException: sketch_140319d$1Dog.<init>(float, float)

Community
  • 1
  • 1
Manuel Di Iorio
  • 3,631
  • 5
  • 29
  • 31
  • 1
    Do something like `for(Constructor> c : Dog.class.getDeclaredConstructors()) print(c);` (or however you print a line in processing) and tell us what it says. – Radiodef Mar 19 '14 at 03:36
  • result: public sketch_140319e$1Dog(sketch_140319e,float,float) – Manuel Di Iorio Mar 19 '14 at 03:38
  • 1
    The answer by @DavidWallace is correct then. Dog is an inner class. You need to either pass it a `sketch_140319e` instance or declare it as static (if processing lets you do either of those things). – Radiodef Mar 19 '14 at 03:41
  • Yeah! I have found this: http://www.processing.org/reference/environment/ Specifically in the "advanced" section of the sketch guide. It's possibile to write "straight java code" renaming the sketch suffix.. only renaming with .java this code works perfectly printing 10.0 thank you so much radiodef and david wallace :D – Manuel Di Iorio Mar 19 '14 at 03:53

2 Answers2

3

As I have just commented on your other question, this won't work if Dog is a non-static inner class of sketch_140319, which the error message suggests. I guess you stripped out the sketch_140319 class from your question - I don't know why you did that, when this is what the problem was.

You need to either make Dog static, or add sketch_140319.class as the first argument to getConstructor and an instance of sketch_140319 as the first argument to newInstance.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
2

you can't call

foo.create(10.0, 10.0, Dog.class); 

because 10.0 is a double, not a float

to call it like a float, do

foo.create(10.0f, 10.0f, Dog.class);

Update: my code is

class Dog {
    public Dog(float x, float y) {
        print(x);
        print(y);
    }

    private void print(float x) {
        System.out.println(x);
    }
}

and

import java.lang.reflect.Constructor;

class Foo {
   public <T> T create(float x, float y, Class<T> myClass) 
   throws Exception {
      Constructor<T> toCall = myClass.getConstructor(float.class, float.class);
      return toCall.newInstance(x, y);
  }
}

and

public class SomeOtherClass {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Foo foo = new Foo();

        try {
           foo.create(10.0f, 10.0f, Dog.class);
         } catch (Exception e) {
           e.printStackTrace();
        }

    }
}

and it returned

10.0
10.0
Leo
  • 6,480
  • 4
  • 37
  • 52