0

I have an AsyncTask for receiving data from the database, here is my full code:

line26

 public class Tabasync2 extends AsyncTask<String, String, String> {
    static Posdent tempposdent;
    ...
    ArrayList<Posdent> aResults = new ArrayList<Posdent>();

public static Posdent send() {
    return tempposdent;
}

@Override
protected void onPreExecute() {
  ...}

@Override
protected String doInBackground(String... sText) {
   ...
   ...
        Posdent P = getList(result);
   ...
    return result;
}
protected void onPostExecute(String v) {
    super.onPostExecute(result);
   Tabs t=new Tabs();

line:99

    t.calling(aResults);
}

public Posdent getList(String name) {
    tempposdent = new Posdent();
    String name1 = null;
    try {
        JSONArray Jarray = new JSONArray(result);

        int k;
        for (k = 0; k < Jarray.length(); k++) {
            try {
                JSONObject Jasonobject = null;
                Jasonobject = Jarray.getJSONObject(k);
                String msg = Jasonobject.getString("message");

                name1 = Jasonobject.getString("s_name");
                Tabs t1=new Tabs();
                t1.testing(name1);
                tempposdent = new Posdent();

                tempposdent.setStudent_name(name1);
                tempposdent.setMessage(msg);
                for(int j=0;j<aResults.size();j++)
                {
                    if(aResults.get(j).hashCode()!=(tempposdent.hashCode())){
                        aResults.add(tempposdent);
                    }
                }
            } catch (JSONException js) {
                js.printStackTrace();
            }
        }
    } catch (Exception e) {
        Log.e("log_tag", "Error parsing data" + e.toString());

    }
    Tabs t=new Tabs();

    t.testing(name1);
    return tempposdent;
}
}

this is my new logcat trace:

  java.lang.NullPointerException
        at android.view.LayoutInflater.from(LayoutInflater.java:211)
        at com.student.anurag.student_connect.Tabs$SearchResultAdapter.<init>(Tabs.java:144)
        at com.student.anurag.student_connect.Tabs.calling(Tabs.java:115)
        at com.student.anurag.student_connect.Tabasync2.onPostExecute(Tabasync2.java:99)
        at com.student.anurag.student_connect.Tabasync2.onPostExecute(Tabasync2.java:26)
        at android.os.AsyncTask.finish(AsyncTask.java:632)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5086)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

Also i would like to include that from the main activity when i m pressing a button two things are happening:

  • first AsyncTask posts data
  • other AsyncTask retrieves data; like this:

    switch(v.getId()) {
    
           case R.id.postb:
    
           new task1().execute();
    
           new Tabasync2().execute(ep.getText().toString());
    
           break;
    
           }
    

here is the tabs class snippet:

line 115

public void calling(ArrayList<Posdent> aResults) {
    searchResults.setAdapter(new SearchResultAdapter(context, aResults));
}

 class SearchResultAdapter extends BaseAdapter {

    int count;
    Typeface type;
    Context context;
    private LayoutInflater layoutInflater;
    private ArrayList<Posdent> sdetails = new ArrayList<Posdent>();

    public SearchResultAdapter(Context context, ArrayList<Posdent> aResults) {

line 144

        layoutInflater = LayoutInflater.from(this.context);
        this.sdetails = aResults;
        this.count = aResults.size();
        this.context = context;
        type = Typeface.createFromAsset(context.getAssets(), "fonts/book.TTF");
        Log.d("Inside SearchResultAdapter", "" + count);
    }
ANURAG GUPTA
  • 173
  • 1
  • 13

1 Answers1

0

this.context is assigned after the initialization of the layoutInflater. Change from

  layoutInflater = LayoutInflater.from(this.context);

to

  layoutInflater = LayoutInflater.from(context);
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • thanks for reply but it didnot solved the problem. The same error is obtained again. – ANURAG GUPTA Apr 27 '15 at 13:47
  • then it means that your are passing null in the first place to the adapter. in which context is called this line `searchResults.setAdapter(new SearchResultAdapter(context, aResults));` ? – Blackbelt Apr 27 '15 at 13:49
  • In the Tabs class which is an activity calling that asynctask class – ANURAG GUPTA Apr 27 '15 at 13:52
  • then use `new SearchResultAdapter(Tabs.this, aResults)` – Blackbelt Apr 27 '15 at 13:57
  • nope, same error again..is it possible that the calling function(line:99) is passing null – ANURAG GUPTA Apr 27 '15 at 14:01
  • What IDE are you using? Why not start up the debugger and inspect the variable values to know for sure what is stored where? – Zerp Apr 27 '15 at 14:06
  • yes it is possible, the way you wrote your post it is impossible, at least to me, to understand what is going on – Blackbelt Apr 27 '15 at 14:19
  • can you send me your email id so that i can send you the both of the java files? @Blackbelt – ANURAG GUPTA Apr 27 '15 at 14:25
  • @Blackbelt i have created the gist here: [link](https://gist.github.com/Anurag690/4b83c59b312e9168b694) please help, thank you – ANURAG GUPTA Apr 27 '15 at 15:20
  • you can't use the new operator on a class that extends Activity. `Tabs t=new Tabs(); t.calling(aResults);`. Try using a delegate. Have a look [here](http://stackoverflow.com/questions/16752073/how-do-i-return-a-boolean-from-asynctask) – Blackbelt Apr 27 '15 at 15:27
  • @Blackbelt sorry i am not able to do that..again the same error is coming..i added this in Tabs.java public interface MyInterface{ public void calling(ArrayList arrayList); }created an interface as private member of Tabasync2 and called through that, if i m wrong please show me how to do it, if correct please help solving it.. – ANURAG GUPTA Apr 27 '15 at 16:34
  • Update the gist with the changes and post the link again – Blackbelt Apr 27 '15 at 16:43
  • 1
    @Blackbelt please check that there is no data going into aResults. the log.d() statement in the SearchResultAdapter prints the size of aResults as 0. – ANURAG GUPTA Apr 27 '15 at 17:54
  • At least is not crashing anymore – Blackbelt Apr 27 '15 at 18:15
  • @Blackbelt yes it is not crashing but the thing for which i added if its not working then what is the use..please sir help me. – ANURAG GUPTA Apr 27 '15 at 18:44
  • the for loop `for(int j=0;j – Blackbelt Apr 27 '15 at 19:21