gunZ333 gunZ333 - 4 months ago 475
Android Question

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

I am trying to make a list view that contains pictures and text within another activity. I am not so focused in android programming, so basically like a newbie in android programming and i have difficulties in debugging my app. please help me :)

Error :

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
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.fingervoiz.app.tamla.Activity.ListDataIdntActivity.onCreate(ListDataIdntActivity.java:53)


Result Shown in LogCat:

07-10 06:24:12.386 14968-14968/com.fingervoiz.app.tamla E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.fingervoiz.app.tamla, PID: 14968
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fingervoiz.app.tamla/com.fingervoiz.app.tamla.Activity.ListDataIdntActivity}: 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:2534)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2608)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
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.fingervoiz.app.tamla.Activity.ListDataIdntActivity.onCreate(ListDataIdntActivity.java:53)
at android.app.Activity.performCreate(Activity.java:6092)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2608) 
at android.app.ActivityThread.access$800(ActivityThread.java:178) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) 
at android.os.Handler.dispatchMessage(Handler.java:111) 
at android.os.Looper.loop(Looper.java:194) 
at android.app.ActivityThread.main(ActivityThread.java:5637) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 
07-10 06:24:18.049 14968-14999/com.fingervoiz.app.tamla D/OpenGLRenderer: Flushing caches (mode 0)
07-10 06:24:18.053 14968-14999/com.fingervoiz.app.tamla D/OpenGLRenderer: Flushing caches (mode 0)
07-10 06:24:32.360 14968-14968/com.fingervoiz.app.tamla I/Process: Sending signal. PID: 14968 SIG: 9


My Code:(Adapter)

package com.fingervoiz.app.tamla.Adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fingervoiz.app.tamla.Model.TamlaDataIdntModel;
import com.fingervoiz.app.tamla.R;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;

public class ListDIDadapter extends ArrayAdapter<TamlaDataIdntModel> {

Context context;
int layoutResID;

ArrayList<TamlaDataIdntModel> dataToList = new ArrayList<TamlaDataIdntModel>();

public ListDIDadapter(Context context, int layoutResID, ArrayList<TamlaDataIdntModel> dataToList){
super(context,layoutResID,dataToList);
this.layoutResID = layoutResID;
this.context = context;
this.dataToList = dataToList;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View view = convertView;
Holder holder = null;

if (view == null){
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(layoutResID,parent,false);

holder = new Holder();
holder.txtFname = (TextView) view.findViewById(R.id.did_item_fullname);
holder.txtGender = (TextView) view.findViewById(R.id.did_item_gender);
holder.txtBplace = (TextView) view.findViewById(R.id.did_item_bplace);
holder.txtBday = (TextView) view.findViewById(R.id.did_item_bday);
holder.txtAddress = (TextView) view.findViewById(R.id.did_item_address);
holder.imgProfile = (ImageView) view.findViewById(R.id.did_item_profilepic);
view.setTag(holder);
}else {
holder = (Holder) view.getTag();
}

//FILL
TamlaDataIdntModel dids = dataToList.get(position);
if(dids != null){
holder.txtFname.setText(dids.get_lname()+", "+dids.get_fname()+" "+dids.get_mname());
holder.txtGender.setText(dids.get_gender());
holder.txtBplace.setText(dids.get_bplace());
holder.txtBday.setText(dids.get_bday());
holder.txtAddress.setText(dids.get_address());

byte[] outImage = dids.get_dp();
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
holder.imgProfile.setImageBitmap(theImage);
}

return view;
}

static class Holder{
TextView txtFname;
TextView txtGender;
TextView txtBplace;
TextView txtBday;
TextView txtAddress;
ImageView imgProfile;
}
}


(List Activity)

package com.fingervoiz.app.tamla.Activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.fingervoiz.app.tamla.Adapter.ListDIDadapter;
import com.fingervoiz.app.tamla.DataAccessObjects.DataIdntDAO;
import com.fingervoiz.app.tamla.Model.TamlaDataIdntModel;
import com.fingervoiz.app.tamla.R;

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

public class ListDataIdntActivity extends Activity{

public static final String TAG = "ListDataIdntActivity";

public static final int REQUEST_CODE_ADD_COMPANY = 40;
public static final String EXTRA_ADDED_COMPANY = "extra_key_added_did";

private ListView mListViewDID;
private TextView emptyTxt;
private Button b1;

private ListDIDadapter adapter;
private ArrayList<TamlaDataIdntModel> arryList;
private DataIdntDAO mDIDdao;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//Initialize View
mListViewDID = (ListView) findViewById(R.id.list_did);
emptyTxt = (TextView) findViewById(R.id.txt_empty_list_dids);
b1 = (Button) findViewById(R.id.back_list_did);

//Fill List View
mDIDdao = new DataIdntDAO(this);
List<TamlaDataIdntModel> mIdntList = mDIDdao.getAllDID();

if (mIdntList != null && !mIdntList.isEmpty()){
adapter = new ListDIDadapter(this,R.layout.activity_list_did,arryList);
mListViewDID.setAdapter(adapter);
}else {
emptyTxt.setVisibility(View.VISIBLE);
mListViewDID.setVisibility(View.GONE);
}

}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_CODE_ADD_COMPANY){
if(requestCode == RESULT_OK){
// add the added identity to the list and refresh the list view
if(data != null){
TamlaDataIdntModel createdNewDID = (TamlaDataIdntModel)data.getSerializableExtra(EXTRA_ADDED_COMPANY);
if(createdNewDID != null){
if(arryList == null){
arryList = new ArrayList<TamlaDataIdntModel>();
arryList.add(createdNewDID);

if (adapter == null){
if (mListViewDID.getVisibility() != View.VISIBLE){
mListViewDID.setVisibility(View.VISIBLE);
emptyTxt.setVisibility(View.GONE);
}

adapter = new ListDIDadapter(this,R.layout.activity_list_did,arryList);
mListViewDID.setAdapter(adapter);
}else {

if (arryList != null){

for (Object object : arryList) {

adapter.insert((TamlaDataIdntModel) object, adapter.getCount());
}
}
adapter.notifyDataSetChanged();
}
}
}

}
}
} else {
super.onActivityResult(requestCode,resultCode,data);
}
}

@Override
protected void onDestroy() {
super.onDestroy();
mDIDdao.close();
}
}

Answer

You're missing one very important line - setContentView(). That line binds the xml with the Activity's view and it's the view that's being used for findViewById. Add this line to your onCreate:

@Override
public void onCreate(Bundle state){
    super.onCreate(state);
    setContentView(R.layout.name_of_the_xml);
Comments