"ERROR: null" when using network.request

Hi there,

I have encountered a problem when I use “network.request”. When I get the HTTP contents using “POST” method, the error occurred. I tried to comment all the code and just run the “network.request”, but the problem still here. I tested with three android devices (4.0, 4.3, 2.3.6). None of them works. But the function works on simulator and iPhone. What should I do to get rid of this problem? Thanks.

The corona version that I am using is 2014.2189a.

Here is the log from the android device:

07-03 04:56:59.800: E/Corona(19149): ERROR: null
07-03 04:56:59.800: W/System.err(19149): java.lang.NullPointerException
07-03 04:56:59.800: W/System.err(19149): at java.util.regex.Matcher.reset(Matcher.java:181)
07-03 04:56:59.810: W/System.err(19149): at java.util.regex.Matcher.<init>(Matcher.java:94)
07-03 04:56:59.810: W/System.err(19149): at java.util.regex.Pattern.matcher(Pattern.java:290)
07-03 04:56:59.810: W/System.err(19149): at network.NetworkRequest.isContentTypeXML(NetworkRequest.java:130)
07-03 04:56:59.810: W/System.err(19149): at network.NetworkRequest.isContentTypeText(NetworkRequest.java:152)
07-03 04:56:59.810: W/System.err(19149): at network.NetworkRequest.access$400(NetworkRequest.java:33)
07-03 04:56:59.810: W/System.err(19149): at network.NetworkRequest$AsyncNetworkRequestRunnable.run(NetworkRequest.java:1567)
07-03 04:56:59.810: W/System.err(19149): at java.lang.Thread.run(Thread.java:856)

Can you post your code that’s doing the network request and it’s listener function?

I just run the following code, but it returns error

 

 

display.setStatusBar( display.HiddenStatusBar )

 

network = require(“network”)

local function networkListener(event)

if (event.isError) then

print(error)

else

print(event.response)

end

end

 

local params = {}

params.body = <dataParams>

network.request( <Link> , “POST”, networkListener, params)

I think the problem may because of the received packets. I tried to connect to another server and seems works fine.

Here is the updated code with the url that run with error:

display.setStatusBar( display.HiddenStatusBar )

 

network = require(“network”)

local function networkListener(event)

if (event.isError) then

print(error)

else

print(event.response)

end

end

 

local params = {}

params.body = “{}”

network.request("http://59.148.247.164:8050/meclsv1/APIMemberInfo", “POST”, networkListener, params)

First of all you don’t need to require “network”.  It’s built into the core.  Make sure you don’t have a network.lua in your folder.

Secondly, I don’t know what this web script is. Does it accept Post requests?  What are the post parameters it’s expecting?  What happens when you feed a string with the value {} to it?  Post usually wants key-value pairs passed in a string like:

params.body = “somekey=somevalue&someotherkey=someothervalue&yetanotherkey=anothervalue”.

This string must be URLencoded.

Perhaps something there isn’t behaving right which is why it’s erroring on you.

Rob

The “network” require here is the corona built-in module. I require “network” is because I’m not sure whether it needs require when the error occurred.

The server accept “json” format with “POST” method, so I use “{}” as a parameter.

By the way, As I said that I tried in iPhone and simulator, and both of them can get the information. And I use “CocoaRestClient” to test it and it returns “HTTP 200 No Error” with the following response:

{“memberinfo”:{“code”:“E11”,“remarks”:“appid: null”}}

The error it states seems because of java layer so I cannot debug. And I’m not familiar with http protocol. So I want to know what can makes the error.

P.S.: The code in the 4th post is the whole program that I just for testing and it also returns error. And here is the code under “settings” of “build.settings”

    android =

    {

        usesPermissions =

        {

            – Required by the MapView to fetch its contents from the Google Maps servers.

            “android.permission.INTERNET”,

 

            – Optional permission used to display current location via the GPS.

            “android.permission.ACCESS_FINE_LOCATION”,

 

            – Optional permission used to display current location via WiFi or cellular service.

            “android.permission.ACCESS_COARSE_LOCATION”,

        },

        usesFeatures =

        {

            – If you set permissions “ACCESS_FINE_LOCATION” and “ACCESS_COARSE_LOCATION” above,

            – then you may want to set up your app to not require location services as follows.

            – Otherwise, devices that do not have location sevices (such as a GPS) will be unable

            – to purchase this app in the app store.

            { name = “android.hardware.location”, required = false },

            { name = “android.hardware.location.gps”, required = false },

            { name = “android.hardware.location.network”, required = false },

        },

    }

Sorry, but what I can do in this situation?
I tried lots of method to test it, but only android device cannot get the information from server.

Now the code is updated like this, but this error still occur:

display.setStatusBar( display.HiddenStatusBar )

 

local function networkListener(event)

if (event.isError) then

print(error)

else

print(event.response)

end

end

 

local params = {}

params.header = {}

params.header[“Content-Type”] = “application/json”

params.body = “{}”

network.request("http://59.148.247.164:8050/meclsv1/APIMemberInfo", “POST”, networkListener, params)

I tried lots of method to test it, but only android device cannot get the information from server. iPhone and Simulator still knows fine.

I ran this on my Google Nexus 7 (4.4.4) and this was output:

I/Corona  (28471): {“memberinfo”:{“code”:“E11”,“remarks”:“appid: null”}}

