I have problem with my code. I'm learning how to create Simple Firebase CRUD application with Android . Here is my MainActivity code :
package com.ogigpermana.crudfirebaseapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.ogigpermana.crudfirebaseapp.adapter.ArtistList;
import com.ogigpermana.crudfirebaseapp.model.Artist;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
EditText editTextName;
Button buttonAdd;
Spinner spinnerGenres;
DatabaseReference databaseArtists;
ListView listViewArtists;
List<Artist> artistList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseArtists = FirebaseDatabase.getInstance().getReference("artists");
editTextName = (EditText) findViewById(R.id.editTextName);
buttonAdd = (Button) findViewById(R.id.buttonAddArtist);
spinnerGenres = (Spinner) findViewById(R.id.spinnerGenres);
listViewArtists = (ListView) findViewById(R.id.listViewArtists);
artistList = new ArrayList<>();
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addArtist();
}
});
}
@Override
protected void onStart() {
super.onStart();
databaseArtists.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
artistList.clear();
for (DataSnapshot artistSnapshot: dataSnapshot.getChildren()){
Artist artist = artistSnapshot.getValue(Artist.class);
artistList.add(artist);
}
ArtistList adapter = new ArtistList(MainActivity.this, artistList);
listViewArtists.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void addArtist(){
String name = editTextName.getText().toString().trim();
String genre = spinnerGenres.getSelectedItem().toString();
if (!TextUtils.isEmpty(name)){
String id = databaseArtists.push().getKey();
Artist artist = new Artist(id, name, genre);
databaseArtists.child(id).setValue(artist);
Toast.makeText(this, "Artis berhasil ditambah", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "Nama tidak boleh kosong", Toast.LENGTH_LONG).show();
}
}
}
And here is the errors code that displayed when i run :
07-03 00:30:45.727 30907-30907/com.ogigpermana.crudfirebaseapp
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ogigpermana.crudfirebaseapp, PID: 30907
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a
null object reference
at
com.ogigpermana.crudfirebaseapp.MainActivity$2.onDataChange(MainActivity.java:72)
at com.google.android.gms.internal.zzbmz.zza(Unknown Source)
at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
at com.google.android.gms.internal.zzboc$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…