java.lang.NullPointerException when invoking onLoadFinished() [duplicate]

Internet of Things IoT Frameworks 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_x000D_ _x000D_ _x000D_ This question already has an answer here:_x000D_ _x000D_ _x000D_ What is a NullPointerException, and how do I fix it?_x000D_ _x000D_ 12 answers_x000D_ _x000D_ _x000D_ _x000D_ NullPointerException in Java when using an object as a field_x000D_ _x000D_ 3 answers_x000D_ _x000D_ _x000D_ _x000D_ _x000D_ public class CatalogActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks { private static final int PRODUCT_LOADER = 0; ProductCursorAdapter mCursorAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_catalog); ListView productsListView = (ListView) findViewById(R.id.list); View emptyView = findViewById(R.id.empty_view); productsListView.setEmptyView(emptyView); ProductCursorAdapter mCursorAdapter = new ProductCursorAdapter(this, null); productsListView.setAdapter(mCursorAdapter); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(CatalogActivity.this, AddProductActivity.class); startActivity(intent); } }); productsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView adapterView, View view, int position, long id) { Intent intent = new Intent(CatalogActivity.this, DetailEditActivity.class); Uri currentProductUri = ContentUris.withAppendedId(ProductContract.ProductEntry.CONTENT_URI, id); intent.setData(currentProductUri); startActivity(intent); } }); Log.e("working fine" , "product"); getSupportLoaderManager().initLoader(PRODUCT_LOADER, null, this); } And the onLoadFinished() method is : public void onLoadFinished(Loader loader, Cursor data) { mCursorAdapter.swapCursor(data); } I keep receiving the error as following : java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.example.android.gloryinventory.ProductCursorAdapter.swapCursor(android.database.Cursor)' on a null object reference Is there's something wrong with my code? Thanks in advance.

Posted on 16 Aug 2022, this text provides information on IoT Frameworks related to Internet of Things. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ Because you are creating ProductCursorAdapter mCursorAdapter within the context of the class but initializing another local variable instead of that one in your onCreate. Here is the problematic line: ProductCursorAdapter mCursorAdapter = new ProductCursorAdapter(this, null); which should be: mCursorAdapter = new ProductCursorAdapter(this, null);

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Internet of Things Links