Error when calling object - Android

I'm calling the object here.

public class TestDetails extends ListActivity {

    protected TextView testNameText;
    protected SQLiteDatabase db;
    protected TextView testvalueText;
    protected List<TestAction> actions;
    protected TestItemAdapter adapter;
    protected int testId;
    protected int categoryId;

    @Override
    //adds options menu
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.list_search: onSearchRequested();
            break;
        }
        return true;
    }
    //end of add options menu
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_details);

     // Get the intent, verify the action and get the query
        db = (new DatabaseHelper(this)).getWritableDatabase();
        Intent intent1 = getIntent();
        SimpleSearch SSearch = new SimpleSearch();
        if (Intent.ACTION_SEARCH.equals(intent1.getAction())) {
          String query = intent1.getStringExtra(SearchManager.QUERY);
          SSearch.testSearch(query);
          }

        testId = getIntent().getIntExtra("EMPLOYEE_ID", 0);
        SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
        Cursor cursor = db.rawQuery("SELECT emp._id, emp.firstName, emp.lastName, emp.title, emp.officePhone, emp.cellPhone, emp.email, emp.managerId, mgr.firstName managerFirstName, mgr.lastName managerLastName FROM employee emp LEFT OUTER JOIN employee mgr ON emp.managerId = mgr._id WHERE emp._id = ?", 
                new String[]{""+testId});

        if (cursor.getCount() == 1)
        {
            cursor.moveToFirst();
            testNameText = (TextView) findViewById(R.id.employeeName);
            testNameText.setText(cursor.getString(cursor.getColumnIndex("firstName")) + " " + cursor.getString(cursor.getColumnIndex("lastName")));
            actions = new ArrayList<TestAction>();

            String officePhone = cursor.getString(cursor.getColumnIndex("officePhone"));
            if (officePhone != null) {
                actions.add(new TestAction("Call office", officePhone, TestAction.ACTION_CALL));
            }

            String cellPhone = cursor.getString(cursor.getColumnIndex("cellPhone"));
            if (cellPhone != null) {
                actions.add(new TestAction("Call mobile", cellPhone, TestAction.ACTION_CALL));
                actions.add(new TestAction("SMS", cellPhone, TestAction.ACTION_SMS));
            }

            String email = cursor.getString(cursor.getColumnIndex("email"));
            if (email != null) {
                actions.add(new TestAction("Email", email, TestAction.ACTION_EMAIL));
            }

            categoryId = cursor.getInt(cursor.getColumnIndex("managerId"));
            if (categoryId>0) {
                actions.add(new TestAction("View manager", cursor.getString(cursor.getColumnIndex("managerFirstName")) + " " + cursor.getString(cursor.getColumnIndex("managerLastName")), TestAction.ACTION_VIEW));
            }

            cursor = db.rawQuery("SELECT count(*) FROM employee WHERE managerId = ?", 
                    new String[]{""+testId});
            cursor.moveToFirst();
            int count = cursor.getInt(0);
            if (count>0) {
                actions.add(new TestAction("View direct reports", "(" + count + ")", TestAction.ACTION_REPORTS));
            }

            adapter = new TestItemAdapter();
            setListAdapter(adapter);
        }

    }

    class TestItemAdapter extends ArrayAdapter<TestAction> {
        TestItemAdapter() {
            super(TestDetails.this, R.layout.action_list_item, actions);
        }

        @Override
        public boolean areAllItemsEnabled() {
            return false;
        }
        public boolean isEnabled(int position) {
           return false;
        }
        public View getView(int position, View convertView, ViewGroup parent) {
            TestAction action = actions.get(position);
            LayoutInflater inflater = getLayoutInflater();
            View view = inflater.inflate(R.layout.action_list_item, parent, false);
            TextView label = (TextView) view.findViewById(R.id.label);
            label.setText(action.getLabel());
            TextView data = (TextView) view.findViewById(R.id.data);
            data.setText(action.getData());
            return view;
        }

    }

}

