Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
NoSuchElementException using Iterator class with HashSet

I don't udnerstand why this code is giving me a NoSuchElementException

Essentially what I'm doing is iterating across a HashSet of Gatt servers and closing their connections with a device if their device addresses match.

BluetoothDevice device = mDeselectedDeviceData.device;
Iterator<BluetoothGatt> it = BleManager.getInstance().myGattConnections.iterator();
while (it.hasNext()) {
    if(it.next().getDevice().getAddress() == device.getAddress()){ 
        it.next().close();
    }
}

This line

if(it.next().getDevice().getAddress() == device.getAddress()){ 

is throwing the error

>NoSuchElementException

However, it.hasNext() has is true if I log it

while (it.hasNext()) {
    Log.(TAG,"it.hasNext() is "+String.valueOf(it.hasNext())); // Prints true
    if(it.next().getDevice().getAddress() == device.getAddress()){ 
        it.next().close();
    }
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

if(it.next().getDevice().getAddress() == device.getAddress()){ 
    it.next().close();
}

You call it.next() twice here, and it advances the iterator two positions. I'm pretty sure what you mean here is

BluetoothGatt gatt = it.next();
if(gatt.getDevice().getAddress() == device.getAddress()){ 
    gatt.close();
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!