Getting nullpointer exception in my code
<p>I am making a contact list application. I have a button called search button and when it is clicked,  am reading the name written in textbox and searching the database here is my code:</p>

<pre><code>public class SearchResultsActivity extends Activity {

public DatabaseAdapter db;
public ArrayList&lt;String&gt; myList;
public ListView listview;
private TextView view;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);
    view = (TextView) findViewById(R.id.textView1);
    db = new DatabaseAdapter(this);
    myList = new ArrayList&lt;String&gt;();
    getSearchResults();
    //getAllContacts();
    printList();
}

public void getSearchResults(){
    db.open();
    db.getContactbyName(view.getText().toString());
    Cursor c = db.getContactbyName(view.getText().toString());
            if (c.moveToFirst()) {
        do {
            DisplayContact(c);

        } while (c.moveToNext());
    }

    db.close();

}

public void printList(){

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

    ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, myList);

    listview.setAdapter(adapter);
}

public void getAllContacts() {
    db.open();
    Cursor c = db.getAllContacts();
    if (c.moveToFirst()) {
        do {
            DisplayContact(c);

        } while (c.moveToNext());
    }
    db.close();
}

public void DisplayContact(Cursor c) {

    String entry = "";
    // if you add another attribute to your table, you need to change 3 into x
    for (int i=1; i&lt;5;i++){
        entry += c.getString(i) + "\n";
    }
    myList.add(entry);
}
}
</code></pre>

<p>In my code, getAllContacts() method works fine and prints all the contacts. But when i try to search a contact i get a nullpointer exception and application shuts down. Can anyone see the problem?</p>

<p>And here is the log cat:</p>

<p><img src="http://i.stack.imgur.com/T8mgQ.jpg" alt="enter image description here"></p>

<p>Thanks</p>

<p>Here is another version of logcat if you cannot read it :)</p>

