简体   繁体   中英

NullPointerException: .... virtual method 'android.bluetooth.le.ScanRecord android.bluetooth.le.ScanResult.getScanRecord()' on a null object reference

I'm using the function getScanRecord in this line of code:

if (!(this.mScanResult.getScanRecord().getServiceData() == null || this.mScanResult.getScanRecord().getServiceData().size() == 0)) {
    advertisementData = advertisementData + "Service Data: " + this.mScanResult.getScanRecord().getServiceData().toString().replace('{', ' ').replace('}', ' ') + "\n";
}

And it gives me this error in Android Studio:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.catu.catucomm/com.catu.catucomm.Second}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.le.ScanRecord android.bluetooth.le.ScanResult.getScanRecord()' on a null object reference

Based on your error message, I suspect that your 'mScanResult' variable is empty . Check whether your 'mScanResult' is empty or not at that moment of code.

Try this.

    /*Listener sur le btn Pair*/
    mPairBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,Second.class);
                intent.putExtra(EXTRAS_SCAN_RESULT, -YOU NEED TO PUT YOUR SCAN RESULT HERE- );
                startActivity(intent);
                finish();
        }
    });

You need to pass your scan result object to the second activity with this putExtra() function.

I Have soved the problem by initializing the variable like this :

private ScanResult mScanResult;
public Second(ScanResult mScanResult) 
{
       this.mScanResult = mScanResult;
}

but now i have the new problem :

java.lang.InstantiationException: java.lang.Class<com.catu.catucomm.Second> has no zero argument constructor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM