I am new to android programming and I have been getting bug in my code for the last day or so.
When I run my live chat I can send and read messages as intended, but when I back track to the page that shows my list of conversations and click back into the live chat and resend a message my app crashes with the error below. Why is that? How do I fix it?
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.view.LayoutInflater.from(LayoutInflater.java:282)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:213)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:207)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:193)
at com.example.chatbox4.adapterListLiveChat.<init>(adapterListLiveChat.java:36)
at com.example.chatbox4.LiveChatFragment$2.onChildAdded(LiveChatFragment.java:131)
RELATED CODE
CHAT FRAGMENT
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AppCompatActivity activity=(AppCompatActivity)v.getContext();
activity.getSupportFragmentManager().beginTransaction().replace(R.id.container, new LiveChatFragment(groupId.get(position),groupName.get(position),name,schoolId)).addToBackStack(null).commit();
groupId.clear();
groupName.clear();
}
});
LIVE CHAT FRAGMENT
data = FirebaseDatabase.getInstance().getReference().child("ChatLogs").child(gid);
data.addChildEventListener(new ChildEventListener() {
@Override
//this runs first
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
showChat(snapshot);
adapterListLiveChat customList = new adapterListLiveChat(getContext(), liveMessage, ownerName, timePosted,thumbnailUrl,ownerId); //SECOND ERROR ON LINE 131
list.setAdapter(customList);
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
showChat(snapshot);
adapterListLiveChat customList = new adapterListLiveChat(getContext(), liveMessage, ownerName, timePosted,thumbnailUrl,ownerId);
list.setAdapter(customList);
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
return v;
}
private void showChat(DataSnapshot snapshot) {
Iterator i = snapshot.getChildren().iterator();
while (i.hasNext()){
liveMessage.add((String) ((DataSnapshot)i.next()).getValue());
ownerId.add((String) ((DataSnapshot)i.next()).getValue());
ownerName.add((String) ((DataSnapshot)i.next()).getValue());
thumbnailUrl.add((String) ((DataSnapshot)i.next()).getValue());
timePosted.add((Long) ((DataSnapshot)i.next()).getValue());
}
}
CUSTOM ADAPTER FOR LIVE CHAT
public class adapterListLiveChat extends ArrayAdapter{
ArrayList<String> liveMessage = new ArrayList<String>();
ArrayList<String> ownerId = new ArrayList<String>();
ArrayList<String> ownerName = new ArrayList<String>();
ArrayList<String> thumbnailUrl = new ArrayList<String>();
ArrayList<Long> timePosted = new ArrayList<Long>();
Context context;
public adapterListLiveChat(Context context, ArrayList<String> liveMessage, ArrayList<String> ownerName, ArrayList<Long> timePosted, ArrayList<String> thumbnailUrl, ArrayList<String> ownerId) {
super(context,R.layout.livechatlayout, ownerName); //FIRST ERROR ON LINE 36
this.context = context;
this.liveMessage = liveMessage;
this.ownerId = ownerId;
this.ownerName = ownerName;
this.thumbnailUrl = thumbnailUrl;
this.timePosted = timePosted;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(convertView == null){
LayoutInflater layoutInflater = LayoutInflater.from(context);
row = layoutInflater.inflate(R.layout.livechatlayout, null);
TextView userName = (TextView) row.findViewById(R.id.userName);
TextView message = (TextView) row.findViewById(R.id.textView);
final ImageView profilePic = (ImageView) row.findViewById(R.id.profilePic);