0

I got an error when I try to run my application. I hope someone can point me in the right direction

Here is where my problem starts(Line 73):

USER_ID_TMP = userIdtext2.getText().toString();

Longer code snippet:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab1);
    Log.i("tabs", "CurrentTab is: 1");

    DB_NAME_AND_PATH = Tab1.this.getDatabasePath(DB_NAME);
    Log.d("Haze", "DB_NAME_AND_PATH: "+DB_NAME_AND_PATH);

    listView = (ListView) findViewById(R.id.listViewer);
    Button copyDbButton = (Button)findViewById(R.id.copydbbutton);
    Button readDbButton = (Button)findViewById(R.id.readdbbutton);
    final EditText userIdtext= (EditText) findViewById(R.id.userIDtextBox);
    EditText userIdtext2= (EditText) findViewById(R.id.userIDtextBox);

    if(fileFound){
        Log.d("Haze", "Filefound...........");
      readDbButton.setEnabled(false);
    }else{
      Log.d("Haze", "FileNotfound...........");
      fileFound = checkFileOnCreate(DB_NAME_AND_PATH);
      readDbButton.setEnabled(true);
    }

    USER_ID_TMP = userIdtext2.getText().toString();
    Log.d("Haze", "USER_ID_TMP: "+USER_ID_TMP);
    USER_ID = USER_ID_TMP.toUpperCase();

    final TextWatcher watcher= new TextWatcher() {
      public void afterTextChanged(Editable s) {
            if (userIdtext.getText().toString().equals("")) {
              userIdtext.setBackgroundColor(getResources().getColor(android.R.color.white));
        userIdtext.setTextColor(getResources().getColor(android.R.color.black));
            }

Error message:

05-13 03:43:08.778    2079-2079/com.androidexample.tabbar D/Haze﹕ FileNotfound...........
05-13 03:43:08.800    2079-2079/com.androidexample.tabbar D/AndroidRuntime﹕ Shutting down VM
05-13 03:43:08.800    2079-2079/com.androidexample.tabbar W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x409961f8)
05-13 03:43:08.828    2079-2079/com.androidexample.tabbar E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidexample.tabbar/com.androidexample.tabbar.TabBar}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidexample.tabbar/com.androidexample.tabbar.Tab1}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
            at android.app.ActivityThread.access$600(ActivityThread.java:122)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4340)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidexample.tabbar/com.androidexample.tabbar.Tab1}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
            at android.app.ActivityThread.startActivityNow(ActivityThread.java:1796)
            at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
            at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
            at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:682)
            at android.widget.TabHost.setCurrentTab(TabHost.java:346)
            at android.widget.TabHost.addTab(TabHost.java:236)
            at com.androidexample.tabbar.TabBar.onCreate(TabBar.java:43)
            at android.app.Activity.performCreate(Activity.java:4465)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
            at android.app.ActivityThread.access$600(ActivityThread.java:122)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4340)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.androidexample.tabbar.Tab1.onCreate(Tab1.java:73)
            at android.app.Activity.performCreate(Activity.java:4465)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
            at android.app.ActivityThread.startActivityNow(ActivityThread.java:1796)
            at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
            at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
            at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:682)
            at android.widget.TabHost.setCurrentTab(TabHost.java:346)
            at android.widget.TabHost.addTab(TabHost.java:236)
            at com.androidexample.tabbar.TabBar.onCreate(TabBar.java:43)
            at android.app.Activity.performCreate(Activity.java:4465)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
            at android.app.ActivityThread.access$600(ActivityThread.java:122)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4340)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)
8
  • Is that EditText and others in tab1.xml?
    – Hariharan
    May 13, 2014 at 4:07
  • can you please post your activity whole code ? May 13, 2014 at 4:08
  • 1
    May be, userIdtext2.getText() value is null. and null.toString() referenced by userIdtext2.getText().toString() returns the NPE.
    – ngrashia
    May 13, 2014 at 4:09
  • final EditText userIdtext= (EditText) findViewById(R.id.**userIDtextBox**); EditText userIdtext2= (EditText) findViewById(R.id.**userIDtextBox**); Same Id ???
    – akash
    May 13, 2014 at 4:22
  • I think you have copied line EditText userIdtext= (EditText) findViewById(R.id.userIDtextBox); May 13, 2014 at 4:27

4 Answers 4

1

you have used tab1 layout to set view by using setContentView(R.layout.tab1);

And initize userIdtext2 by using id userIDtextBox from this line EditText userIdtext2= (EditText) findViewById(R.id.userIDtextBox);

Then you take value of userIdtext2 by following code

USER_ID_TMP = userIdtext2.getText().toString();

so check if the id is R.id.userIDtextBox is available in layout R.layout.tab1 or not ?

1
  • Hi and thanks for your input. Of course the name of this EditText was not "userIDtextBox" ....copy paste error....name was "edittextbox" ...stupid nameing of me:) Thanks again May 13, 2014 at 17:11
0

Check if your userIdtext2.getText() is null or not. If it is not null, then use userIdtext2.getText().toString()

Because using toString() on null object is known to cause NPE

Reference : Convert null object to String

0

I guess your problem is in this line

USER_ID = USER_ID_TMP.toUpperCase();

because user_id_temp contains nothing so when you try to convert it to upper case, since it contain nothing that's why it throws exception.Try it with some inital input or comment it and then try.

0

Update your EditText Id's (Here both are same)

 final EditText userIdtext= (EditText) findViewById(R.id.userIDtextBox1);
 EditText userIdtext2= (EditText) findViewById(R.id.userIDtextBox2);

May be other issue in line USER_ID_TMP = userIdtext2.getText().toString();

Here nothing found in getText();

So first you have to setText("");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.