Home Ask Login Register

Developers Planet

Your answer is one click away!

Naz_Jnr February 2016

Unable to get Integer Value of Variable Stored in Java Thread Local Object

I am trying to get the integer value of a Thread Local Object as in the code below, but it keeps throwing an error. However, when I try to display it as a string, it displays.

My Question: How can I achieve extracting the Integer value?

Note: Parsing the string as in the code below doesn't work

package ids;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Random;
import javax.swing.SwingWorker;
import jpcap.*;
import jpcap.packet.Packet;

public class InterfaceThread implements Runnable {

ThreadLocal MyInterfaceNumber = new ThreadLocal();

InterfaceThread() {


InterfaceThread(int InterfaceNumber) {
    System.out.println("Check: " + MyInterfaceNumber.get());

public void run() {
    String IntNum = (String) MyInterfaceNumber.get();

    int InterfaceToPrint = Integer.parseInt(IntNum);
    System.out.println("Interface: " + InterfaceToPrint);
    Messages msg = new Messages();
    Printer print = new Printer();

    try {

//Open Selected Interface     
        JpcapCaptor captor =         JpcapCaptor.openDevice(IDS.Interfaces[InterfaceToPrint], 65535, true, 5000);
        System.out.println("Interface Dlink Name: " + IDS.Interfaces[InterfaceToPrint].description);
        //  print.TextAreaAppend("Traffic on Interface "+InterfaceToPrint+" Now Being Sniffed\n");
        while (MainFrame.StopSniffing == false) {
            captor.processPacket(-1, new Printer());
        //captor.loopPacket(-1, new Printer());

    } catch (IOException ex) {
              msg.ErrorMessages("Interface " + InterfaceToPrint + " Encountered    Error: " + ex);


public void Start(int InterfaceNumber) {

    InterfaceThread interfaceT = new InterfaceThread(InterfaceNumber);

    Thread thread = new Thread(interfaceT);





Peter Lawrey February 2016

I suggest you use generics to simplify the code

private final ThreadLocal<Integer> int = new ThreadLocal<>();

// to read
Integer i = int.get();

// to write

Post Status

Asked in February 2016
Viewed 3,806 times
Voted 7
Answered 1 times


Leave an answer

Quote of the day: live life