java.lang.NullPointerException 比较蓝牙和 Parse.com table

java.lang.NullPointerException on comparing bluetooth and Parse.com table

我有一个应用程序 returns 如果它发现蓝牙 mac 地址与 Parse.com 用户 table.

然而它returns 空指针异常,我不知道为什么。在 table 中,8 位用户中有 2 位上传了他们的 mac 地址。

蓝牙处理比较class;

public class BLETestActivity extends FragmentActivity {
    private TextView out;
    private ParseUser currentUser = ParseUser.getCurrentUser();
    private String username;    Context context;
    private BroadcastReceiver mReceiver;
    private BluetoothDevice devicenew;
    private ArrayList<String> list;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ble2);

        out = (TextView) findViewById(R.id.out);

        // Getting the Bluetooth adapter
        final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        //List<String> items = new ArrayList<String>();

        //final ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
        out.append("\nAdapter: " + adapter);
        String device_bt = adapter.getAddress();

        // Check for Bluetooth support in the first place
        // Emulator doesn't support Bluetooth and will return null
        if(adapter==null) {
            out.append("\nBluetooth NOT supported. Aborting.");
            return;
        }

        // Starting the device discovery
        out.append("\nStarting discovery...");

        adapter.startDiscovery();
        //FINDER NYE ENHEDER
        mReceiver = new BroadcastReceiver()

        {

            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();

                //Finding devices
                if (BluetoothDevice.ACTION_FOUND.equals(action))
                {
                    // Get the BluetoothDevice object from the Intent
                    devicenew = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    // Add the name and address to an array adapter to show in a ListView
                    System.out.print(devicenew.getAddress());
                    Log.d("Bluetooth","jeg når her til");
                    out.append("\nDevices Pared:");
                    Set<BluetoothDevice> devices = adapter.getBondedDevices();
                    list = new ArrayList<>();
                    if(!list.contains(devicenew.getAddress())) {
                        list.add(devicenew.getAddress());
                    }
                    for (BluetoothDevice device : devices) {
                        out.append("\nFound device: " + device + devicenew);
                    }
                    out.append("\nDone with discovery...");

                }
            }
        };
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, filter);

        // Listing paired devices

        currentUser.put("BT_ID", device_bt);
        currentUser.saveInBackground();
        context = getApplicationContext();


        final ParseQuery<ParseUser> queryParseUser = ParseUser.getQuery();
        queryParseUser.findInBackground(new FindCallback<ParseUser>()
        {
            @Override
            public void done(List<ParseUser> BTList, ParseException arg1) {
                // TODO Auto-generated method stub
                Log.d("Parse","we made it this far");

                if (BTList != null && arg1 == null) {
                    for (ParseUser parseObject : BTList) {
                        if(parseObject.getString("BT_ID") != null){
                        for(String string : list) {
                            if(string.equals(parseObject.getString("BT_ID"))) {
                                String BTuser = parseObject.getString("BT_ID");
                                Toast toast = Toast.makeText(context, "Wow we found" + BTuser, Toast.LENGTH_SHORT);
                                toast.show();
                            }
                        }
                        }
                    }
                } else {
                    Log.d("Bluetooth", "Fejl i returnering af data: " + arg1.toString());
                }
            }
        });
    }


}

更具体地说,我在这里得到了空指针;

                for(String string : list) {
                    if(string.equals(parseObject.getString("BT_ID"))) {
                        String BTuser = parseObject.getString("BT_ID");
                        Toast toast = Toast.makeText(context, "Wow we found" + BTuser, Toast.LENGTH_SHORT);
                        toast.show();
                    }
                }

我检查时不明白为什么它 returns 为空?

我的堆栈跟踪;

java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Iterator java.util.ArrayList.iterator()' on a null object reference
        at com.example.nan.spymapv2.BLETestActivity.done(BLETestActivity.java:113)
        at com.example.nan.spymapv2.BLETestActivity.done(BLETestActivity.java:104)
        at com.parse.ParseTaskUtils.run(ParseTaskUtils.java:115)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

您的 list 变量为空,因此在传递给 for(...) 循环时会抛出错误。这里有几个选项:您可以在声明它的地方初始化空列表,所以

private ArrayList<String> list;

会变成

private ArrayList<String> list = new ArrayList<>();

或者您可以将 list 添加到空检查中,因此

if (BTList != null && arg1 == null) {

会变成

if (BTList != null && arg1 == null && list != null) {

或者在尝试遍历 list

之前的某个时间将其设为单独的 if 语句