Solved

Returning an Array in Coldfusion Webservices

Posted on 2012-04-11
8
284 Views
Last Modified: 2013-08-25
Hi

I'm trying to create webservices in Coldfusion that will return an array.  I'm receiving the following error.  

Error Msg: Cannot perform web service invocation STATUSOFORDER. The fault returned when invoking the web service operation is:
'' java.lang.NullPointerException

My code is below. I'm running coldfusion 9.01 and IIS on Windows 2003

status_order.cfc


<cfcomponent>
<cffunction name="STATUSOFORDER" access="remote" returntype="array" output="no">
      <cfargument name="req" type="string" required="yes">
      <cfargument name="stock_number" type="string" required="yes">
      


      <cfquery name="mg" datasource="#application.datasource#" dbtype="ODBC">
            SELECT order_status, order_date , order, stock_number from orders
            WHERE order = '#arguments.req#'
            and stock_number = '#arguments.stock_number#'
      </cfquery>

      <cfset myrow = 2>
      <cfset returnarray = ArrayNew(2)>


      <cfset returnarray[1][1] = "ORDER">
      <cfset returnarray[1][2] = "STOCK NUMBER">
      <cfset returnarray[1][3] = "ORDER STATUS">
      <cfset returnarray[1][4] = "ORDER DATE">

      <cfloop query="mg">

            <cfset returnarray[myrow][1] = "#mg.order#">
            <cfset returnarray[myrow][2] = "#mg.stock_number#">
            <cfset returnarray[myrow][3] = "#mg.order_status#">
            <cfset returnarray[myrow][4] = "#mg.order_date#">
      </cfloop>

<cfreturn duplicate(returnarray)>

</cffunction>

</cfcomponent>



consumestatus_order.cfm

<cfinvoke
webservice="https://localhost/cfws/status_order.cfc?wsdl"
method="STATUSOFORDER"
returnvariable="strg">
<cfinvokeargument name="REQ" value="NKR1234LP">
<cfinvokeargument name="stock_number" value="053010750">
</cfinvoke>

<cfdump var="#strg#">
0
Comment
Question by:curtis247
  • 4
  • 2
  • 2
8 Comments
 
LVL 2

Expert Comment

by:sumCold
ID: 37834166
Try to return query directly from cfc, your call has no problem, i guess problem is with your array. Try to modify your cfc :

<cfcomponent>
<cffunction name="STATUSOFORDER" access="remote" returntype="query" output="no">
      <cfargument name="req" type="string" required="yes">
      <cfargument name="stock_number" type="string" required="yes">
     


      <cfquery name="mg" datasource="#application.datasource#" dbtype="ODBC">
            SELECT order_status, order_date , order, stock_number from orders
            WHERE order = '#arguments.req#'
            and stock_number = '#arguments.stock_number#'
      </cfquery>

     

<cfreturn mg>

</cffunction>
</cfcomponent>


and after this your call is ok, so I keep the same. If still it not work try query and call your web service directly from browser to test it is on right path.
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 37834175
FWIW it works perfectly for me with cf9.0.1.  I've only seen mentions of problems in older versions.
 
I'm assuming you're using an array for smoother support with non-CF sites too? If so, do you really have to return a 2D array? Most people find arrays of structures a lot more intuitive.  

ie   <cfset returnarray = arrayNew(1)>      
      <cfloop query="mg">
            <cfset row = structNew()>
            <cfset row.Order  = mg.order>
            <cfset row.StockNumber = mg.stock_number>
            <cfset row.OrderStatus = mg.order_status>
            <cfset row.OrderDate = mg.order_date>
            <cfset arrayAppend(returnarray, row)>
      </cfloop>

      <cfreturn returnArray>

<cfset returnarray[myrow][1] = "#mg.order#">

A few tips totally unrelated to your error

- it's cleaner to lose the # signs: ie use
          <cfset returnarray[myrow][1] = mg.order >

- constructing 2d arrays is simpler using arrayAppend

      <!--- create intial elements [1][2][3][4] --->
       <cfset row = []>
       <cfset arrayAppend(row , mg.order)>
       ...
       <cfset arrayAppend(row , mg.order_date)>
      <!--- append it to main array --->
       <cfset arrayAppend(returnarray, row )>

- be sure to use cfqueryparam  and var scope all function variables
0
 

Author Comment

by:curtis247
ID: 37837388
I changed my array to

<cfset returnarray = arrayNew(1)>      
      <cfloop query="mg">
            <cfset row = structNew()>
            <cfset row.Order  = mg.order>
            <cfset row.StockNumber = mg.stock_number>
            <cfset row.OrderStatus = mg.order_status>
            <cfset row.OrderDate = mg.order_date>
            <cfset arrayAppend(returnarray, row)>
      </cfloop>

but, I'm still receiving the same error.

'' java.lang.NullPointerException
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:curtis247
ID: 37837396
Additionally, returning the query is not an option for me.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37838297
but, I'm still receiving the same error.

Make sure it's not still using the old definition. Use  refreshWSDL="yes|no" or drop/recreate it.

java.lang.NullPointerException

If it persists, what's the full trace message?
0
 
LVL 2

Expert Comment

by:sumCold
ID: 37838711
Hey I guess you have to try first returning simple string from cfc if it work then try to query then try array....
if still not work then hit the web service url on browser

e.g.

https://localhost/cfws/status_order.cfc?wsdl (if you use local environment why 'https' try 'http')
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37838850
If it persists, what's the full trace message?

.. because NullPointerException could be anything, even an undefined variable. ie It's probably the most common error there is .
0
 
LVL 52

Expert Comment

by:_agx_
ID: 39438099
Since it worked fine for both of us w/cf 9.0.1, it must have been an environment specific issue.

What ended up being the problem, and how did you resolve it?
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
Online collaboration is quickly becoming embedded in the workplace, and its benefits are tangible. See what the current landscape looks like and what the future holds for collaboration tools and the future of work.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question