Why NullPointerException occures in Android ?

Many Android developer faced NullPointerException more than one times in a day.


When you try to use Any nullable object then NPE is getting.Any object that not initialize or not give  correct refrences .Also View have not give correct  refrences from xml.Sometimes make mistakes due to set wrong content to view.And also when you give any refrence of view from xml  layout see you setContentView().Before setContenview you cant get any refrence from xml.


Suppose i declare ListView and setAdapter.
ListView listview;
listview.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mStrings));

Now your listview is null and you try to setAdapter so nullPointerException is occur.

so you have to give refrence from the xml or create object of listview.

ListView listview=(ListView)findViewById(R.id.list);

or

ListView listview = new ListView(this);

In Most case NPE is occured Android Custom Dialog(Alert Dialog),Custom Adapter ListView.see red color text ,thats wrong.


Custom Dilaog/AlertDialog.


Dialog dialog = new Dialog(MyActivityName.this);
dialog.setContentView(R.layout.dialog_layout);


EditText editText =(EditText)dialog.findViewById(R.id.editext1);
TextView textView =(TextView)dialog.findViewById(R.id.textview1);
ImageView imgView =( ImageView)dialog.findViewById(R.id.imageview1);


editText.setText("edittext"); textView.setText("textview");
imgView.setImageResources(R.drawable.icon);


dialog.show();


Custom Adapter ListView


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder holder;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.row, null);
            holder = new ViewHolder();
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
   
        holder.title = (TextView) view.findViewById(R.id.txttitle);
        holder.description = (TextView) view.findViewById(R.id.txtdesc);

    
     holder.title.setText("title"+ position);
     holder.description.setText("description"+ position);


    return view
} public class ViewHolder {
        public TextView title,description;
    }



How To Solve NullPointerException::
When you get NPE ,First Check your Logcat Error.And find your app package and activity/class name.Then see in parentheses there are your activity/Class name and line no of code that have nullable View/Object.


Here i have NPE in GameActivity.java and Line no. is 11.


java.lang.RuntimeException: Unable to instantiate activity ComponentInfo
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
   at android.app.ActivityThread.access$1500(ActivityThread.java:117)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:130)
   at android.app.ActivityThread.main(ActivityThread.java:3691)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
   at android.app.Activity.findViewById(Activity.java:1653)
   at com.zafar.game.GameActivity.<init>(GameActivity.java:11)
   at java.lang.Class.newInstanceImpl(Native Method)
   at java.lang.Class.newInstance(Class.java:1409)
   at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
   ... 11 more