-1

I have a recyclerviewholder that contains edittext fields.

In the edittext fields, I'm trying to update a specific SQLite values using TextChangedListener so that it will automatically update SQLite values without pressing any button. But it throws a NullPointerException

My OnCreateView

SQLiteCBLCAdapter sqliteCBLCAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_tab_cart, container, false);
    ButterKnife.setDebug(true);
    ButterKnife.bind(this, view);
    sqliteCBLCAdapter = new SQLiteCBLCAdapter(getContext());

    return view;
}

This is my onTextChangedListener method

  @Override
  public void onTextChanged(CharSequence s, int start, int before, int count) {
      try {
          if (!s.equals("")) {
              String newQuantity = s.toString();
              sqliteCBLCAdapter.updateQuantity(orderName, newQuantity);

          } else {
              // irrelevant to the question
          }
      } catch (NumberFormatException e) {
          holder.tv_currentPrice_atc.setText("0");
      }
  }

And this is my updateQuantity method.

  public void updateQuantity(String orderName, String newQuantity) {
      SQLiteDatabase sqLiteDatabase = sqLiteCBLC.getWritableDatabase();
      String updateQuery = "UPDATE "+SQLiteCBLC.TABLE_NAME+" SET "+SQLiteCBLC.COL_QUANTITY+" = "+newQuantity+" WHERE "+SQLiteCBLC.COL_ORDNAME+" = '"+orderName+"'";
      sqLiteDatabase.execSQL(updateQuery);
  }

This is my logs

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.steven.frio.systemanalysisanddesign.sqlite.SQLiteCBLCAdapter.updateQuantity(java.lang.String, java.lang.String)' on a null object reference
                                                                                         at com.steven.frio.systemanalysisanddesign.FragmentTabCart$2.onTextChanged(FragmentTabCart.java:204)
                                                                                         at android.widget.TextView.sendOnTextChanged(TextView.java:8189)
                                                                                         at android.widget.TextView.setText(TextView.java:4485)
                                                                                         at android.widget.TextView.setText(TextView.java:4339)
                                                                                         at android.widget.EditText.setText(EditText.java:89)
                                                                                         at android.widget.TextView.setText(TextView.java:4314)
                                                                                         at com.steven.frio.systemanalysisanddesign.FragmentTabCart$1.addButtonClick(FragmentTabCart.java:138)
                                                                                         at com.steven.frio.systemanalysisanddesign.recycleradapters.RAdapterAddToCart$1.onClick(RAdapterAddToCart.java:80)
                                                                                         at android.view.View.performClick(View.java:5620)
                                                                                         at android.view.View$PerformClick.run(View.java:22300)
                                                                                         at android.os.Handler.handleCallback(Handler.java:754)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                         at android.os.Looper.loop(Looper.java:160)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6237)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

When initializing sqliteCBLCAdapter inside the textchangedlistener/ parent method of the listener it throws this exception

Process: com.steven.frio.systemanalysisanddesign, PID: 22921
                                                                                     java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
                                                                                         at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
                                                                                         at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                                         at com.steven.frio.systemanalysisanddesign.sqlite.SQLiteCBLCAdapter$SQLiteCBLC.<init>(SQLiteCBLCAdapter.java:115)
                                                                                         at com.steven.frio.systemanalysisanddesign.sqlite.SQLiteCBLCAdapter.<init>(SQLiteCBLCAdapter.java:21)
                                                                                         at com.steven.frio.systemanalysisanddesign.FragmentTabCart.editTextWatchers(FragmentTabCart.java:189)
                                                                                         at com.steven.frio.systemanalysisanddesign.recycleradapters.RAdapterAddToCart.onBindViewHolder(RAdapterAddToCart.java:75)
                                                                                         at com.steven.frio.systemanalysisanddesign.recycleradapters.RAdapterAddToCart.onBindViewHolder(RAdapterAddToCart.java:30)
                                                                                         at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6356)
forestvine
  • 537
  • 6
  • 12
  • `sqliteCBLCAdapter` is not initialized. – SripadRaj Aug 22 '17 at 10:04
  • may be orderName or newQuantity passing null value.. please attach debug or print log and check what value going to update – Gundu Bandgar Aug 22 '17 at 10:17
  • @GunduBandagar I created a log that will print the value, it successfully captures the value but when the method sqliteCBLCAdapter.updateQuantity(orderName, newQuantity); is going to be called it throws null pointer exception, eventhough it is properly instantiated – forestvine Aug 22 '17 at 10:30

1 Answers1

0

Make sure you are initializing your database correctly.

SQLiteCBLCAdapter  sqliteCBLCAdapter = new SQLiteCBLCAdapter (context); 
Milad Moosavi
  • 1,587
  • 10
  • 20