0

I've been testing my app in Android Studio emulator through my Samsung S7 phone (API 23 and 6.0.1) and it works fine. When I unplug it from my laptop and run the app it crashes.

My logcat says that I am trying to call a null array... but it isn't null? I don't understand why when I run the app through Android Studio to my phone everything works fine but when I run it alone it crashes. Thankful for any help!

Here is my logcat

FATAL EXCEPTION: main
Process: com.example.abc.def, PID: 28693
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abc.def/com.example.abc.def.MainActivity}: java.lang.NullPointerException: Attempt to read from null array
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to read from null array
at com.example.abc.def.MainActivity.onCreate(MainActivity.java:49)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

And here is my code. The issue arises at the bottom when I call btn1.setText(itemArray[0]) because the array is "null"

public class MainActivity extends AppCompatActivity {

Button btn1, btn2;
String btn1Name, btn2Name;
String btnValue = "", itemValue = "", itemPosition = "";
String[] buttonArray = new String[]{"blank", "blank"};
String[] itemArray = new String[]{"blank", "blank"};
String[] positionArray = new String[]{"blank", "blank"};  

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn1 = (Button) findViewById(R.id.button1);
    btn2 = (Button) findViewById(R.id.button2);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        btnValue = extras.getString("btnValue");
        itemValue = extras.getString("itemValue");
        itemPosition = extras.getString("itemPosition");

        buttonArray = extras.getStringArray("buttonArray");
        itemArray = extras.getStringArray("array");
        positionArray = extras.getStringArray("positionArray");

        if (btnValue != null && btnValue.equals("btn1")){
            buttonArray[0] = btnValue;
            itemArray[0] = itemValue;
            positionArray[0] = itemPosition;
        } else if (btnValue != null && btnValue.equals("btn2")) {
            buttonArray[1] = btnValue;
            itemArray[1] = itemValue;
            positionArray[1] = itemPosition;
        }
    }

    btn1.setText(itemArray[0]);
    btn2.setText(itemArray[1]);
}
}
Pam
  • 73
  • 9
  • What is being returned by "extras.getString("itemValue")" & "itemArray = extras.getStringArray("array");"? – Adeel Shahzad Nov 25 '16 at 05:32
  • @AdeelShahzad it's from another activity. I don't think it would have anything to do with my code though since "extras" should be null when I launch the app. itemArray[0] and [1] is already assigned to "blank" at the launch of the activity. – Pam Nov 25 '16 at 05:42
  • Can you please post the other class code where you are passing these extras? – Adeel Shahzad Nov 25 '16 at 05:44
  • @AdeelShahzad Just added it. Thanks – Pam Nov 25 '16 at 05:52
  • It's just my opinion that "extras.getStringArray("array");" is returning null. I can be wrong, though. Place a breakpoint on that statement and evaluate that expression – Adeel Shahzad Nov 25 '16 at 06:08

1 Answers1

1

I'm guessing here, but is MainActivity the first activity in the project?

I think you wrote the code to handle returning to this activity with data from other activities. I just tried and getIntent().getExtras() returns null when the activity is first created on an emulator. However, when I ran it on my device, getIntent generated this exception.

Note that onCreate is called once, so if you want to see the results of another activity, take a look at How to manage `startActivityForResult` on Android?.

Community
  • 1
  • 1
Roy Falk
  • 1,685
  • 3
  • 19
  • 45