Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7049541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:01:47+00:00 2026-05-28T03:01:47+00:00

I’m getting a NullPointerException in my Android app and, as far as I can

  • 0

I’m getting a NullPointerException in my Android app and, as far as I can tell from LogCat, it’s happening when trying to set a listview to clickable. This is my code.

public class MyActivity extends Activity {
private ListView lstView = (ListView) findViewById(R.id.listView1);
private SQLiteDatabase database;

private static final String fields[] = { "field1", "field2", "field3" };


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    CursorAdapter dataSource;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //lstView.setClickable(true);   
private static final String fields[] = { "field1", "field2", "field3" };


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    CursorAdapter dataSource;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    lstView.setClickable(true);

    DatabaseHelper helper = new DatabaseHelper(this);
    database = helper.getWritableDatabase();
    Cursor data = database.query("mydb", fields, null, null, null, null, null);
    dataSource = new SimpleCursorAdapter(this, R.layout.row, data, fields, new int[] {R.id.field1, R.id.field2});
    database.close();
    lstView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
            Intent newActivity = new Intent(getApplicationContext(), NewIntent.class);
            newActivity.putExtra("itemId",lstView.getItemIdAtPosition(position));
            // start activity
            startActivity(newActivity);
          }
        });

}


}

This is the LogCat output

01-10 14:03:53.740: E/AndroidRuntime(30361): FATAL EXCEPTION: main
01-10 14:03:53.740: E/AndroidRuntime(30361): java.lang.RuntimeException: Unable to start activity ComponentInfo{app.jonward.myapp/app.jonward.myapp.MyActivity}: java.lang.NullPointerException
01-10 14:03:53.740: E/AndroidRuntime(30361):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1821)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at android.app.ActivityThread.access$1500(ActivityThread.java:132)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at android.os.Looper.loop(Looper.java:150)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at android.app.ActivityThread.main(ActivityThread.java:4263)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at java.lang.reflect.Method.invokeNative(Native Method)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at java.lang.reflect.Method.invoke(Method.java:507)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at dalvik.system.NativeStart.main(Native Method)
01-10 14:03:53.740: E/AndroidRuntime(30361): Caused by: java.lang.NullPointerException
01-10 14:03:53.740: E/AndroidRuntime(30361):    at  app.jonward.castr.CastrActivity.onCreate(MyActivity.java:31)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
01-10 14:03:53.740: E/AndroidRuntime(30361):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)
01-10 14:03:53.740: E/AndroidRuntime(30361):    ... 11 more

The code on line 31 of MyActivity that LogCat says caused the exception is

lstView.setClickable(true);

FIXED
Here is the working code. There are a few changes.
public class MyActivity extends ListActivity {
private SQLiteDatabase database;
private static final String fields[] = { “field1”, “field2”, “_id” };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    CursorAdapter dataSource;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    DatabaseHelper helper = new DatabaseHelper(this);
    database = helper.getWritableDatabase();
    Cursor data = database.query("castr", fields, null, null, null, null, null);
    dataSource = new SimpleCursorAdapter(this, R.layout.row, data, fields, new int[] {R.id.field1, R.id.field2});
    final ListView view = getListView();

    view.setHeaderDividersEnabled(true);
    view.addHeaderView(getLayoutInflater().inflate(R.layout.row, null));

    database.close();

    view.setOnItemClickListener(new AdapterView.OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

            // Prepare intent
            Intent newActivity = new Intent(getApplicationContext(), NewActivity.class);
            newActivity.putExtra("itemId",view.getItemIdAtPosition(position));
            // start activity
            startActivity(newActivity);
          }
        });

}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:01:48+00:00Added an answer on May 28, 2026 at 3:01 am

    you should get the listView reference from the layout. probably use something like this..

    listView = (ListView)findViewById(R.id.nameofyourlistView);
    

    and then set listView to clickable.

    on the other hand, why do you want to set a list view to be clickable?, you need not do it, list items are clickable by default.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.