BRDroid
BRDroid

Reputation: 4388

null pointer exception using KSOAP

I am sending JSON data to a webservice using KSOAP and AsyncTask. In some cases I get a NullPointerException error, when I did the code debug, as a response from service I get a NULL value. In onPostExecute of AsyncTask I am trying to catch this NULL value but could not do so. I used if(result.equals(null)) and if(result.equals("null")) but doesnt seem to work. Along with nullPointerException the LogCat shows me other warnings on SSL, connection timed out. I could not figure out what is causing the issue, if you could point me in the right direction of where I am going wrong that would be of great help.

AsyncTask class

public class IncidentDataToServer extends AsyncTask<JSONObject, Void, String>{
        @Override
        protected String doInBackground(JSONObject... params) {
            incidentJSON = params[0];
            ServerAuthentication sa = new ServerAuthentication();
            String res = sa.incidentDataToServer(incidentJSON);         
            return res;
        }

        @Override
        protected void onPostExecute(String result) {
           //This is where I am trying to catch the null value.
           //tried using result.equals("null") but does not get into the if statement
            if(result.equals(null)){
    //if error from service write the data to local DB                  
                DBStaffList dbObj = new DBStaffList(IncidentsActivity.this);
                dbObj.open();
                dbObj.createIncidentDataJSONEntry( loggedInUserId, incidentJSON.toString());
                dbObj.close();
            }                    
                DBStaffList dbObj = new DBStaffList(IncidentsActivity.this);
                dbObj.open();
                String JsonDatafromDB = dbObj.getIncidentJSONData().toString();             
                dbObj.getIncidentJSONData().size(); 
   //check if there is data in local DB to be sent to service           
            if(dbObj.getIncidentJSONData().size() >0){                   
                    IncidentDataToServer incidentToServerObj = new IncidentDataToServer();
                    try {
                        jsonIncidentObject = new JSONObject(dbObj.getIncidentJSONData().get(0));

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }                   dbObj.deleteIncidentRow(dbObj.getIncidentRowIds().get(0));
                    incidentToServerObj.execute(jsonIncidentObject);

            }               
                dbObj.close();      
            super.onPostExecute(result);
        }       
    }

Method which is sending data to service

public String incidentDataToServer(JSONObject incidentJSON){
        // package the request
        SoapObject Request = new SoapObject(INCIDENT_NAMESPACE, INCIDENT_METHOD_NAME);
        String incidentData = incidentJSON.toString();
        PropertyInfo pi = new PropertyInfo();
        pi.setName("IncidentJSonString");
        pi.setValue(incidentData);
        pi.setType(String.class);
        Request.addProperty(pi);    
        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(Request);      
        HttpTransportSE aht = new HttpTransportSE(INCIDENT_URL);
        try {           
            aht.call(INCIDENT_SOAP_ACTION, soapEnvelope);
            SoapPrimitive resultString = (SoapPrimitive) soapEnvelope
             .getResponse();
              resIncident = resultString.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resIncident;

    }

error from LogCat

03-17 19:15:36.758: W/System.err(8237): javax.net.ssl.SSLException: Write error: ssl=0x4e0bfc28: I/O error during system call, Connection timed out
03-17 19:15:36.798: W/System.err(8237):     at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_write(Native Method)
03-17 19:15:36.798: W/System.err(8237):     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:693)
03-17 19:15:36.808: W/System.err(8237):     at java.io.BufferedOutputStream.flushInternal(BufferedOutputStream.java:185)
03-17 19:15:36.808: W/System.err(8237):     at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:85)
03-17 19:15:36.808: W/System.err(8237):     at libcore.net.http.FixedLengthOutputStream.flush(FixedLengthOutputStream.java:49)
03-17 19:15:36.808: W/System.err(8237):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:188)
03-17 19:15:36.818: W/System.err(8237):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)
03-17 19:15:36.818: W/System.err(8237):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113)
03-17 19:15:36.818: W/System.err(8237):     at com.dimensions.dimensionsapp.ServerAuthentication.incidentDataToServer(ServerAuthentication.java:130)
03-17 19:15:36.818: W/System.err(8237):     at com.dimensions.dimensionsapp.IncidentsActivity$IncidentDataToServer.doInBackground(IncidentsActivity.java:3207)
03-17 19:15:36.818: W/System.err(8237):     at com.dimensions.dimensionsapp.IncidentsActivity$IncidentDataToServer.doInBackground(IncidentsActivity.java:1)
03-17 19:15:36.818: W/System.err(8237):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-17 19:15:36.818: W/System.err(8237):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-17 19:15:36.818: W/System.err(8237):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-17 19:15:36.838: W/System.err(8237):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-17 19:15:36.838: W/System.err(8237):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-17 19:15:36.838: W/System.err(8237):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-17 19:15:36.838: W/System.err(8237):     at java.lang.Thread.run(Thread.java:856)
03-17 19:15:36.838: D/AndroidRuntime(8237): Shutting down VM
03-17 19:15:36.838: W/dalvikvm(8237): threadid=1: thread exiting with uncaught exception (group=0x40dd52a0)
03-17 19:15:36.838: E/AndroidRuntime(8237): FATAL EXCEPTION: main
03-17 19:15:36.838: E/AndroidRuntime(8237): java.lang.NullPointerException
03-17 19:15:36.838: E/AndroidRuntime(8237):     at com.dimensions.dimensionsapp.IncidentsActivity$IncidentDataToServer.onPostExecute(IncidentsActivity.java:3215)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at com.dimensions.dimensionsapp.IncidentsActivity$IncidentDataToServer.onPostExecute(IncidentsActivity.java:1)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at android.os.AsyncTask.finish(AsyncTask.java:631)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at android.os.Looper.loop(Looper.java:137)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at android.app.ActivityThread.main(ActivityThread.java:4944)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at java.lang.reflect.Method.invokeNative(Native Method)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at java.lang.reflect.Method.invoke(Method.java:511)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
03-17 19:15:36.838: E/AndroidRuntime(8237):     at dalvik.system.NativeStart.main(Native Method)

Help/Suggestions much appreciated. Thank you

Upvotes: 0

Views: 226

Answers (2)

codeslapper
codeslapper

Reputation: 297

Here you go bro, System.err(8237): at com.dimensions.dimensionsapp.ServerAuthentication.incidentDataToServer(ServerAuthentication.java:130)

Look at that line. Probably in your doInBg. Put a try{}catch{} around all the code in your doinbg and then return a null or otherwise handle the exception.

Upvotes: 0

donfuxx
donfuxx

Reputation: 11321

to check if result is null you should use:

if(result == null)

instead of:

if(result.equals(null))

The reason is because for the case result is null then you cannot call any method on it (including equals) if you still do so then NPE would be thrown.

See also related question: Java null check why use == instead of .equals()

Upvotes: 1

Related Questions