Solved

Returning an Array in Coldfusion Webservices

Posted on 2012-04-11
8
294 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
The task of choosing a web design company to build a website for your business should never be taken in a light manner. Provided the fact that your website will act as a representative to your business and will be responsible for imposing an online …
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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

615 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