Kippi
Kippi

Reputation: 514

Null pointer exception in android studio

I'm getting a null pointer exception in a project on AndroidStudio. I can't figure out why. I know I should debug the program but I'm having problems with my computer and android studio keeps crashing when I try to debug (will reinstall tomorrow but have to give this in before...) so I'm hoping someone with a more experienced eye (I'm very new at this) will be able to spot the problem w/o having to actually run the thing. The exception is on line 56 (line 2 of onItemClick) of the following code:

package com.example.amosref.middleproject;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
/**
 * Created by Amos on 06/08/2015.
 */
public class ContactFromList extends ListActivity {

    ArrayList<Contact> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);

        ArrayList<Contact> list = null;

        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = openFileInput("contacts");
            ois = new ObjectInputStream(fis);
            list = (ArrayList<Contact>)ois.readObject();
        }catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }catch (IOException ex) {
            ex.printStackTrace();
        }catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }finally {
            try{
                if(ois!=null) ois.close();
            }catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        if(list == null) list = new ArrayList<>();

        ViewContactsAdapter contactsAdapter= new ViewContactsAdapter(list, this);
        setListAdapter(contactsAdapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Contact chosenContact = list.get(position);
        Intent intent = new Intent(this,ViewContact.class);
        intent.putExtra("name", chosenContact.getName());
        intent.putExtra("phone", chosenContact.getPhoneNum());
        intent.putExtra("dob", chosenContact.getDob());
        intent.putExtra("email", chosenContact.getEmailAddress());
        intent.putExtra("preferred", chosenContact.getPreferredDays());
        intent.putExtra("website", chosenContact.getWebsite());
        intent.putExtra("address", chosenContact.getHomeAddress());
        intent.putExtra("pic", chosenContact.getPhoto());
        startActivity(intent);
    }
}

logcat:

08-09 20:12:29.671  30816-30816/com.example.amosref.middleproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.amosref.middleproject, PID: 30816
    java.lang.NullPointerException
            at com.example.amosref.middleproject.ContactFromList.onListItemClick(ContactFromList.java:56)
            at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
            at android.widget.AdapterView.performItemClick(AdapterView.java:299)
            at android.widget.AbsListView.performItemClick(AbsListView.java:1153)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java:3065)
            at android.widget.AbsListView$3.run(AbsListView.java:4031)
            at android.os.Handler.handleCallback(Handler.java:808)
            at android.os.Handler.dispatchMessage(Handler.java:103)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5292)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
            at dalvik.system.NativeStart.main(Native Method)

Thanks in advance!

Upvotes: 0

Views: 2401

Answers (1)

Mark
Mark

Reputation: 9919

Remove: 'ArrayList<Contact> list = null' from onCreate. You have created a instance (local) variable to hold contacts, so your field variable of the same name is never used and will remain null when it is referenced in onListItemClick.

Upvotes: 2

Related Questions