null pointer exception with jpcap

class Thread1 extends Thread

{

public void run()

{

try

{

String[] devices=Jpcap.getDeviceList();

String val="";

for(int j=0;j<50;j++)

{

System.out.println(devices[j]);

val=val+devices[j];

}

Jpcap jpcap=Jpcap.openDevice(val,10000,false,20);

jpcap.loopPacket(-1,new PacketSniff());

}

catch(Exception e1)

{

pane1.showMessageDialog(f1,"Error occured");

}

}

//

A simple program to capture packets and display the drivers on clicking a button on the gui.A null pointer exception is raised when the device array is accessed(the array is not empty).

[684 byte] By [java_blaa] at [2007-11-27 2:25:25]
# 1
hi!you should not run the loop till 50, means static value. instead you run the loop till devices.length. and please do not forget to use the code formatting tag.RegardsAniruddha
Aniruddha-Herea at 2007-7-12 2:33:42 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2
thnks a lot for the immediate reply.The problem seems to be with the device array.The network card is not being detected either.We altered the code to include devices.length,the error still exists.
java_blaa at 2007-7-12 2:33:42 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 3

here is the full class

On clicking "sniff" button in the main window control comes to this class..

--

import javax.swing.*;

import javax.swing.table.*;

import java.awt.event.*;

import java.awt.*;

import javax.swing.event.*;

import jpcap.*;

public class AllProtocols extends JFrame implements ActionListener

{

JButton bstart = new JButton("START SNIFF");

JButton bpause = new JButton("PAUSE SNIFF");

JButton bclose = new JButton("CLOSE");

DefaultTableModel model = new DefaultTableModel();

JTable table = new JTable(model);

JScrollPane pane=new JScrollPane();

Container con;

Thread1 th=new Thread1();

boolean SniffStatus = true;

String st;

JOptionPane pane1=new JOptionPane();

JFrame f1;

AllProtocols()

{

con = getContentPane();

pane=new JScrollPane(table);

f1=this;

JPanel p1=new JPanel(new FlowLayout());

p1.add(bstart);

p1.add(bpause);

p1.add(bclose);

con.add(p1,"South");

con.add(pane);

model.addColumn("SourceIP");

model.addColumn("DestinationIP");

model.addColumn("SourcePort");

model.addColumn("DestPort");

model.addColumn("Protocol");

bstart.addActionListener(this);

bpause.addActionListener(this);

bclose.addActionListener(this);

setSize(450,250);

setTitle("ALL PROTOCOLS");

show();

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource() == bstart)

{

th.start();

bstart.setEnabled(false);

}

if(e.getSource() == bpause)

{

if(bpause.getText().equals("Pause Sniff"))

{

bpause.setText("Continue Sniff..");

SniffStatus = false;

}

else

{

bpause.setText("Pause Sniff");

SniffStatus = true;

}

}

if(e.getSource() == bclose)

{

SniffStatus=false;

dispose();

}

}

class PacketSniff implements JpcapHandler

{

public void handlePacket(Packet pac)

{

try

{

if(SniffStatus)

{

IPPacket ip = (IPPacket)pac;

IPAddress sadr = ip.src_ip;

IPAddress dadr = ip.dst_ip;

int proto = (int)ip.protocol;

if(proto == 17)

{

st="UDP";

String sip = sadr.getHostAddress();

String dip = dadr.getHostAddress();

UDPPacket up = (UDPPacket)pac;

String sport = "" + up.src_port;

String dport = "" + up.dst_port;

model.addRow(new Object[]{sip,dip,sport,dport,st});

}

else if(proto == 2)

{

st="IGMP";

String sip = sadr.getHostAddress();

String dip = dadr.getHostAddress();

TCPPacket tp = (TCPPacket)pac;

String sport = "" + tp.src_port;

String dport = "" + tp.dst_port;

model.addRow(new Object[]{sip,dip,sport,dport,st});

}

else if(proto == 1 )

{

st="ICMP";

String sip = sadr.getHostAddress();

String dip = dadr.getHostAddress();

TCPPacket tp = (TCPPacket)pac;

String sport = "" + tp.src_port;

String dport = "" + tp.dst_port;

String seq="" +tp.sequence;

model.addRow(new Object[]{sip,dip,sport,dport,st});

}

else if(proto == 6)

{

st="TCP";

String sip = sadr.getHostAddress();

String dip = dadr.getHostAddress();

TCPPacket tp = (TCPPacket)pac;

String sport = "" + tp.src_port;

String dport = "" + tp.dst_port;

String seq="" +tp.sequence;

model.addRow(new Object[]{sip,dip,sport,dport,st});

}

}

else

return;

}

catch(Exception e)

{

pane1.showMessageDialog(f1,"Error occured");

}

}

}

class Thread1 extends Thread

{

public void run()

{

try

{

String[] devices=Jpcap.getDeviceList();

String val="";

for(int j=0;j<50;j++)

{

val=val+devices[j];

}

Jpcap jpcap=Jpcap.openDevice(val,10000,false,20);

jpcap.loopPacket(-1,new PacketSniff());

}

catch(Exception e1)

{

pane1.showMessageDialog(f1,"Error occured");

}

}

}

}

Message was edited by:

java_bla

java_blaa at 2007-7-12 2:33:42 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 4

hi!

you didn't followed what i requested. i requested you to use the code formatting tag. and another thing is.. in your last post you are still using the

for(int j=0;j<50;j++)

:(

please until and unless you follow the requests it won't be possible to solve your problem.

regards

Aniruddha

Aniruddha-Herea at 2007-7-12 2:33:42 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...