【问题标题】:NullPointerException in SNMP protocolSNMP 协议中的 NullPointerException
【发布时间】:2014-01-07 20:02:47
【问题描述】:

我正在尝试将客户端添加到我的 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);
    }

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

我的 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();

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

【问题讨论】:

    标签: java nullpointerexception snmp snmp4j


    【解决方案1】:

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

    也许您的服务器未启动或不可用。尝试使用诸如snmpwalk 之类的已知应用程序访问它,以便将您的代码与某些网络问题区分开来。

    【讨论】:

    • 您能帮我如何使用 snmpwalk 吗?如果这是一个应用程序,我在哪里可以找到它?我使用的是 Windows 8,我安装了 net-snmp,还从 Windows 功能中添加了 snmp。他们依赖这个问题吗?
    • 看起来 snmpwalk 可能带有 net-snmp。查看提供的示例。 net-snmp.org/docs/man/snmpwalk.html
    • 如何在 Windows 中运行此命令? % snmpwalk -mALL -v1 -cpublic snmp_agent_Ip_address 系统?
    • 我在我的 net-snmp 目录下运行 snmpwalk.exe,它成功完成了,现在我应该如何运行这段代码?
    • 我会分解您通话的各个部分,一次看一个。 getVariable 是空的吗?看起来很像,您可能想尝试更多地探索响应以查看它有哪些有用的信息。
    【解决方案2】:

    您尝试从中获取响应的设备是否设置了社区字符串?社区就像密码,设置在设备上,以防止人们在未经授权的情况下从该设备中检索信息。如果社区字符串不正确,您将不会收到回复。

    【讨论】:

      【解决方案3】:

      确保您为 SNMP 服务代理添加了相同的社区字符串。

      对于窗户... 打开服务->在SNMP中右键->在安全选项卡下->添加社区字符串

      【讨论】:

        【解决方案4】:

        我今天遇到了同样的问题。而且,我通过执行以下操作解决了它:

        • 在 Windows 运行对话框中键入“services.msc”并回车。这 将打开“服务”窗口。
        • 双击“SNMP 服务”并选择“安全”选项卡。在 “接受的社区名称”部分,添加社区名称“public” 安全级别为“只读”或“读写”(根据您的需要)。 然后单击“确定”按钮完成此过程。
        • 现在,尝试运行该程序。它将返回结果。

        在我的 Windows 10 操作系统上从 NetBeans 运行它时,我得到了以下信息。 输出: 硬件:Intel64 Family 6 Model 60 Stepping 3 AT/AT COMPATIBLE - 软件:Windows 版本 6.3(Build 16299 多处理器免费)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多