0

I try to write a library application. I works with php and MySql.

In my mainACtivity, I call function that return 3 lists: books, cart and orders. The first is full, but cart and orders are empty. Theses functions are called in myAsyncTask.

  new AsyncTask<Void, Void,  ArrayList<Book>>() {
        @Override
        protected void onPreExecute() {
        }

        @Override
        protected  ArrayList<Book> doInBackground(Void... voids) {
            listBook = backend.getBookInStock();
            billForMember = backend.getBillForMember(member.getMemberID());
            cart = backend.getCart(member.getMemberID());
            return listBook;
        }

        @Override
        protected void onPostExecute( ArrayList<Book> list) {
            searchView = (SearchView) findViewById(R.id.searchView);
            listView = (ListView) findViewById(R.id.listView);
            bookAdapter = new BookAdapter(CrownLibrary.this, listBook, AccessType.DIRECTOR, SearchType.BOOK_IN_STOCK, 0);
            bookAdapter.addListener(CrownLibrary.this);
            listView.setAdapter(bookAdapter);
        }
    }.execute();


       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent = new Intent(CrownLibrary.this, CrownLibraryDetails.class);
            intent.putExtra(IntentExtra.BOOK.toString(), bookAdapter.getItem(i));
            intent.putExtra(IntentExtra.MEMBER.toString(), member);
            startActivity(intent);
        }
    });

When I debug my application, the function calls theses 3 functions, and initializes them well. But just after doInBackground, the application doesn't effectuate onPOstExecute. I know this, because the listView (whose is in onPostExecute) is never initialized and the application stops.

 01-17 17:28:25.956 8002-8002/com.example.lilo.ebookstore E/AndroidRuntime:       Caused by: java.lang.NullPointerException
 01-17 17:28:25.956 8002-8002/com.example.lilo.ebookstore E/AndroidRuntime:     at com.example.lilo.ebookstore.CrownLibrary.onCreate(CrownLibrary.java:94)

I don't have any idea.

Liloo
  • 13
  • 2
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Bö macht Blau Jan 17 '16 at 17:53
  • Can you post onCreate() method code. there is an exception NPE in that method – Pankaj Jan 17 '16 at 17:54

1 Answers1

0

you need to assign the views outside the AsyncTask. onPostExecute is called after do in background is done on a different thread. Before that the main thread already reaches the line where you assign the OnItemClickListener. Just move the lines

searchView = (SearchView) findViewById(R.id.searchView);
listView = (ListView) findViewById(R.id.listView);

outside the AsyncTask.

Mo1989
  • 2,374
  • 1
  • 15
  • 17