Solved

Returning an Array in Coldfusion Webservices

Posted on 2012-04-11
8
288 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When coding a Webservice to provide data, the normal way is to create a method that returns a Dataset object.   But a Dataset cannot be used easily from another platform, such as PHP,  FLAS (FLEX) , etc. Solution : Return a XML Document instea…
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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