Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No matching ctor found" while trying to populate a Java class from Clojure

Tags:

java

clojure

I am getting a "No matching ctor found" error while trying to populate a Java class from Clojure.

I want to populate this class from Clojure.

import java.util.Date;

public class Account {
    Account() { acct_num = 0; 
                trans_type = 'U';
                trans_amt = 0.00;
                cur_bal = 0.00;
                last_update = null;
               }

    public int acct_num = 0;
    public char trans_type;
    public double trans_amt = 0.00;
    public double cur_bal = 0.00;
    public Date last_update;
}

I can import the class:

ba2-app=> (ns ba2-app (:import Account))
Account

but when I go to populate it, I get this error:

ba2-app=> (:use java.util.Date)
nil
ba2-app=> (Account. 1000 \C 100.00 0.00 (java.util.Date. "12/21/2011"))
java.lang.IllegalArgumentException: No matching ctor found for class Account (NO_SOURCE_FILE:9)

I followed these suggestions to get the Java class built in with my Clojure code. The suggestions which were very helpful, because I can now build the Java class.

Any pointers or suggestions would be helpful. I know the class members should be private, but this is to test out a larger project.

like image 236
octopusgrabbus Avatar asked Dec 21 '11 20:12

octopusgrabbus


2 Answers

Make the constructor public with the public access modifier. Also your constructor doesn't accept any args, but just sets a bunch of members to some values. So you can now actually only call it like: (Account.) (if it were public).

When you want to use a constructor like this: (Account. 1000 \C 100.00 0.00 (java.util.Date. "12/21/2011")) you'll have to add a constructor that accepts these types of arguments:

    public Account(int a, char c, double d1, double d2, Date date) { 
      acct_num = a; 
      trans_type = c;
      trans_amt = d1;
      cur_bal = d2;
      last_update = date;
    }
like image 179
Michiel Borkent Avatar answered Oct 16 '22 19:10

Michiel Borkent


This constructor takes no arguments. You are calling it with lots of arguments. Probably you need to brush up on your Java, or copy some working Java from somewhere else (if your end goal is just to package someone else's Java in your project).

like image 21
amalloy Avatar answered Oct 16 '22 19:10

amalloy