That’s identical to what I got in the Simulator.  What device and OS are you trying to run this on?  You may need to file a bug report for this if it seems to be device specific. 

Rob

Thanks for your reply.

3 androids devices for testing are:

Toshiba Thrive tablet (4.0.4)

Samsung S II (2.3.6)

Xiaomi Redmi (4.3) (which is the brand from chinese)

After the further inspection, I suspect that the problem may comes from the parts of the checking the header field “Content-Type” of the response packet.

I searched from google that some of the "java.lang.NullPointerException"of “java.util.regex.Matcher” are come from passing the “nil” to  this class. And I found that the server response is missing this field. I asked server side to modify this to see if the problem is from here.

I’m not sure the problem is fixed in new JDK, but both of JDK 1.6 and 1.7 are not work.

Can you post your code that’s doing the network request and it’s listener function?

I just run the following code, but it returns error

 

 

display.setStatusBar( display.HiddenStatusBar )

 

network = require(“network”)

local function networkListener(event)

if (event.isError) then

print(error)

else

print(event.response)

end

end

 

local params = {}

params.body = <dataParams>

network.request( <Link> , “POST”, networkListener, params)

I think the problem may because of the received packets. I tried to connect to another server and seems works fine.

Here is the updated code with the url that run with error:

display.setStatusBar( display.HiddenStatusBar )

 

network = require(“network”)

local function networkListener(event)

if (event.isError) then

print(error)

else

print(event.response)

end

end

 

local params = {}

params.body = “{}”

network.request("http://59.148.247.164:8050/meclsv1/APIMemberInfo", “POST”, networkListener, params)

First of all you don’t need to require “network”.  It’s built into the core.  Make sure you don’t have a network.lua in your folder.

Secondly, I don’t know what this web script is. Does it accept Post requests?  What are the post parameters it’s expecting?  What happens when you feed a string with the value {} to it?  Post usually wants key-value pairs passed in a string like:

params.body = “somekey=somevalue&someotherkey=someothervalue&yetanotherkey=anothervalue”.

This string must be URLencoded.

Perhaps something there isn’t behaving right which is why it’s erroring on you.

Rob

The “network” require here is the corona built-in module. I require “network” is because I’m not sure whether it needs require when the error occurred.

The server accept “json” format with “POST” method, so I use “{}” as a parameter.

By the way, As I said that I tried in iPhone and simulator, and both of them can get the information. And I use “CocoaRestClient” to test it and it returns “HTTP 200 No Error” with the following response:

{“memberinfo”:{“code”:“E11”,“remarks”:“appid: null”}}

The error it states seems because of java layer so I cannot debug. And I’m not familiar with http protocol. So I want to know what can makes the error.

P.S.: The code in the 4th post is the whole program that I just for testing and it also returns error. And here is the code under “settings” of “build.settings”

    android =

    {

        usesPermissions =

        {

            – Required by the MapView to fetch its contents from the Google Maps servers.

            “android.permission.INTERNET”,

 

            – Optional permission used to display current location via the GPS.

            “android.permission.ACCESS_FINE_LOCATION”,

 

            – Optional permission used to display current location via WiFi or cellular service.

            “android.permission.ACCESS_COARSE_LOCATION”,

        },

        usesFeatures =

        {

            – If you set permissions “ACCESS_FINE_LOCATION” and “ACCESS_COARSE_LOCATION” above,

            – then you may want to set up your app to not require location services as follows.

            – Otherwise, devices that do not have location sevices (such as a GPS) will be unable

            – to purchase this app in the app store.

            { name = “android.hardware.location”, required = false },

            { name = “android.hardware.location.gps”, required = false },

            { name = “android.hardware.location.network”, required = false },

        },

    }

Sorry, but what I can do in this situation?
I tried lots of method to test it, but only android device cannot get the information from server.

Now the code is updated like this, but this error still occur:

display.setStatusBar( display.HiddenStatusBar )

 

local function networkListener(event)

if (event.isError) then

print(error)

else

print(event.response)

end

end

 

local params = {}

params.header = {}

params.header[“Content-Type”] = “application/json”

params.body = “{}”

network.request("http://59.148.247.164:8050/meclsv1/APIMemberInfo", “POST”, networkListener, params)

I tried lots of method to test it, but only android device cannot get the information from server. iPhone and Simulator still knows fine.

I ran this on my Google Nexus 7 (4.4.4) and this was output:

I/Corona  (28471): {“memberinfo”:{“code”:“E11”,“remarks”:“appid: null”}}

That’s identical to what I got in the Simulator.  What device and OS are you trying to run this on?  You may need to file a bug report for this if it seems to be device specific. 

Rob

Thanks for your reply.

3 androids devices for testing are:

Toshiba Thrive tablet (4.0.4)

Samsung S II (2.3.6)

Xiaomi Redmi (4.3) (which is the brand from chinese)

After the further inspection, I suspect that the problem may comes from the parts of the checking the header field “Content-Type” of the response packet.

I searched from google that some of the "java.lang.NullPointerException"of “java.util.regex.Matcher” are come from passing the “nil” to  this class. And I found that the server response is missing this field. I asked server side to modify this to see if the problem is from here.

I’m not sure the problem is fixed in new JDK, but both of JDK 1.6 and 1.7 are not work.