Solved

Returning an Array in Coldfusion Webservices

Posted on 2012-04-11
8
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
2017 Webroot Threat Report

MSPs: Get the facts you need to protect your clients.
The 2017 Webroot Threat Report provides a uniquely insightful global view into the analysis and discoveries made by the Webroot® Threat Intelligence Platform to provide insights on key trends and risks as seen by our users.

 

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

Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

Question has a verified solution.

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

Suggested Solutions

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
These instructions are based on installing Owncloud on your new raspberry pi connected with a usb HDD. What do you need Part A? A Raspberry Pi, model B. A boot SD card for the Raspberry Pi. A usb HDD An Ethernet cable to connect to the lo…
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…

734 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