NetBeans下Java读卡器开发遇IndexOutOfBoundsException,如何检测卡连接?
Hey there, let's tackle your two questions one by one—first that annoying IndexOutOfBoundsException, then how to check if a smart card is connected.
解决
IndexOutOfBoundsException异常 This error pops up because you're trying to access the 0th element of an empty list (in your case, Collections$EmptyList). Here's how to fix it:
- Locate the problematic code: Look at your stack trace to find where you called
.get(0). Chances are you're trying to grab the first card terminal or card from a list that has no entries (e.g., no card reader connected, or no card inserted in the reader). - Add empty checks before accessing the list: Always verify the list isn't empty before trying to get elements from it. For example, if you're fetching card terminals:
import javax.smartcardio.*; import java.util.List; public class CardReaderExample { public static void main(String[] args) { try { TerminalFactory factory = TerminalFactory.getDefault(); List<CardTerminal> terminals = factory.terminals().list(); // Check if there are any available terminals first if (terminals.isEmpty()) { System.out.println("No card readers found!"); return; } CardTerminal terminal = terminals.get(0); // Proceed with card operations... } catch (CardException e) { e.printStackTrace(); } } }
- Check for card presence before proceeding: Even if you have a valid terminal, make sure a card is inserted before trying to interact with it using
terminal.isCardPresent().
判断智能卡是否已连接
There are a few reliable ways to check if a smart card is connected or present:
- Use
isCardPresent()for quick checks: This method returns a boolean indicating if a card is physically present in the terminal:
if (terminal.isCardPresent()) { System.out.println("Card is inserted!"); } else { System.out.println("No card in the reader."); }
- Attempt to connect and handle exceptions: Trying to connect to the card will throw a
CardExceptionif no card is present (or if it can't be accessed). This is a more definitive check:
Card card = null; try { // Connect using any available protocol ("*" means auto-detect) card = terminal.connect("*"); System.out.println("Successfully connected to the card!"); } catch (CardException e) { System.out.println("Card not connected or inaccessible: " + e.getMessage()); } finally { // Always disconnect to free resources if (card != null) { try { card.disconnect(false); } catch (CardException e) { e.printStackTrace(); } } }
- Listen for real-time card events: If you need to detect when a card is inserted or removed, use
waitForCardPresent()orwaitForCardAbsent(). Pass0to wait indefinitely until the state changes:
try { System.out.println("Waiting for card to be inserted..."); terminal.waitForCardPresent(0); // Blocks until card is inserted System.out.println("Card inserted!"); // Wait for card removal terminal.waitForCardAbsent(0); System.out.println("Card removed!"); } catch (CardException e) { e.printStackTrace(); }
内容的提问来源于stack exchange,提问作者YurvanAodi
相关产品推荐
相关产品推荐

