Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7437739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:26:44+00:00 2026-05-29T10:26:44+00:00

I want to use simple snmp get in android.I found the code for java

  • 0

I want to use simple snmp get in android.I found the code for java and try to use it in android.I also attach snmp4j.jar file for android.But I got the NullPointer Exception and I don’t get the output.If anybody have any idea or code for how to use snmp in android then please suggest me..

Following is my code which i have tried,

1   import java.io.IOException;
2   import org.snmp4j.CommunityTarget;
3   import org.snmp4j.PDU;
4   import org.snmp4j.Snmp;
5   import org.snmp4j.TransportMapping;
6   import org.snmp4j.event.ResponseEvent;
7   import org.snmp4j.mp.SnmpConstants; 
8   import org.snmp4j.smi.OID;
9   import org.snmp4j.smi.OctetString;
10  import org.snmp4j.smi.UdpAddress;
11  import org.snmp4j.smi.VariableBinding;
12  import org.snmp4j.transport.DefaultUdpTransportMapping;
13  import android.app.Activity;
14  import android.os.Bundle;
15  import android.util.Log;
16  import android.view.View;
17  import android.view.View.OnClickListener;
18  import android.widget.Button;
19  import android.widget.Toast;
20 
21  public class SNMPClient extends Activity {
22
23  private static String ipAddress = "127.0.0.1";
24  private static String port = "161";
25  //cmd to request from Server
26  private static String oidValue = "1.3.6.1.2.1.1.5.0";
27  private static int snmpVersion = SnmpConstants.version2c;
28  private static String community = "public";
29 
30  public static Snmp snmp;
31  public static CommunityTarget comtarget;
32  static PDU pdu;
33  static OID oid;
34  static VariableBinding req;
35  Button b;
36  private static final String tag = "SNMP CLIENT";
37
38 @Override
39 public void onCreate(Bundle savedInstanceState) {
40    super.onCreate(savedInstanceState);
41    setContentView(R.layout.main);
42    
43    System.setProperty("java.net.preferIPv4Stack", "true");
44      System.setProperty("java.net.preferIPv6Addresses", "false");
45      
46    b = (Button) findViewById(R.id.getvalue);
47    b.setOnClickListener(new OnClickListener() {
48
49        public void onClick(View v) {
50            // TODO Auto-generated method stub
51           try {
52                sendSnmpRequest(oidValue);
53            } catch (IOException e) {
54                // TODO Auto-generated catch block
55                e.printStackTrace();
56            } catch (Exception e) {
57                // TODO Auto-generated catch block
58                e.printStackTrace();
59            }
60
61        }
62    });
63   }
64 
65 private void sendSnmpRequest(String cmd) throws Exception {
66    // Create TransportMapping and Listen
67    TransportMapping transport = new DefaultUdpTransportMapping();
68    transport.listen();
69 
70    // Create Target Address object
71    CommunityTarget comtarget = new CommunityTarget();
72    comtarget.setCommunity(new OctetString(community));
73    comtarget.setVersion(snmpVersion);
74    comtarget.setAddress(new UdpAddress(ipAddress + "/" + port));
75    comtarget.setRetries(2);
76    comtarget.setTimeout(1000);
77    // Prepare PDU
78    req = new VariableBinding();
79    OID oid = new OID(cmd);   
80    req.setOid(oid);
81    pdu = new PDU();
82    pdu.add(req);
83    Snmp snmp = new Snmp(transport);
84    Log.i(tag ,"Sending Request to Agent...");
85    ResponseEvent response = snmp.get(pdu, comtarget);
86    // Process Agent Response
87    if (response != null) {
88        Log.i(tag,"Got Response from Agent");   //upto  this get in Logcat 
89        PDU responsePDU = response.getResponse();  //Here get Null response
90        if (responsePDU != null) {
91            int errorStatus = responsePDU.getErrorStatus();
92            int errorIndex = responsePDU.getErrorIndex();
93            String errorStatusText = responsePDU.getErrorStatusText();
94 
95            if (errorStatus == PDU.noError) {
96                Log.i(tag,"Snmp Get Response = "
97                        + responsePDU.getVariableBindings());
98                Toast.makeText(getApplicationContext(),
99                     "Snmp Get Response = " + responsePDU.getVariableBindings(),Toast.LENGTH_LONG).show();
100            } else {
101                 Log.i((String) tag,"Error: Request Failed");
102                 Log.i(tag,"Error Status = " + errorStatus);
103                 Log.i(tag,"Error Index = " + errorIndex);
104                 Log.i(tag,"Error Status Text = " + errorStatusText);
105             }
106         } else {
107             Log.i(tag,"Error: Response PDU is null");  // This get in Logcat
108         }
109     } else {
110         Log.i(tag,"Error: Agent Timeout... ");
111     }
112     snmp.close();
113 }}

Please anyone have any solution Please suggest me. Thanks in Advance………….

At line 89 get null value so get NUllPointer Exception…….Any idea or code

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:26:45+00:00Added an answer on May 29, 2026 at 10:26 am

    From the ResponseEvent.getResponse() documentation (emphasis mine):

    Returns: a PDU instance if a response has been received. If the
    request timed out then null will be returned.

    Now obviously you can see that you’re getting the null response at the line:

    PDU responsePDU = response.getResponse();
    

    It is most likely because you’re trying to do an SNMP get your Android’s localhost address, i.e.:

    private static String ipAddress = "127.0.0.1";
    private static String port = "161";
    // ...
    comtarget.setAddress(new UdpAddress(ipAddress + "/" + port));
    

    This doesn’t make sense because your Android probably does not have the SNMP service running locally, so how you would get a response from an SNMP service that isn’t running?

    Try changing that ipAddress to the address of the device you wish to connect to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I looking for your help please I use simple php code like: $file =
I want to use the Simple Query tool in SubSonic 3(.0.0.2) and the docs
I want to use the php simple HTML DOM parser to grab the image,
I want to use a timer in my simple .NET application written in C#.
I want to use List.Find() on a simple collection that does not implement Find().
I want to use data binding with an XML document to populate a simple
I want to use the Django admin interface for a very simple web application
i want to use facebook's fb:dialog tag to display a simple pop-up form for
i want to know which parsing method is best to use among Simple XML
Given a simple parent/child class structure. I want to use linqkit to apply a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.