0

I'm trying to create a Dream list app. It has an Sqlite database and a recyclerview to display the information, the mainAcitivy should have only the recyclerview to display the items and one button that starts another activity to actually fill in the details for the dream. My problem is how can I use the recyclerview adapter from another acitivity to populate the data. I'm getting this error please help :

*Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
 at com.example.ahmad.dreams.DreamWritingActivity.onCreate(DreamWritingActivity.java:34)**

MainActivity

Button addNewDreamButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    addNewDreamButton = (Button)findViewById(R.id.button_id);
    addNewDreamButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this, DreamWritingActivity.class);
            startActivity(intent);

        }
    });
}

}

DreamWritingActivity

 EditText nameEditText;
EditText dreamEditText;
EditText dateEditText;
Button   saveButton;
RecyclerView myRecyclerView;
DreamListAdapter dreamListAdapter;
SQLiteDatabase sqLiteDatabase;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dream_writing);
    myRecyclerView = (RecyclerView)findViewById(R.id.recyclerview_id);

    //setting a layout manager for our recyclerview linearlayout
    myRecyclerView.setLayoutManager( new LinearLayoutManager(this));
    //getting the database helper to get a writable database
    DreamHelper dreamHelper = new DreamHelper(this);
    //getting a writable database
    sqLiteDatabase = dreamHelper.getWritableDatabase();
    //query the database by a cursor to give to the adapter
    Cursor cursor = getAllInfoCursor();

    dreamListAdapter = new DreamListAdapter(this,cursor);

    myRecyclerView.setAdapter(dreamListAdapter);

}
public void addtodreams(View view){
    if(nameEditText.getText().length() == 0 || dreamEditText.getText().length() == 0 || dateEditText.getText().length() == 0) {
        return;
    }
    String name = nameEditText.getText().toString();
    String date = dateEditText.getText().toString();
    String dream = dreamEditText.getText().toString();

    addToDatabase(name,date,dream);

    dreamListAdapter.swapCursor(getAllInfoCursor());

    dreamEditText.clearFocus();
    dreamEditText.getText().clear();
    nameEditText.getText().clear();
    dateEditText.getText().clear();
}


public long addToDatabase(String name , String date, String dream){
    ContentValues contentValues = new ContentValues();
    contentValues.put(name, DreamDatabase.DreamEntry.COLUMN_DREAM_NAME);
    contentValues.put(date, DreamDatabase.DreamEntry.COLUMN_DREAM_DATE);
    contentValues.put(dream, DreamDatabase.DreamEntry.COLUMN_DREAM_EXPLANATION);
    return sqLiteDatabase.insert(DreamDatabase.DreamEntry.TABLE_NAME,null,contentValues);
}

public Cursor getAllInfoCursor(){
    return sqLiteDatabase.query(DreamDatabase.DreamEntry.TABLE_NAME,null,null,null,null,null, DreamDatabase.DreamEntry.COLUMN_DREAM_NAME);
}

}

DreamListAdapter

  Context mContext;
Cursor mCursor;


public DreamListAdapter(Context context, Cursor cursor) {
    this.mContext = context;
    this.mCursor = cursor;
}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(mContext);
    View view = layoutInflater.inflate(R.layout.recyclerview_item,parent,false);
    return  new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    if (!mCursor.moveToPosition(position))
        return;

    String name = mCursor.getString(mCursor.getColumnIndex(DreamDatabase.DreamEntry.COLUMN_DREAM_NAME));
    //retrieve it as a string for now if nothing else is better
    String date = mCursor.getString(mCursor.getColumnIndex(DreamDatabase.DreamEntry.COLUMN_DREAM_DATE));
    String dream = mCursor.getString(mCursor.getColumnIndex(DreamDatabase.DreamEntry.COLUMN_DREAM_EXPLANATION));
    long id = mCursor.getLong(mCursor.getColumnIndex(DreamDatabase.DreamEntry._ID));

    holder.nameTextView.setText(name);
    //Set as text change when you make it date type
    holder.dateTextView.setText(date);
    holder.explanationTextView.setText(dream);
    holder.itemView.setTag(id);
}

@Override
public int getItemCount() {
  return   mCursor.getCount();
}

public void swapCursor(Cursor newCursor){
    if (mCursor != null) mCursor.close();
    mCursor = newCursor;
    if (newCursor != null) {
        // Force the RecyclerView to refresh
        this.notifyDataSetChanged();
    }
}


public static class ViewHolder extends RecyclerView.ViewHolder{
    public TextView nameTextView;
    public TextView dateTextView;
    public TextView explanationTextView;

    public ViewHolder(View itemView) {
        super(itemView);
        nameTextView = (TextView) itemView.findViewById(R.id.dream_name);
        dateTextView = (TextView) itemView.findViewById(R.id.date_id);
        explanationTextView = (TextView) itemView.findViewById(R.id.dream_id);

    }
}

}

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Akay
  • 53
  • 2
  • 9
  • 7
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) –  Sep 15 '17 at 11:39
  • Your `Activity`'s layout **activity_dream_writing** has no RecyclerView with id **recyclerview_id**. Check if you have it in some other layout. – Abbas Sep 15 '17 at 13:09
  • Yes i'm aware of that. my bad i should have put the layout code as well. but anyways i fixed it, i was trying to do something stupid i guess which is defining a Recyclerview in one activity's layout and populating it in another which made no sense, but now i'm passing the data by intent putExtra. thanks for the reply – Akay Sep 16 '17 at 10:20

0 Answers0