Greetings all.
I need to implement a UDP port scanner for a university security module but I seem to be having issues. I have checked that everything has a value and that nothing is null. The address has the value it should and the port numbers are those that exist.
I have included the error that I get as well as the code snippet that seems to be the problem.
-- ERROR --
Exception in thread "main" java.lang.NullPointerException: null address || null buffer
at java.net.PlainDatagramSocketImpl.send(Native Method)
at java.net.DatagramSocket.send(Unknown Source)
at PortScanner.scanUDP(PortScanner.java:116)
at PortScanner.scan(PortScanner.java:142)
at PS.main(PS.java:30)
-- CODE SNIPPET -- PortScanner.java
... private ArrayList<String> udpServiceNames = null; private ArrayList<Integer> udpPortNumbers = null; private String IP = null; ... //Initialization of udpServiceNames and udpPortNumbers from a file. //Setting of IP to "127.0.0.1" ... File udpOut = new File("UDP_Ports_Status.txt"); FileOutputStream uout = new FileOutputStream(udpOut); DatagramSocket sock = new DatagramSocket(); for(int i = 0; i < udpServiceNames.size(); i++) { System.out.print("."); InetAddress addr = null; try { addr = InetAddress.getByName(IP); } catch(UnknownHostException ex) { System.out.println("Passed original IP name conversion but failed now. Terminating...."); System.exit(1); } byte[] buff = new byte[256]; DatagramPacket pack = new DatagramPacket(buff, 256, addr, udpPortNumbers.get(i).intValue()); sock.setSoTimeout(500); boolean received = false; int sendCnt = 0; while(!received && sendCnt < 3) { //Error refers to the sending of the packet. sock.send(pack); pack = new DatagramPacket(buff, 256); try { sock.receive(pack); received = true; break; } catch(Exception ex) { } sendCnt++; } String str; if(!received) str = udpPortNumbers.get(i).intValue() + "::" + udpServiceNames.get(i) + "::CLOSED\r\n"; else str = udpPortNumbers.get(i).intValue() + "::" + udpServiceNames.get(i) + "\r\n"; uout.write(str.getBytes()); } ...