<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CB1, INC. &#187; coldfusion</title>
	<atom:link href="http://www.cb1inc.com/category/coldfusion/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cb1inc.com</link>
	<description></description>
	<lastBuildDate>Wed, 28 Sep 2011 17:54:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>MethodSelectionException When Invoking Java From ColdFusion MX</title>
		<link>http://www.cb1inc.com/2007/06/14/methodselectionexception-when-invoking-java-from-coldfusion-mx/</link>
		<comments>http://www.cb1inc.com/2007/06/14/methodselectionexception-when-invoking-java-from-coldfusion-mx/#comments</comments>
		<pubDate>Thu, 14 Jun 2007 17:50:16 +0000</pubDate>
		<dc:creator>Chris Barber</dc:creator>
				<category><![CDATA[coldfusion]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[uncategorized]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[In ColdFusion MX, you can create a Java object by calling the handy <a href="http://livedocs.adobe.com/coldfusion/6.1/htmldocs/functi42.htm#wp1102241" target="_blank"><code>CreateObject()</code></a> function.

<pre>&#60;CFSET myObj = CreateObject("java", "java.util.Hashtable")&#62;</pre>

Suppose you have the following Java class:

<div class="cb1-docs-code"><pre>class Person {
    private String name;

    public void setName(String n) {
       this.name = n;
    }
}</pre></div>

Now, let's create an instance in ColdFusion and set a name:

<div class="cb1-docs-code"><pre>&#60;CFSET somebodySpecial = CreateObject("java", "Person")&#62;
&#60;CFSET somebodySpecial.setName("Chuck")&#62;</pre></div>

Cool, but what happens if you pass an argument that isn't a string:

<div class="cb1-docs-code"><pre>&#60;CFSET somebodySpecial.setName(123)&#62;

coldfusion.runtime.java.MethodSelectionException:
    The selected method setName was not found.</pre></div>

Whoops!  ColdFusion wasn't able to find a method of <code>Person</code> with a signature of "<code>public void setName(int)</code>".  What you need to do is cast the arguments to the correct datatype.  For this example, you could do something like this:

<div class="cb1-docs-code"><pre>&#60;CFSET somebodySpecial.setName(ToString(123))&#62;</pre></div>

The problem is what if you need to call methods that use other data types such as int, double, or boolean.  <code>ToString()</code> won't cut it.  Instead use ColdFusion's <a href="http://livedocs.adobe.com/coldfusion/6.1/htmldocs/funct147.htm" target="_blank"><code>JavaCast()</code></a> function:

<div class="cb1-docs-code"><pre>JavaCast(type, variable)

type        "boolean"
            "int"
            "long"
            "float"
            "double"
            "String"

variable    A ColdFusion variable that holds a scalar or string type
</pre></div>

So, the correct way is:

<div class="cb1-docs-code"><pre>&#60;CFSET somebodySpecial.setName(JavaCast("String", 123))&#62;
&#60;CFSET somebodySpecial.setName(JavaCast("String", "Chuck"))&#62;</pre></div>

No more MethodSelectionExceptions!]]></description>
			<content:encoded><![CDATA[<p>In ColdFusion MX, you can create a Java object by calling the handy <a href="http://livedocs.adobe.com/coldfusion/6.1/htmldocs/functi42.htm#wp1102241" target="_blank"><code>CreateObject()</code></a> function.</p>
<pre class="brush: xml; title: ;">
&lt;CFSET myObj = CreateObject(&quot;java&quot;, &quot;java.util.Hashtable&quot;)&gt;
</pre>
<p>Suppose you have the following Java class:</p>
<pre class="brush: java; title: ;">
class Person {
    private String name;

    public void setName(String n) {
       this.name = n;
    }
}
</pre>
<p>Now, let&#8217;s create an instance in ColdFusion and set a name:</p>
<pre class="brush: xml; title: ;">
&lt;CFSET somebodySpecial = CreateObject(&quot;java&quot;, &quot;Person&quot;)&gt;
&lt;CFSET somebodySpecial.setName(&quot;Chuck&quot;)&gt;
</pre>
<p>Cool, but what happens if you pass an argument that isn&#8217;t a string:</p>
<pre class="brush: xml; title: ;">
&lt;CFSET somebodySpecial.setName(123)&gt;
</pre>
<pre class="brush: plain; title: ;">
coldfusion.runtime.java.MethodSelectionException:
    The selected method setName was not found.
</pre>
<p>Whoops!  ColdFusion wasn&#8217;t able to find a method of <code>Person</code> with a signature of &#8220;<code>public void setName(int)</code>&#8220;.  What you need to do is cast the arguments to the correct datatype.  For this example, you could do something like this:</p>
<pre class="brush: xml; title: ;">
&lt;CFSET somebodySpecial.setName(ToString(123))&gt;
</pre>
<p>The problem is what if you need to call methods that use other data types such as int, double, or boolean.  <code>ToString()</code> won&#8217;t cut it.  Instead use ColdFusion&#8217;s <a href="http://livedocs.adobe.com/coldfusion/6.1/htmldocs/funct147.htm" target="_blank"><code>JavaCast()</code></a> function:</p>
<pre class="brush: plain; title: ;">
JavaCast(type, variable)

type        &quot;boolean&quot;
            &quot;int&quot;
            &quot;long&quot;
            &quot;float&quot;
            &quot;double&quot;
            &quot;String&quot;

variable    A ColdFusion variable that holds a scalar or string type
</pre>
<p>So, the correct way is:</p>
<pre class="brush: xml; title: ;">
&lt;CFSET somebodySpecial.setName(JavaCast(&quot;String&quot;, 123))&gt;
&lt;CFSET somebodySpecial.setName(JavaCast(&quot;String&quot;, &quot;Chuck&quot;))&gt;
</pre>
<p>No more MethodSelectionExceptions!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cb1inc.com/2007/06/14/methodselectionexception-when-invoking-java-from-coldfusion-mx/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

