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]);
}
}