gpt4 book ai didi

java - SNMP 协议(protocol)中的 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 07:11:37 24 4
gpt4 key购买 nike

我正在尝试将客户端添加到我的 snmp 协议(protocol)程序中。我为我的主要方法添加了这段代码:

public static void main(String[] args) throws IOException{
SimpleSnmpClient client = new SimpleSnmpClient("udp:10.0.0.50/161");
String sysDescr = client.getAsString(new OID(".1.3.6.1.2.1.1.1.0"));
System.out.println(sysDescr);
}

我从网站上了解到,输出应该是关于运行该程序的设备的某种信息。

我的 SimpleSnmpClient 代码:

public class SimpleSnmpClient {

private String address;

private Snmp snmp;


public static void main(String[] args) throws IOException{
SimpleSnmpClient client = new SimpleSnmpClient("udp:10.0.0.50/161");
String sysDescr = client.getAsString(new OID(".1.3.6.1.2.1.1.1.0"));
System.out.println(sysDescr);
}

public SimpleSnmpClient(String address) {
super();
this.address = address;
try {
start();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

// Since snmp4j relies on asynch req/resp we need a listener
// for responses which should be closed
public void stop() throws IOException {
snmp.close();
}

private void start() throws IOException {
TransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
// Do not forget this line!
transport.listen();
}

public String getAsString(OID oid) throws IOException {
ResponseEvent event = get(new OID[]{oid});
return event.getResponse().get(0).getVariable().toString();
}


public void getAsString(OID oids,ResponseListener listener) {
try {
snmp.send(getPDU(new OID[]{oids}), getTarget(),null, listener);
} catch (IOException e) {
throw new RuntimeException(e);
}
}


private PDU getPDU(OID oids[]) {
PDU pdu = new PDU();
for (OID oid : oids) {
pdu.add(new VariableBinding(oid));
}

pdu.setType(PDU.GET);
return pdu;
}

public ResponseEvent get(OID oids[]) throws IOException {
ResponseEvent event = snmp.send(getPDU(oids), getTarget(), null);
if(event != null) {
return event;
}
throw new RuntimeException("GET timed out");
}

private Target getTarget() {
Address targetAddress = GenericAddress.parse(address);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(1500);
target.setVersion(SnmpConstants.version2c);
return target;
}

/**
* Normally this would return domain objects or something else than this...
*/
public List<List<String>> getTableAsStrings(OID[] oids) {
TableUtils tUtils = new TableUtils(snmp, new DefaultPDUFactory());

@SuppressWarnings("unchecked")
List<TableEvent> events = tUtils.getTable(getTarget(), oids, null, null);

List<List<String>> list = new ArrayList<List<String>>();
for (TableEvent event : events) {
if(event.isError()) {
throw new RuntimeException(event.getErrorMessage());
}
List<String> strList = new ArrayList<String>();
list.add(strList);
for(VariableBinding vb: event.getColumns()) {
strList.add(vb.getVariable().toString());
}
}
return list;
}

public static String extractSingleString(ResponseEvent event) {
return event.getResponse().get(0).getVariable().toString();
}
}

但是我收到了 NULLPointerException:

Exception in thread "main" java.lang.NullPointerException
at org.bihe.SimpleSnmpClient.getAsString(SimpleSnmpClient.java:70)
at org.bihe.SimpleSnmpClient.main(SimpleSnmpClient.java:41)

它指的是行:

return event.getResponse().get(0).getVariable().toString();

我不知道为什么会这样?谁能帮我解决这个问题?

最佳答案

如果您超时,您会得到一个空响应。来自 ResponseEvent 文档:“a PDU instance if a response has been received. If the request timed out then null will be returned.

也许您的服务器未启动或不可用。尝试使用一些已知的应用程序访问它,例如 snmpwalk只是为了将您的代码与某些网络问题区分开来。

关于java - SNMP 协议(protocol)中的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20980746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com