Question

When I add the line list.add(sb.toString()); the program stops working.
When I delete it data loads to StringBuilder, but I can't display it.
I searched for a solution, but every code example was the same.
I can't find the problem.

public ArrayList<String> list;
public ArrayAdapter<String> adapter;

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

    listView = (ListView) findViewById(R.id.contactList);
    // listView.setOnItemClickListener(this);

    Cursor phones = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, null);
    while (phones.moveToNext()) {

        String name = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

        String phoneNumber = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        StringBuilder sb = new StringBuilder();

        sb.append(name).append(" ").append(phoneNumber);

        list.add(sb.toString());
        Log.d("Added:", sb.toString());

    }
    adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, list);
    listView.setAdapter(adapter);

    phones.close();

stacktrace:

02-05 10:41:03.505: E/AndroidRuntime(29762): FATAL EXCEPTION: main
02-05 10:41:03.505: E/AndroidRuntime(29762): java.lang.NullPointerException
02-05 10:41:03.505: E/AndroidRuntime(29762):    at com.example.anewapp.CameraPreview.surfaceCreated(CameraPreview.java:30)
02-05 10:41:03.505: E/AndroidRuntime(29762):    at android.view.SurfaceView.updateWindow(SurfaceView.java:547)
02-05 10:41:03.505: E/AndroidRuntime(29762):    at android.view.SurfaceView.access$000(SurfaceView.java:85)
02-05 10:41:03.505: E/AndroidRuntime(29762):    at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:173)
02-05 10:41:03.505: E/AndroidRuntime(29762):    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:671)
02-05 10:41:03.505: E/AndroidRuntime(29762):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1821)
02-05 10:41:03.505: E/AndroidRuntime(29762):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:999)
02-05 10:41:03.505: E/AndroidRuntime(29762):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4217)
02-05 10:41:03.505: E/AndroidRuntime(29762):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
02-05 10:41:03.505: E/AndroidRuntime(29762):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
02-05 10:41:03.505: E/AndroidRuntime(29762):    at android.view.Choreographer.doFrame(Choreographer.java:525)
02-05 10:41:03.505: E/AndroidRuntime(29762):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
02-05 10:41:03.505: E/AndroidRuntime(29762):    at android.os.Handler.handleCallback(Handler.java:615)
02-05 10:41:03.505: E/AndroidRuntime(29762):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-05 10:41:03.505: E/AndroidRuntime(29762):    at android.os.Looper.loop(Looper.java:137)
02-05 10:41:03.505: E/AndroidRuntime(29762):    at android.app.ActivityThread.main(ActivityThread.java:4802)
02-05 10:41:03.505: E/AndroidRuntime(29762):    at java.lang.reflect.Method.invokeNative(Native Method)
02-05 10:41:03.505: E/AndroidRuntime(29762):    at java.lang.reflect.Method.invoke(Method.java:511)
02-05 10:41:03.505: E/AndroidRuntime(29762):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:813)
02-05 10:41:03.505: E/AndroidRuntime(29762):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:580)
02-05 10:41:03.505: E/AndroidRuntime(29762):    at dalvik.system.NativeStart.main(Native Method)
Was it helpful?

Solution

You need to initialize your list

list = new ArrayList<String>();

OTHER TIPS

The stack traceback you show is for a null pointer exception. It is saying line 30 of your file CameraPreview.java dereferences a null pointer.

Take a look at line 30. Understand which pointer is null and why. Debug the code, so when control passes there, the pointer is referencing an object.

Clearly, the stack traceback doesn't have any connection with the onCreate() you showed. So that's another thing for you to work on. Why do you think it failed in the list.add() (or, alternatively, why did you show the wrong stack traceback)?

Where is the variable "list" initialized, by the way?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top