0

Can someone help me with this problem ? I tried to display data from the firebase in Listview, but it crashes! I have tried different codes from different topics at stack and other sites but received an error either at

adapter=new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,retrieve());

or at

lv.setAdapter(adapter);

with nullexception, so here is the code and error

JavaCode

Tried Listview

Now I've tried listview, and it crashes. Here is the code and error

 public class WorkActivity extends AppCompatActivity {

//    Spinner jobSpin;

ListView lv;
Button ShowButton;
// Define a String ArrayList for the job
private ArrayList<String> jobdes=new ArrayList<>();

// Define an ArrayAdapter for the list
private ArrayAdapter<String> arrayAdapter;

// Declaring String variable ( In which we are storing firebase server URL 
).
public static final String Firebase_Server_URL = 
"https://jobaps70.firebaseio.com/";
public static final String Firebase_Server_URL1 = 
 "https://jobaps70.firebaseio.com/jobs";

// Declaring String variables to store name & phone number get from 
EditText.
String PriceHolder, JobDescriptionHolder;
FirebaseHelper helper;

// Declaring Firebase object.
Firebase firebase,firebase1;

// Creating FirebaseAuth.
FirebaseAuth firebaseAuth ;

// Creating FirebaseAuth.
FirebaseUser firebaseUser;

// Creating Boolean variable that holds EditText is empty or not status.
Boolean EditTextStatus ;


TextView ShowDataTextView ;


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

      //        Actionbar
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setIcon(R.drawable.logo);
    getSupportActionBar().setBackgroundDrawable(new 
    ColorDrawable(getResources().getColor(R.color.Snow)));

    ShowButton = (Button)findViewById(R.id.show);

    // Adding MainActivity context into Firebase context.
    Firebase.setAndroidContext(WorkActivity.this);

    // Passing firebase Server URL into firebase object.
    firebase = new Firebase(Firebase_Server_URL);
    firebase1=new Firebase(Firebase_Server_URL1);

    firebaseAuth = FirebaseAuth.getInstance();

    // On activity start check whether there is user previously logged in or 
       not.
    if(firebaseAuth.getCurrentUser() == null){

        // Finishing current Profile activity.
        finish();

        // If user already not log in then Redirect to LoginActivity .
        Intent intent = new Intent(WorkActivity.this, LoginActivity.class);
        startActivity(intent);

        // Showing toast message.
        Toast.makeText(WorkActivity.this, "Please Log in to continue", 
        Toast.LENGTH_LONG).show();

    }

    // Adding firebaseAuth current user info into firebaseUser object.
    firebaseUser = firebaseAuth.getCurrentUser();

    // Getting logged in user email from firebaseUser.getEmail() method and 
        set into TextView.
    setTitle( firebaseUser.getEmail());
    setTitleColor(R.color.DarkGray);
      firebase.child("jobs");

    // Associate the jobs' list with the corresponding ListView
    lv = (ListView) findViewById(R.id.lvshow);

    // Set the ArrayAdapter to the ListView
    arrayAdapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, jobdes);
    lv.setAdapter(arrayAdapter);

    // Attach a ChildEventListener to the teacher database, so we can 
          retrieve the job entries
    firebase.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            // Get the value from the DataSnapshot and add it to the jobs' 
         list
            JobDetails jo = (JobDetails) 
         dataSnapshot.getValue(JobDetails.class);
            String joString = String.valueOf(jo);
            arrayAdapter.add(joString);
 //

            // Notify the ArrayAdapter that there was a change
            arrayAdapter.notifyDataSetChanged();
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(FirebaseError databaseError) {

        }
    });
}

}

JobDetail

 public class JobDetails {

    private String price;
    private String jobDescription;
    private String jobSpinner;
    public String email;
    public JobDetails() {
        // This is default constructor.
    }

    public String getPrice() {

        return price;
    }

    public void setPrice(String pri) {

        this.price = pri;
    }

    public String getjobDescription() {

        return jobDescription;

    }

    public void setjobDescription(String jobd) {

        this.jobDescription = jobd;
    }

    public void setjob(String jry) {

        this.jobSpinner = jry ;

    }

    public String getjob() {

        return jobSpinner;

    }

    public void setemail(String jel) {

        this.email = jel ;

    }

    public String getemail() {

        return email;

    }
   @Override
   public String toString() {
    return this.jobSpinner + ": " + this.jobDescription+ ": " +price;
}

}

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.gumption.zeeshanahmed.jobaps.WorkActivity">
<TextView
    android:id="@+id/textdispjobs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/text_display_jobs"
    android:textSize="40sp"
    android:textAlignment="center"/>

<ListView
    android:id="@+id/lvshow"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/show" />


</RelativeLayout>

Error

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.gumption.zeeshanahmed.jobaps, PID: 4774
              java.lang.RuntimeException: Unable to start activity 
              ComponentInfo{com.gumption.zeeshanahmed.jobaps/
              com.gumption.zeeshanahmed.jobaps.WorkActivity}: 
              java.lang.NullPointerException: Attempt to invoke virtual 
              method 'void 
             android.widget.ListView.setAdapter(android.widget.ListAdapter)' 
             on a null object reference at 
  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                  at android.app.ActivityThread.-wrap11(ActivityThread.java)
                  at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)at 
 android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method) at 
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
 (ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main 
 (ZygoteInit.java:616) 
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 
 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a 
 null object reference at 
 com.gumption.zeeshanahmed.jobaps.WorkActivity.onCreate
 (WorkActivity.java:109)
              at android.app.Activity.performCreate(Activity.java:6237) at 
 android.app.Instrumentation.callActivityOnCreate
 (Instrumentation.java:1107) at 
 android.app.ActivityThread.performLaunchActivity
 (ActivityThread.java:2369)atandroid.app.ActivityThread.handleLaunchActivity
 (ActivityThread.java:2476) at android.app.ActivityThread.-
  wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage
 (ActivityThread.java:1344) at android.os.Handler.dispatchMessage
 (Handler.java:102) at android.os.Looper.loop(Looper.java:148) at 
 android.app.ActivityThread.main(ActivityThread.java:5417) at 
 java.lang.reflect.Method.invoke(Native Method) at 
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
 (ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main
 (ZygoteInit.java:616) 

java Error line

It is showing error at this

 lv.setAdapter(arrayAdapter);

It would be very helpful if someone could help me with this. Thank you!

Kappa
  • 15
  • 1
  • 1
    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) – AskNilesh Dec 13 '17 at 12:02
  • Please do verify that, whatever layout name "activity_edits_jobs" you are using is excatly same to the layout in which list view is there. – Ankit Patidar Dec 13 '17 at 12:12
  • @ankitpatidar well i made separate XML for this class and it worked well my both classes have same layout with button, one for editing and one for applying for jobs well anyhow thanks for help. P.S. it works – Zeeshan Ahmad Dec 14 '17 at 08:08
  • That's really ok.... – Ankit Patidar Dec 14 '17 at 08:10

0 Answers0