0

I am trying to access an ArrayList of Integers from a previous class that was set in the previous view however it doesn't seem to be saving the arrayList correctly when I go to the new view I think it deletes whatever was there?

Is there anyway to access the "lightInfo" that was already set?

ConnectTest declaration

public class ConnectTest extends MainActivity {
    TextView out;
    private static final int REQUEST_ENABLE_BT = 1;
    private BluetoothAdapter btAdapter = null;
    private BluetoothSocket btSocket = null;
    private OutputStream outStream = null;

    // Well known SPP UUID
    private static final UUID MY_UUID =
            UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    // Insert your server's MAC address
    private static String address = "A4:5E:60:EB:3C:E8";
    ArrayList<Integer> temp = ((MainActivity) getApplicationContext()).lightInfo;

Call to temp that creates the error:

 String finalMessage = "";
        for (int i = 0; i < temp.size(); i++) {
            finalMessage = finalMessage + temp.get(i) + "\n";
        }

        String message = finalMessage;

MainActivity declaration

public class MainActivity extends AppCompatActivity {


    private final static int REQUEST_ENABLE_BT = 1;
    UUID myUUID = UUID.randomUUID();
    private static final int DISCOVER_DURATION = 300;
    private static final int REQUEST_BLU = 1;
    ArrayAdapter<String> mArrayAdapter;
    ArrayList<Double> currentBodyTemp;
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    boolean targetTimeZoneIsAhead = false;
    boolean targetTimeZoneIsBehind = false;

    int currentWakeTime = 420;
    int currentSleepTime = 1320;

    int targetWakeTime = 0;
    int targetSleepTime = 0;
    int daysLeft = 0;
    int timeDifference = 0;

    public ArrayList<Integer> lightInfo;

Error Message:

10-27 12:59:56.141 15803-15803/com.teamfara.circadianrhythmmonitor4 D/AndroidRuntime: Shutting down VM
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: FATAL EXCEPTION: main
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: Process: com.teamfara.circadianrhythmmonitor4, PID: 15803
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.teamfara.circadianrhythmmonitor4/com.teamfara.circadianrhythmmonitor4.ConnectTest}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2964)
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at android.app.ActivityThread.access$1000(ActivityThread.java:198)
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:6837)
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:111)
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at com.teamfara.circadianrhythmmonitor4.ConnectTest.<init>(ConnectTest.java:34)
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at java.lang.reflect.Constructor.newInstance(Native Method)
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at java.lang.Class.newInstance(Class.java:1684)
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at android.app.Instrumentation.newActivity(Instrumentation.java:1080)
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2954)
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at android.app.ActivityThread.access$1000(ActivityThread.java:198) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:6837) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
10-27 12:59:56.151 15803-15803/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 

thank you!

2 Answers2

0

You can not simply typecast any object and expect it to work. Please use proper data passing mechanisms. Try this:

Passing data through Intent and receiving it

Community
  • 1
  • 1
Ankur Aggarwal
  • 2,210
  • 2
  • 34
  • 39
0

your app is crashing because your are trying to access getApplicationContext in the global scope of your Activity while, this latter didn't go through its lifecycle. Since your ConnectTest extends your MainActivity you don't any cast to access any not-private member of MainActivity. You only need to mark is as public or protected. Even the default access specifier is good if the two classes are in the same package. Have a look here

Blackbelt
  • 156,034
  • 29
  • 297
  • 305