I get a java.lang.NullPointerException error

Hello,

This runs fine in the simulator but when I click the button in the app on my phone I get that error. Can someone tell me why? The error is on the network.request line and you can see I have data entered for the params.

local function handlebtnSendEvent( event ) if ( "ended" == event.phase ) then print( "Button was pressed and released" ) local foo = {} foo.chat = { {FromUser="Warren", ChatLine="123"} } local encode = json.encode (foo) local params = {} params.body = encode network.request( "http://www.domainhere.com/chatin.aspx?a=encode", "GET", networkChatInListener, params) end end

I see a couple of things…

1.  The ?a=encode at the end of the URL is part of the “parameters” that get passed.  You either need to put all your parameters directly on the URL or all of t hem in the params table.  What is likely happening is the URL being requested ends up being something like:

      http://www.domainhere.com/chatin.aspx?a=encode?body=your jsonencoded string

instead of:

      http://www.domainhere.com/chatin.aspx?a=encode&body=your jsonencoded string.

It’s subtle but a big difference in how things are handled.

2.  I don’t think a json encoded string is necessarily safe to pass on a URL.  Since you are using GET, this likely needs to be URL encoded in addition to being json encoded.

Rob

Okay thanks. So what do I need to change to pass the encoded JSON to the URL?

Warren

Google “lua urlencode”

That should get your some options to use to encode the string.  Lets say you end up with a function called “urlencode”, then you would do something like:

params.body = urlencode( encode )

Is there another way to pass the data than at the end of a URL? How else does other people submit JSON data to a web page to be used?

I then just tried to post the data to the page since it is one record and nothing happens these ways:

1st Try: U = "http://www.domain.com/chatin.aspx?t=Warren: Hi there today!!!" network.request(U , "GET", networkChatInListener) 2nd Try: local headers = {} headers["Content-Type"] = "application/x-www-form-urlencoded" headers["Accept-Language"] = "en-US" local body = "t=Warren: The sky is blue today!" local params = {} params.headers = headers params.body = body network.request( "http://www.domain.com/chatin.aspx", "POST", networkChatInListener, params)

Are most people creating a JSON file and sending it to the server? There is not much documentation on submitting the JSON, just up to the point of encoding it and then the docs stop.

How you pass things is 100% dependent on what the server side script is looking for.  Some scripts look for GET parameters (key value pairs passed on the URL), while others look for POST based passing where the data is passed in key-value pairs in the input data IO stream.

You choose “GET” or “POST” depending on what the script expects.

POST data does not need URL encoded because it’s not passed on the URL where spaces and special characters have to be converted to Hex strings.

Now as far as JSON data, I don’t know of anyone who actually passes it as a GET parameter.  If your JSON data is just Key-value pairs, most people would probably just pass the data directly.  If you’re sending it to the server, I would think POST would make the most sense

I see a couple of things…

1.  The ?a=encode at the end of the URL is part of the “parameters” that get passed.  You either need to put all your parameters directly on the URL or all of t hem in the params table.  What is likely happening is the URL being requested ends up being something like:

      http://www.domainhere.com/chatin.aspx?a=encode?body=your jsonencoded string

instead of:

      http://www.domainhere.com/chatin.aspx?a=encode&body=your jsonencoded string.

It’s subtle but a big difference in how things are handled.

2.  I don’t think a json encoded string is necessarily safe to pass on a URL.  Since you are using GET, this likely needs to be URL encoded in addition to being json encoded.

Rob

Okay thanks. So what do I need to change to pass the encoded JSON to the URL?

Warren

Google “lua urlencode”

That should get your some options to use to encode the string.  Lets say you end up with a function called “urlencode”, then you would do something like:

params.body = urlencode( encode )

Is there another way to pass the data than at the end of a URL? How else does other people submit JSON data to a web page to be used?

I then just tried to post the data to the page since it is one record and nothing happens these ways:

1st Try: U = "http://www.domain.com/chatin.aspx?t=Warren: Hi there today!!!" network.request(U , "GET", networkChatInListener) 2nd Try: local headers = {} headers["Content-Type"] = "application/x-www-form-urlencoded" headers["Accept-Language"] = "en-US" local body = "t=Warren: The sky is blue today!" local params = {} params.headers = headers params.body = body network.request( "http://www.domain.com/chatin.aspx", "POST", networkChatInListener, params)

Are most people creating a JSON file and sending it to the server? There is not much documentation on submitting the JSON, just up to the point of encoding it and then the docs stop.

How you pass things is 100% dependent on what the server side script is looking for.  Some scripts look for GET parameters (key value pairs passed on the URL), while others look for POST based passing where the data is passed in key-value pairs in the input data IO stream.

You choose “GET” or “POST” depending on what the script expects.

POST data does not need URL encoded because it’s not passed on the URL where spaces and special characters have to be converted to Hex strings.

Now as far as JSON data, I don’t know of anyone who actually passes it as a GET parameter.  If your JSON data is just Key-value pairs, most people would probably just pass the data directly.  If you’re sending it to the server, I would think POST would make the most sense