<pre><code>03-26 11:52:37.983: D/dalvikvm(12524): GC_CONCURRENT freed 78K, 7% free 12818K/13703K,     
paused 10ms+3ms, total 45ms
03-26 11:52:37.988: D/AbsListView(12524): [unregisterDoubleTapMotionListener]
03-26 11:52:37.993: I/MotionRecognitionManager(12524):   .unregisterListener : /     
listener count = 0-&gt;0, listener=android.widget.AbsListView$4@42464368
03-26 11:55:53.778: I/Process(12524): Sending signal. PID: 12524 SIG: 9
03-26 11:56:42.238: E/SensorManager(13254): thread start
03-26 11:56:42.238: D/SensorManager(13254): registerListener :: handle = 0  name=     
LSM330DLC 3-axis Accelerometer delay= 200000 Listener=    
android.view.OrientationEventListener$SensorEventListenerImpl@4243e698
03-26 11:56:42.308: D/SensorManager(13254): unregisterListener::  Listener=     
android.view.OrientationEventListener$SensorEventListenerImpl@4243e698
03-26 11:56:42.308: D/Sensors(13254): Remain listener = Sending .. normal delay 200ms
03-26 11:56:42.308: I/Sensors(13254): sendDelay --- 200000000
03-26 11:56:42.308: D/SensorManager(13254): JNI - sendDelay
03-26 11:56:42.308: I/SensorManager(13254): Set normal delay = true
03-26 11:56:42.333: D/SensorManager(13254): registerListener :: handle = 0  name=    
LSM330DLC 3-axis Accelerometer delay= 200000 Listener=   
android.view.OrientationEventListener$SensorEventListenerImpl@4243e698
03-26 11:56:43.808: D/SensorManager(13254): registerListener :: handle = 0  name=   
LSM330DLC 3-axis Accelerometer delay= 200000 Listener= 
android.view.OrientationEventListener$SensorEventListenerImpl@42474b30
03-26 11:56:43.823: D/dalvikvm(13254): GC_CONCURRENT freed 119K, 7% free 12450K/13319K, 
paused 2ms+2ms, total 21ms
03-26 11:56:43.823: D/dalvikvm(13254): WAIT_FOR_CONCURRENT_GC blocked 6ms
03-26 11:56:43.843: D/SensorManager(13254): unregisterListener::  Listener=   
android.view.OrientationEventListener$SensorEventListenerImpl@42474b30
03-26 11:56:43.843: I/SensorManager(13254): Set min delay = 200000
03-26 11:56:43.843: I/SensorManager(13254): Before sending... delay = 200000
03-26 11:56:43.843: D/Sensors(13254): Remain listener = 
android.view.OrientationEventListener$SensorEventListenerImpl@4243e698
03-26 11:56:43.843: I/Sensors(13254): sendDelay --- 200000000
03-26 11:56:43.843: D/SensorManager(13254): JNI - sendDelay
03-26 11:56:43.848: D/SensorManager(13254): unregisterListener::  Listener= 
android.view.OrientationEventListener$SensorEventListenerImpl@4243e698
03-26 11:56:43.848: D/Sensors(13254): Remain listener = Sending .. normal delay 200ms
03-26 11:56:43.848: I/Sensors(13254): sendDelay --- 200000000
03-26 11:56:43.848: D/SensorManager(13254): JNI - sendDelay
03-26 11:56:43.848: I/SensorManager(13254): Set normal delay = true
03-26 11:56:43.873: D/SensorManager(13254): registerListener :: handle = 0  name= 
LSM330DLC 3-axis Accelerometer delay= 200000 Listener= 
android.view.OrientationEventListener$SensorEventListenerImpl@42474b30
03-26 11:56:44.003: W/IInputConnectionWrapper(13254): getSelectedText on inactive 
InputConnection
03-26 11:56:44.003: W/IInputConnectionWrapper(13254): setComposingText on inactive 
InputConnection
03-26 11:56:44.003: W/IInputConnectionWrapper(13254): getExtractedText on inactive 
InputConnection
03-26 11:56:45.658: D/AbsListView(13254): Get MotionRecognitionManager
03-26 11:56:45.673: D/AndroidRuntime(13254): Shutting down VM
03-26 11:56:45.673: W/dalvikvm(13254): threadid=1: thread exiting with uncaught   
exception (group=0x417922a0)
03-26 11:56:45.678: E/AndroidRuntime(13254): FATAL EXCEPTION: main
03-26 11:56:45.678: E/AndroidRuntime(13254): java.lang.RuntimeException: Unable to 
start activity 
ComponentInfo{cs306.hw2.database/cs306.hw2.database.SearchResultsActivity}:    
java.lang.NullPointerException 03-26 11:56:45.678: E/AndroidRuntime(13254):     at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
android.app.ActivityThread.access$600(ActivityThread.java:140)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
android.os.Handler.dispatchMessage(Handler.java:99)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
android.os.Looper.loop(Looper.java:137)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
android.app.ActivityThread.main(ActivityThread.java:4898)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
java.lang.reflect.Method.invokeNative(Native Method)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
java.lang.reflect.Method.invoke(Method.java:511)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
dalvik.system.NativeStart.main(Native   Method)
03-26 11:56:45.678: E/AndroidRuntime(13254): Caused by: java.lang.NullPointerException
03-26 11:56:45.678: E/AndroidRuntime(13254):    at     
s306.hw2.database.SearchResultsActivity.getSearchResults(SearchResultsActivity.java:34)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
cs306.hw2.database.SearchResultsActivity.onCreate(SearchResultsActivity.java:27)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at  
android.app.Activity.performCreate(Activity.java:5206)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
03-26 11:56:45.678: E/AndroidRuntime(13254):    at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
03-26 11:56:45.678: E/AndroidRuntime(13254):    ... 11 more
</code></pre>

<p>And here is the layour file
    
    </p>

<pre><code>    &lt;ListView
        android:id="@+id/contactcListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" &gt;
    &lt;/ListView&gt;

&lt;/LinearLayout&gt;
</code></pre>
