-1

In our app, the list of diaries should contains a diary picture followed by diary name, for which we are using custom class and custom adapter. But the app crashes.

//This is Word class (custom class)

public class Word
{
String dname;

public Word()
{
    //default constructor
}

public Word(String dname) {
    this.dname=dname;
}

/*public void setDiaryName(String dname)
{
    this.dname=dname;
}*/

public String getDiaryName()
{
    return dname;
}
}



//the custom adapter

public class ContentAdapter extends ArrayAdapter<Word>
{
    Display d;
    public ContentAdapter(Activity context, ArrayList<Word>title)

{
    super(context, 0, title);
}

public View getView(int position, View convertView, ViewGroup parent) {

    // Check if the existing view is being reused otherwise inflate the view

    View listItemView = convertView;

    if (listItemView == null) {

        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.contents, parent, false);

    }

    Word w = getItem(position);

    TextView mt = (TextView) listItemView.findViewById(R.id.exp);

    mt.setText(w.getDiaryName());

    mt.setVisibility(View.VISIBLE);

    return listItemView;
}
}




//the fragment in which list of diaries is displayed
public class HistoryFragment extends Fragment 
{
    Word di;
    ListView lv;
    DatabaseReference reference;
    FirebaseAuth mAuth = FirebaseAuth.getInstance();
    Activity context;
    public HistoryFragment(){
    //default constructor
    }
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
    final View view = inflater.inflate(R.layout.scrolllist, container, false);

    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference uidRef = rootRef.child("image").child(uid);
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //List<String> cities = new ArrayList<>();
            final ArrayList<Word> cities = new ArrayList<>();
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String cityName = ds.getKey();
                cities.add(new Word(cityName));
            }
            ListView listView = (ListView)view.findViewById(R.id.list_of_ds);
            ContentAdapter arrayAdapter = new ContentAdapter(context,cities);
            listView.setAdapter(arrayAdapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            //Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
        }
    };
    uidRef.addListenerForSingleValueEvent(valueEventListener);
    return view;
}

}

When the app is run, on selecting history fragment, the following error occurs - java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

    //scrolllist.xml (for listview)    
    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/drawable_gradient">
<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/list_of_ds"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</ListView>


<Button
    android:id="@+id/add_images"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add Images"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"/>

<Button
    android:id="@+id/new_diary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Diary?"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"/>

</RelativeLayout>





//contents.xml (the custom view of diary pic and diary name)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="88dp">



    <ImageView

        android:id="@+id/image_of_user"

        android:layout_width="88dp"

        android:layout_height="88dp"

        android:src="@drawable/diarypic"
        android:layout_marginTop="20dp"/>



    <RelativeLayout

        android:id="@+id/text_container"

        android:layout_width="match_parent"

        android:layout_height="88dp"

        android:layout_alignParentBottom="true"

        android:layout_alignParentRight="true"

        android:layout_alignParentTop="true"

        android:layout_toRightOf="@id/image_of_user"

        android:orientation="vertical"

        android:paddingLeft="16dp"

        android:layout_marginTop="20dp">



        <TextView

            android:id="@+id/exp"

            android:layout_width="match_parent"

            android:layout_height="44dp"

            android:layout_weight="1"

            android:gravity="bottom"

            android:textAppearance="?android:textAppearanceMedium"

            android:textColor="@android:color/background_dark"

            android:textStyle="bold"

            android:text="lutti" />

    </RelativeLayout>
</RelativeLayout>
</RelativeLayout>
sree
  • 31
  • 5
  • If the app crashes, there is a stack trace. Please look that up on logcat, and add it to your question. Please also add the code that produces that error. – Alex Mamo Jun 24 '19 at 14:48
  • Sorry, but you neither have provided your code, nor the error message nor have you even asked a question. Please update / edit your question. – deHaar Jun 24 '19 at 14:50
  • yeah. I'm adding the code – sree Jun 24 '19 at 14:51

1 Answers1

0

Your activity context is null.You haven't initialized it.

public class Word
{
String dname;

public Word()
{
    //default constructor
}

public Word(String dname) {
    this.dname=dname;
}

/*public void setDiaryName(String dname)
{
    this.dname=dname;
}*/

public String getDiaryName()
{
    return dname;
}
}



//the custom adapter

public class ContentAdapter extends ArrayAdapter<Word>
{
    Display d;
    public ContentAdapter(Context context, ArrayList<Word>title)

{
    super(context, 0, title);
}

public View getView(int position, View convertView, ViewGroup parent) {

    // Check if the existing view is being reused otherwise inflate the view

    View listItemView = convertView;

    if (listItemView == null) {

        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.contents, parent, false);

    }

    Word w = getItem(position);

    TextView mt = (TextView) listItemView.findViewById(R.id.exp);

    mt.setText(w.getDiaryName());

    mt.setVisibility(View.VISIBLE);

    return listItemView;
}
}




//the fragment in which list of diaries is displayed
public class HistoryFragment extends Fragment 
{
 Context context;

    Word di;
    ListView lv;
    DatabaseReference reference;
    FirebaseAuth mAuth = FirebaseAuth.getInstance();
    public HistoryFragment(){
    //default constructor
    }

//The following method will get the context from the activity to which the fragment is attached 
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context=context;
    }
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
    final View view = inflater.inflate(R.layout.scrolllist, container, false);

    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference uidRef = rootRef.child("image").child(uid);
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //List<String> cities = new ArrayList<>();
            final ArrayList<Word> cities = new ArrayList<>();
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String cityName = ds.getKey();
                cities.add(new Word(cityName));
            }
            ListView listView = (ListView)view.findViewById(R.id.list_of_ds);
            ContentAdapter arrayAdapter = new ContentAdapter(context,cities);
            listView.setAdapter(arrayAdapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            //Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
        }
    };
    uidRef.addListenerForSingleValueEvent(valueEventListener);
    return view;
}
}
Gowtham K K
  • 3,123
  • 1
  • 11
  • 29