-1

I am brand new to Java and was given this piece of code, no explanation just figure it out. Im getting the basics but Im not sure why the error is in place. Im getting

java.lang.arrayindexoutofboundsexception:2
       at prealert.listener.<init>.(Listener.Java:26)
       at prealert.listener.main(Listener.Java:40)

Thanks in advance for any help.

 package prealert;

    import org.apache.log4j.Logger;
    import org.apache.log4j.PropertyConfigurator;

    import jpcap.JpcapCaptor;
    import jpcap.NetworkInterface;

    public class Listener {

        private NetworkInterface[] devices;
        private NetworkInterface deviceName;
        private Reciever reciever;
        private JpcapCaptor jpcap;
        public static Logger log;


        public Listener() {
            PropertyConfigurator.configure("log4j.properties");
            log = Logger.getRootLogger();
            log.debug("Log4j has been initialized");
            devices = JpcapCaptor.getDeviceList();
            for (int i = 0; i < devices.length; i++) {
                log.info(devices[i].description);
            }
            deviceName = devices[2];
            reciever = new Reciever();
            try {
                jpcap = JpcapCaptor.openDevice(deviceName, 2000, true, 100);
            } catch (Exception e) {
                log.error("Error with JPcapCreation", e);
            }
            reciever.jpcap = jpcap;
            reciever.start();
            new SetBoard(SetBoard.DEFAULT).start();
        }

        public static void main(String args[]) {
            try {
                new Listener();
            } catch (Exception e) {
                log.error("ERROR IN THE MAIN METHOD OF LISTENER!", e);
            }
        }
    }
PGFDBUG
  • 47
  • 1
  • 7
  • possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Raedwald Apr 01 '15 at 22:31

5 Answers5

3

Take a look at

deviceName = devices[0];

If there are no devices, then this will fail with the exception you see.

Mike Samuel
  • 109,453
  • 27
  • 204
  • 234
  • Im sorry that was a misprint on my part I corrected that earlier I place [2] and am still getting the same error – PGFDBUG Sep 06 '11 at 23:37
  • If there isn't even 1 device, then there won't be 3 either. – Mike Samuel Sep 07 '11 at 00:05
  • I have 2 devices present and it list both of them then it throws the error – PGFDBUG Sep 07 '11 at 01:20
  • 1
    Arrays are [zero indexed](http://en.wikipedia.org/wiki/Zero-based_numbering). If the length is `2`, then you can access `devices[0]`, the first device, and `devices[1]`, the second device. `devices[2]` is out of bounds. – Mike Samuel Sep 07 '11 at 01:32
  • Thank you for reminding me to fix my dumb mistakes I always forget to account for O when counting. Thanks a million – PGFDBUG Sep 07 '11 at 18:55
0

I assume that the line in error is: deviceName = devices[0]; which would because devices = JpcapCaptor.getDeviceList(); returns an array with no elements.

Assuming that is true you need to figure out why the array is empty (since you are assuming it will have at least one element) or cope with the fact that it doesn't have any elements and add an if(devices.length > 0) before using it.

TofuBeer
  • 58,140
  • 15
  • 111
  • 160
0

I think the problem is in the for loop in the middle. Although the code itself looks fine, you may want to check that devices has something in it first

kennypu
  • 5,599
  • 2
  • 20
  • 28
0
 deviceName = devices[0];

You need to make sure that devices[0] is an element of that array, because else you will get an index out of bound exception. You could check that by using devices.length

Alfred
  • 56,245
  • 27
  • 137
  • 181
0

You have to make sure you have at least 3 elements in your array when you call devices[2], maybe your getDeviceList() return an array with size of 0..

Jeremy D
  • 4,589
  • 1
  • 26
  • 36