-1

Dear JAVA Programmers,

I have come to an NullPointerException in the onCreate method when opening one of my activities. This is what my onCreate method looks like:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    overridePendingTransition(R.anim.push_left_in, R.anim.fadeout);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.category_page);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);

    // get intent data
    Intent i = getIntent();

    // Selected category id
    int position = i.getExtras().getInt("id");
    Categories categories = new Categories();
    Categories.CategoriesFromDatabase textAdapter1 = categories.new CategoriesFromDatabase(this);

    TextView pageTitle = (TextView) findViewById(R.id.category_page_title);
    if (textAdapter1.mCatIds[position] == null){
        pageTitle.setText("error parsing pagename");
    } else {
        pageTitle.setText(textAdapter1.mCatIds[position]);
    }  

    ListView pageContent = (ListView) findViewById(R.id.category_page_content);
    pageContent.setAdapter(new LinksPerCategory(this));

    pageContent.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            // Sending image id to FullScreenActivity
            Intent i = new Intent(getApplicationContext(), viewPageItem.class);
            // passing array index
            i.putExtra("id", position);
            startActivity(i);
        }

    });


}

The main purpose of this activity is to get the category's name, 'mCatIds[position]', from the past activity and put it on top of the current. I suspected that the NullPointerException is given here, therefore I put the if statement around the lines, but with no effect. Since the past activity displays content, 'mCatIds[position]' is likely to have a value. Could the problem be anywhere else? Or have I missed something important?

I am looking forward to your response.

EDIT: Here is the stacktrace, sorry for not posting it directly:

05-27 08:37:41.845: E/AndroidRuntime(31413): FATAL EXCEPTION: main
05-27 08:37:41.845: E/AndroidRuntime(31413): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.example.abnwiki/com.example.abnwiki.viewCategoryPage}:     java.lang.NullPointerException
05-27 08:37:41.845: E/AndroidRuntime(31413):    at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1996)
05-27 08:37:41.845: E/AndroidRuntime(31413):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2023)
05-27 08:37:41.845: E/AndroidRuntime(31413):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
05-27 08:37:41.845: E/AndroidRuntime(31413):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1174)
05-27 08:37:41.845: E/AndroidRuntime(31413):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-27 08:37:41.845: E/AndroidRuntime(31413):    at android.os.Looper.loop(Looper.java:137)
05-27 08:37:41.845: E/AndroidRuntime(31413):    at android.app.ActivityThread.main(ActivityThread.java:4503)
05-27 08:37:41.845: E/AndroidRuntime(31413):    at java.lang.reflect.Method.invokeNative(Native Method)
05-27 08:37:41.845: E/AndroidRuntime(31413):    at java.lang.reflect.Method.invoke(Method.java:511)
05-27 08:37:41.845: E/AndroidRuntime(31413):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
05-27 08:37:41.845: E/AndroidRuntime(31413):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
05-27 08:37:41.845: E/AndroidRuntime(31413):    at dalvik.system.NativeStart.main(Native Method)
05-27 08:37:41.845: E/AndroidRuntime(31413): Caused by: java.lang.NullPointerException
05-27 08:37:41.845: E/AndroidRuntime(31413):    at com.example.abnwiki.viewCategoryPage.onCreate(viewCategoryPage.java:38)
05-27 08:37:41.845: E/AndroidRuntime(31413):    at android.app.Activity.performCreate(Activity.java:4479)
05-27 08:37:41.845: E/AndroidRuntime(31413):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
05-27 08:37:41.845: E/AndroidRuntime(31413):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1960)
05-27 08:37:41.845: E/AndroidRuntime(31413):    ... 11 more
  • Look at your LogCat window for more information on the exception location. – Nate May 26 '13 at 21:14
  • It would help immensely if you let us know where exactly, i.e. in which line, the `NullPointerException` occurs... – jenzz May 26 '13 at 21:28
  • I think there is a typo here: Categories.CategoriesFromDatabase textAdapter1 = categories.new CategoriesFromDatabase(this); – Daniel Conde Marin May 26 '13 at 21:31

2 Answers2

1

Yes, give us your stacktrace of your exception!

Without seeing your stacktrace, I think thats your problem:

int position = i.getExtras().getInt("id");

i.getExtras() returns null !!!

Luser_k
  • 21
  • 1
0

The first time your activity starts, it's not called by an intent. Hence getIntent() will return a NullPointerException. You can overcome this with either

if(getIntent()!=null)
Intent i=getIntent();

Or you could use the function onNewIntent() and set the current activity as singleTop.

onNewIntent() performs a task every time an intent calls the activity. It also makes sure that there's only ONE instance of the activity if you include singleTop.

Karthik Balakrishnan
  • 4,353
  • 6
  • 37
  • 69