How to done with AsyncTask before doing another code
<p>(Sorry for bad English).. I have an Asynctask that takes a data and return a String.. But the data return after the second Log comes out..</p>

<p><strong>This is my code</strong></p>

<pre><code>if  (session.getSpMainKey() == null || Objects.equals(session.getSpMainKey(), "")){
        Log.w("null", "yes");
        g.execute(session.getId());
        Log.w("maiKey", "yes");
    }
try {
JSONObject j = new JSONObject(session.getSpMainKey());
} catch (Exception e) { e.printStackTrace(); }
</code></pre>

<p><strong>This is the Log:</strong></p>

<pre><code>08-21 18:58:59.583 30375-30375/ampersanda.hmmmm W/null: yes
08-21 18:58:59.583 30375-30375/ampersanda.hmmmm W/id -&gt;: 561
08-21 18:58:59.621 30375-30375/ampersanda.hmmmm W/maiKey: yes
08-21 18:58:59.630 30375-574/ampersanda.hmmmm W/param sent -&gt;: id=561
</code></pre>

<p>I thought that <strong>param sent ->: id=561</strong> should before the <strong>W/maiKey: yes</strong></p>

<p>And after that I got <strong>java.lang.NullPointerException</strong> saying that I have a null object reference when I am trying to convert String to JSONObject</p>

<p>This is the exception log</p>

<pre><code>08-21 18:54:11.369 28295-28295/ampersanda.hmmmm W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
08-21 18:54:11.373 28295-28295/ampersanda.hmmmm W/System.err:     at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
08-21 18:54:11.373 28295-28295/ampersanda.hmmmm W/System.err:     at org.json.JSONTokener.nextValue(JSONTokener.java:94)
08-21 18:54:11.373 28295-28295/ampersanda.hmmmm W/System.err:     at org.json.JSONObject.&lt;init&gt;(JSONObject.java:156)
08-21 18:54:11.373 28295-28295/ampersanda.hmmmm W/System.err:     at org.json.JSONObject.&lt;init&gt;(JSONObject.java:173)
08-21 18:54:11.373 28295-28295/ampersanda.hmmmm W/System.err:     at ampersanda.elsys.MainActivity.onCreate(MainActivity.java:66)
08-21 18:54:11.373 28295-28295/ampersanda.hmmmm W/System.err:     at android.app.Activity.performCreate(Activity.java:5976)
08-21 18:54:11.373 28295-28295/ampersanda.hmmmm W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
08-21 18:54:11.373 28295-28295/ampersanda.hmmmm W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
08-21 18:54:11.373 28295-28295/ampersanda.hmmmm W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
08-21 18:54:11.373 28295-28295/ampersanda.hmmmm W/System.err:     at android.app.ActivityThread.access$800(ActivityThread.java:144)
08-21 18:54:11.373 28295-28295/ampersanda.hmmmm W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
08-21 18:54:11.373 28295-28295/ampersanda.hmmmm W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
08-21 18:54:11.373 28295-28295/ampersanda.hmmmm W/System.err:     at android.os.Looper.loop(Looper.java:135)
08-21 18:54:11.373 28295-28295/ampersanda.hmmmm W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5221)
08-21 18:54:11.373 28295-28295/ampersanda.hmmmm W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
08-21 18:54:11.373 28295-28295/ampersanda.hmmmm W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
08-21 18:54:11.373 28295-28295/ampersanda.hmmmm W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
08-21 18:54:11.373 28295-28295/ampersanda.hmmmm W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
</code></pre>