如何在Java中访问端口?

如何在Java中访问端口?

问题描述:

我正在尝试使用以下Java代码访问系统中的端口,并且给出了空指针异常,任何人都可以帮助我解决此问题.拜托,在此先谢谢

I am trying to access ports in system by using below java code And it is giving null pointer exception can anyone help me to solve this . Please , Thanks in advance

package com.ranju.app;

import java.util.Enumeration;

import javax.comm.CommPortIdentifier;

public class Ranjith {

public static void main(String[] args) {

System.out.println("HI ");
Enumeration pList= CommPortIdentifier.getPortIdentifiers();
System.out.println(" ++++++++ ");
//while(pList.hasMoreElements()){
CommPortIdentifier commIdfr=(CommPortIdentifier) pList.nextElement();
System.out.println(" *** "+commIdfr.getName());

//}
}

}

Nullpointer发生在哪里?

这是一个描述它的不错的博客文章-甚至包括示例代码:
http://codeglobe.blogspot.de/2009/02/serial-port- communication-in-java.html [ ^ ]
where is the Nullpointer happening?

Here is a pretty good blog entry describing it - even an example code is included:
http://codeglobe.blogspot.de/2009/02/serial-port-communication-in-java.html[^]