总是提示java.lang.NullPointerException这个错误,什么原因呢
packagejpcap;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.St...
package jpcap;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Date;
import jpcap.PacketReceiver;
import jpcap.packet.ARPPacket;
import jpcap.packet.EthernetPacket;
import jpcap.packet.Packet;
import jpcap.packet.TCPPacket;
public class jpcaplisten implements PacketReceiver {
Connection conn=null;
Statement stmt;
public void receivePacket(Packet packet) {
EthernetPacket ep=(EthernetPacket) packet.datalink;
if(packet instanceof TCPPacket){
TCPPacket tcpIp=(TCPPacket)packet;
EthernetPacket ip=(EthernetPacket)tcpIp.datalink;
System.out.println("源MAC---->"+ip.getSourceAddress()+"目的MAC---->"+ip.getDestinationAddress());
}
try{
String driver = "com.mysql.jdbc.Driver";
String database = "ymsdata";
String user = "root";
String password = "0000";
String url = "jdbc:mysql://localhost:3406/"+database+"?user="+user+"&password="+password;
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url);
Statement stmt=conn.createStatement();
EthernetPacket ip = null;
String sql="insert into r values('"+ip.getSourceAddress()+"','"+ip.getDestinationAddress()+"')";
int executeUpdate = stmt.executeUpdate(sql);
stmt.close();
conn.close();
}catch(Exception ex){
System.out.println(ex);
System.exit(0);
}
}
public static void main(String[] args) {
try{
NetworkInterface[] ni=JpcapCaptor.getDeviceList();
System.out.println("当前设备数目"+ni.length);
int Index=1;
JpcapCaptor jpc=JpcapCaptor.openDevice(ni[Index], 65535, true, 20000);
jpc.loopPacket(-1, new jpcaplisten());
}catch(Exception e){
e.printStackTrace();
}
}
} 展开
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Date;
import jpcap.PacketReceiver;
import jpcap.packet.ARPPacket;
import jpcap.packet.EthernetPacket;
import jpcap.packet.Packet;
import jpcap.packet.TCPPacket;
public class jpcaplisten implements PacketReceiver {
Connection conn=null;
Statement stmt;
public void receivePacket(Packet packet) {
EthernetPacket ep=(EthernetPacket) packet.datalink;
if(packet instanceof TCPPacket){
TCPPacket tcpIp=(TCPPacket)packet;
EthernetPacket ip=(EthernetPacket)tcpIp.datalink;
System.out.println("源MAC---->"+ip.getSourceAddress()+"目的MAC---->"+ip.getDestinationAddress());
}
try{
String driver = "com.mysql.jdbc.Driver";
String database = "ymsdata";
String user = "root";
String password = "0000";
String url = "jdbc:mysql://localhost:3406/"+database+"?user="+user+"&password="+password;
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url);
Statement stmt=conn.createStatement();
EthernetPacket ip = null;
String sql="insert into r values('"+ip.getSourceAddress()+"','"+ip.getDestinationAddress()+"')";
int executeUpdate = stmt.executeUpdate(sql);
stmt.close();
conn.close();
}catch(Exception ex){
System.out.println(ex);
System.exit(0);
}
}
public static void main(String[] args) {
try{
NetworkInterface[] ni=JpcapCaptor.getDeviceList();
System.out.println("当前设备数目"+ni.length);
int Index=1;
JpcapCaptor jpc=JpcapCaptor.openDevice(ni[Index], 65535, true, 20000);
jpc.loopPacket(-1, new jpcaplisten());
}catch(Exception e){
e.printStackTrace();
}
}
} 展开
4个回答
展开全部
这么一堆代码,上哪看去啊!空指针会提示你是第几行,一定是对象之类没有实例化等原因!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你看看那一行空,你用都是框架还是什么。把那一行空写出来
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
NullPointerException
空指针异常
看看错误提示里有没有在第几行出现
空指针异常
看看错误提示里有没有在第几行出现
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询