有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

SNMP协议中的java NullPointerException

我正在尝试向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();

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


共 (4) 个答案

  1. # 2 楼答案

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

  2. # 3 楼答案

    我今天也面临同样的问题。我通过以下方法解决了这个问题:

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

    在Windows10操作系统上从NetBeans运行时,我得到了以下信息。 输出: 硬件:Intel64系列6型号60 Stepping 3 AT/AT兼容-软件:Windows版本6.3(无需构建16299多处理器)

  3. # 4 楼答案

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

    对于windows。。。 开放服务->;在SNMP中单击鼠标右键->;在“安全”选项卡下->;添加社区字符串