This is the class from which I'm calling the object.(part of the class)

public class SimpleSearch extends ListActivity {
    protected SQLiteDatabase db;
    protected Cursor cursor;
    protected ListAdapter adapter;
    protected String query;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        db = (new DatabaseHelper(this)).getWritableDatabase();
        // Get the intent, verify the action and get the query
        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
          String query = intent.getStringExtra(SearchManager.QUERY);
          testSearch(query);
          }
        else TestListAll();
        }

But I'm getting a force close on running the application. Stack trace shows the error to be in SSearch.testSearch(query); statement. What am I missing here?

Stack trace:

app_vercode:1
device_model:umts_jordan
build_version:1.11.18
condition:1
processName:com.simple.search
pid:3529
uid:10063
tag:null
shortMsg:java.lang.NullPointerException
longMsg:java.lang.NullPointerException: Unable to start activity ComponentInfo{com.simple.search/com.simple.search.TestDetails}: java.lang.NullPointerException
stackTrace:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.simple.search/com.simple.search.TestDetails}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1664)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1680)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3703)
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:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.simple.search.SimpleSearch.testSearch(SimpleSearch.java:68)
at com.simple.search.TestDetails.onCreate(TestDetails.java:59)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1628)
... 11 more

Answers


Most likely the line

String query = intent1.getStringExtra(SearchManager.QUERY);

Is assigning a null value to query - are you sure the correct extra is there? I usually get extras in this manner:

public static final String TAG = "MyActivity";
public static final String DATA_KEY = "DataKey";
public static final String MY_CUSTOM_ACTION = "MyCustomSearchAction";

public void onCreate(Bundle savedInstanceState) {
    Intent intent1 = getIntent();
    SimpleSearch SSearch = new SimpleSearch();
    if (intent1.getAction().equals(MY_CUSTOM_ACTION) 
          && intent1.hasExtra(SearchManager.QUERY)
          && intent.hasExtra(DATA_KEY)) {
        String query = intent1.getStringExtra(SearchManager.QUERY);
        DataObject data = intent1.getParcelableExtra(DATA_KEY);   
        if (query != null && data != null)
            SSearch.testSearch(query, data);
        else {
            //invalid query
           Log.d(TAG,"Activity started with invalid query data - closing");
           this.finish();
           return;
        }
    } else {
       //Invalid Intent
       Log.d(TAG,"Activity started with invalid intent - closing");
       this.finish();
       return;
    }
}

A data object can look like this:

public class DataObject implements Parcelable {

    public String someData;
    public String someMoreData;

    @Override
    public int describeContents() {
        return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(someData);
    dest.writeString(someMoreDate);
}

//Constructor for parceler
public DataObject(Parcel src) {
    someData = src.readString();
    someMoreData = src.readString();
}

public static final Parcelable.Creator<DataObject> CREATOR = 
                new Parcelable.Creator<DataObject>() {

    public DataObject createFromParcel(Parcel in) {
        return new DataObject(in);
    }

    public DataObject[] newArray(int size) {
        return new DataObject[size];
    }
};

}

To start your activity just go:

DataObject data = new DataObject();
data.someData = "test";
data.someMoreData = "test2";
Intent intent = new Intnent(this, MyActivity.class);
intent.setAction(MyActivity.MY_CUSTOM_ACTION);
intent.putExtra(MyActivity.DATA_KEY,data);
intent.putExtra(SearchManager.QUERY, "Query");
startActivity(intent);

Need Your Help

Copy Hash Table in Lisp

copy hashtable common-lisp

I have recently been working with hash tables in Common Lisp. I have been wondering how to make a separate copy of a hash table containing all the same values as the first. Is there an official wa...

get main activity context for imageView

java android multithreading uiimageview

I'm using thread (runOnUiThread) for adding imageView to mainActivity.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.