-3

I am new to android so i am not able to figure out why this code is giving NullPointerException. Plz Help

The app keeps crashing.

MainActivity class

package com.example.yash.listview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

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



        ArrayList<wordClass> arrayList = new ArrayList<wordClass>();

        arrayList.add(new wordClass("Yash","Infosys"));
        arrayList.add(new wordClass("Shivam","HCL"));
        arrayList.add(new wordClass("Mohit","HCL"));
        arrayList.add(new wordClass("Deepak","HCL"));
        arrayList.add(new wordClass("Yash","HCL"));

        wordAdapter Adapt = new wordAdapter(this,arrayList);
        ListView listView = (ListView) findViewById(R.id.mera);

        listView.setAdapter(Adapt);



    }
}

wordClass class

package com.example.yash.listview;

/**
 * Created by Yash on 5/28/2017.
 */

public class wordClass {

    private String name;
    private String company;

    public wordClass(String name, String company) {
        this.name = name;
        this.company = company;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }
}

wordAdapter class

package com.example.yash.listview;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Yash on 5/28/2017.
 */

public class wordAdapter extends ArrayAdapter<wordClass> {

    private static final String LOG_TAG = wordAdapter.class.getSimpleName();

    public wordAdapter(Activity context, ArrayList<wordClass> arrayList) {
        super(context,0, arrayList);
    }

    @NonNull
    @Override
    public View getView(int position,View convertView,ViewGroup parent) {
        View v = convertView;

        if(v==null)
        {
            v = LayoutInflater.from(getContext()).inflate(R.layout.list_layout,parent,false);
        }

        wordClass i= getItem(position);



            TextView textView = (TextView) v.findViewById(R.id.textView);
            TextView textView1 = (TextView) v.findViewById(R.id.textView1);


                textView.setText(i.getName());



                textView1.setText(i.getCompany());



        return v;



    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:layout_margin="10dp"
    android:orientation="vertical"
    tools:context="com.example.yash.listview.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        />

</LinearLayout>

listview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/mera"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

    </ListView>

</LinearLayout>

list_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp"/>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15dp"/>

</LinearLayout>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Yash
  • 9
  • 1
  • 1
  • 5
    add the logcat so we can see where the crash happens – yanivtwin May 28 '17 at 14:51
  • 1
    include the line where your code is throwing npe.. Read [mcve].. Currently we know less about your issue than you do – Suraj Rao May 28 '17 at 14:52
  • In your activity_main.xml you don't have ListView and you're trying to access it in MainActivity using findViewById() method that's why you're getting Exception. – Shashanth May 28 '17 at 14:54

1 Answers1

1

Without looking at your Logcat and where the crash happens , it seems you put the Listview in the wrong xml so findviewbyid won't find it

you need to do something like :

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    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"
    android:layout_margin="10dp"
    android:orientation="vertical"
    tools:context="com.example.yash.listview.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        />
    <ListView
        android:id="@+id/mera"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

    </ListView>
</LinearLayout>

This should be your main activity , this way it will find the listview.

yanivtwin
  • 617
  • 8
  • 32