Search code examples
javaandroidlistviewparse-platform

Retriving Single column data from Parse Server and display in a listview


I am new to android and don't have that much knowledge about the database.

I created class Named "topics" in parse server and manually added "topicName" column.

Now I want to retrive this topicName value/Strings in a Listview but I'm getting an error

Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

Any help would be appreciated.

Code:

public class topics_class1 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    final ArrayList<String> topics1 = new ArrayList<>();
    final ListView listView = findViewById(R.id.listView_topics_class1);
    final ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, topics1);

    ParseQuery<ParseObject> query = ParseQuery.getQuery("topics");        
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null){

                for (int i=0; i< objects.size(); i++) {
                    topics1.add(objects.get(i).getString("topicName"));
                }
                listView.setAdapter(arrayAdapter);

                }

            else {
                e.printStackTrace();
            }
        }

    });
 }
}

Xml:

<?xml version="1.0" encoding="utf-8"?>

<ListView
    android:id="@+id/listView_topics_class1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="89dp" />


Solution

  • Error itself explain

    Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
    

    you did not setContentView(layout). that's why u getting error try setting setContentView(R.layout.layoutname);

    and get ListView accordingly

    final ListView listView = (ListView)findViewById(R.id.listView_topics_class1);

    Let me know if u have any other question