NullpointerException when opening a tabhost activity

I am working on an application where i moving from list menu to tabhost activity ,that is

Menu.class -> Recharge.class where Recharge.class is a TabActivity having a tab named prepaid.class

I need to pass a string from menu.class to prepaid.class in the below code i passed the string please find the bug in my code and help me

Menu.class

    if(s.equals("Recharge"))
        {
            Intent i = new Intent(menu.this,recharge.class);
            i.putExtra("app",app);
            startActivity(i);

        }

Recharge.class

Intent i = this.getIntent();            
    final String app=(String)i.getStringExtra("app");
   Resources res = getResources(); 
    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec; 
    Intent intent;

    intent = new Intent().setClass(this, prepaid.class);
    intent.putExtra("app", app);

    //Prepaid
    spec = tabHost.newTabSpec("PREPAID").setIndicator("",
                      res.getDrawable(R.drawable.prepaid))
                  .setContent(intent);
    tabHost.addTab(spec);
    tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tabhost);

prepaid.class

Intent i=this.getIntent();
final String app=(String)i.getStringExtra("app");

This is the error in log:

09-10 14:37:44.038: E/AndroidRuntime(1172): FATAL EXCEPTION: main
09-10 14:37:44.038: E/AndroidRuntime(1172): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ezerecharge/com.example.ezerecharge.recharge}: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ezerecharge/com.example.ezerecharge.prepaid}: java.lang.NullPointerException
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.os.Looper.loop(Looper.java:123)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.ActivityThread.main(ActivityThread.java:3683)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at java.lang.reflect.Method.invokeNative(Native Method)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at java.lang.reflect.Method.invoke(Method.java:507)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at dalvik.system.NativeStart.main(Native Method)
09-10 14:37:44.038: E/AndroidRuntime(1172): Caused by: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ezerecharge/com.example.ezerecharge.prepaid}: java.lang.NullPointerException
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.widget.TabHost.setCurrentTab(TabHost.java:326)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.widget.TabHost.addTab(TabHost.java:216)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at com.example.ezerecharge.recharge.onCreate(recharge.java:33)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-10 14:37:44.038: E/AndroidRuntime(1172):     ... 11 more
09-10 14:37:44.038: E/AndroidRuntime(1172): Caused by: java.lang.NullPointerException
09-10 14:37:44.038: E/AndroidRuntime(1172):     at com.example.ezerecharge.prepaid.<init>(prepaid.java:22)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at java.lang.Class.newInstanceImpl(Native Method)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at java.lang.Class.newInstance(Class.java:1409)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
09-10 14:37:44.038: E/AndroidRuntime(1172):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)

Answers


You are calling getIntent in the constructor. You cannot use it before the onCreate is called.

Move your intitialization code from prepaid in prepaid.onCreate.

And start your classes names by capital letters. please.


Need Your Help

Alternative to ASP.NET Ajax framework in aspx front end

ajax wcf json

I'm developing some CRUD screens for retrieving/updating various items using .NET 3.5 and aspx forms. I am using a WCF service to connect to the back end which is Oracle. I want to use Ajax but I'd

Problem with Arduino and pySerial

python c arduino pyserial

I got a problem. I recently bought an Arduino Uno board. I tried to make something funny like controlling an input from my computer. I used Python with pySerial and the program is the following:

Where is “Run Tests” in monodevelop

c# nunit monodevelop

I have a solution already containing a library with NUnit tests. When I develop with Visual Studio, I normally run these tests with the nunit.exe binaries I have included in a tools\nunit folder in...

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.