Solved

Returning an Array in Coldfusion Webservices

Posted on 2012-04-11
8
278 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
 

Author Comment

by:curtis247
ID: 37837396
Additionally, returning the query is not an option for me.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For some reason Google Apps has decided to reserve 'www' for your new domain which results in an error when trying to map the web address 'www' to a certain site in your Google Sites. The error is 'this address is already being used' and after some …
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now