0

I am trying to do xml parsing program.The link to the xml page is :http://api.androidhive.info/pizza/?format=xml

While making http connection it is showing error as : java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.xml/com.example.xml.XmlparseActivity}: java.lang.NullPointerException

Then i put Log message in each line,so the problem is at HttpResponce no log message is displaying after that line.to solve problem.Can anybody help My program code is shown below :

 public String getXmlElement(String url) {
        // TODO Auto-generated method stub
        String xml=null;
        try{
        DefaultHttpClient client=new DefaultHttpClient();
        Log.d("cli", "ok");
        HttpPost post= new HttpPost(url);
        Log.d("post", "ok"+post);
        HttpResponse responce=client.execute(post);
        Log.d("responce", "ok");
        HttpEntity entity=responce.getEntity();
        Log.d("entity", "ok");
        xml=EntityUtils.toString(entity);
        }catch(UnsupportedEncodingException e){
            e.printStackTrace();
        }catch (IOException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return xml;

    }

The logcat page is :

07-05 17:13:03.227: D/hai(809): http://api.androidhive.info/pizza/?format=xml
07-05 17:13:03.237: D/cli(809): ok
07-05 17:13:03.247: D/post(809): okorg.apache.http.client.methods.HttpPost@44ee99e8
07-05 17:13:03.317: W/System.err(809): java.net.UnknownHostException: api.androidhive.info
07-05 17:13:03.317: W/System.err(809):  at java.net.InetAddress.lookupHostByName(InetAddress.java:513)
07-05 17:13:03.327: W/System.err(809):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:278)
07-05 17:13:03.327: W/System.err(809):  at java.net.InetAddress.getAllByName(InetAddress.java:242)
07-05 17:13:03.327: W/System.err(809):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
07-05 17:13:03.337: W/System.err(809):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-05 17:13:03.337: W/System.err(809):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-05 17:13:03.337: W/System.err(809):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
07-05 17:13:03.347: W/System.err(809):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-05 17:13:03.347: W/System.err(809):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-05 17:13:03.347: W/System.err(809):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-05 17:13:03.357: W/System.err(809):  at com.example.xml.XmlParser.getXmlElement(XmlParser.java:93)
07-05 17:13:03.357: W/System.err(809):  at com.example.xml.XmlparseActivity.onCreate(XmlparseActivity.java:33)
07-05 17:13:03.357: W/System.err(809):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-05 17:13:03.357: W/System.err(809):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-05 17:13:03.357: W/System.err(809):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-05 17:13:03.367: W/System.err(809):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-05 17:13:03.367: W/System.err(809):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-05 17:13:03.367: W/System.err(809):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 17:13:03.367: W/System.err(809):  at android.os.Looper.loop(Looper.java:123)
07-05 17:13:03.377: W/System.err(809):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-05 17:13:03.377: W/System.err(809):  at java.lang.reflect.Method.invokeNative(Native Method)
07-05 17:13:03.377: W/System.err(809):  at java.lang.reflect.Method.invoke(Method.java:521)
07-05 17:13:03.377: W/System.err(809):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-05 17:13:03.388: W/System.err(809):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-05 17:13:03.388: W/System.err(809):  at dalvik.system.NativeStart.main(Native Method)
07-05 17:13:03.407: D/AndroidRuntime(809): Shutting down VM
07-05 17:13:03.407: W/dalvikvm(809): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-05 17:13:03.427: E/AndroidRuntime(809): FATAL EXCEPTION: main
07-05 17:13:03.427: E/AndroidRuntime(809): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.xml/com.example.xml.XmlparseActivity}: java.lang.NullPointerException
07-05 17:13:03.427: E/AndroidRuntime(809):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-05 17:13:03.427: E/AndroidRuntime(809):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-05 17:13:03.427: E/AndroidRuntime(809):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-05 17:13:03.427: E/AndroidRuntime(809):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-05 17:13:03.427: E/AndroidRuntime(809):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 17:13:03.427: E/AndroidRuntime(809):  at android.os.Looper.loop(Looper.java:123)
07-05 17:13:03.427: E/AndroidRuntime(809):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-05 17:13:03.427: E/AndroidRuntime(809):  at java.lang.reflect.Method.invokeNative(Native Method)
07-05 17:13:03.427: E/AndroidRuntime(809):  at java.lang.reflect.Method.invoke(Method.java:521)
07-05 17:13:03.427: E/AndroidRuntime(809):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-05 17:13:03.427: E/AndroidRuntime(809):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
1
  • 1
    did you add the INTERNET permission to your AndroidManifest.xml ? Commented Jul 5, 2013 at 11:53

3 Answers 3

1

Have you added this line in your manifest

<uses-permission android:name="android.permission.INTERNET" /> 

And write that outside your </application> tag

Sign up to request clarification or add additional context in comments.

Comments

1
public String getXmlElement(String url) {

    String xml=null;
    try{
    DefaultHttpClient client=new DefaultHttpClient();
    Log.d("cli", "ok");
    HttpPost post= new HttpPost(url);
    Log.d("post", "ok"+post);
    HttpResponse responce=client.execute(post);
    Log.d("responce", "ok");
    HttpEntity entity=responce.getEntity();
    Log.d("entity", "ok");
    xml=EntityUtils.toString(entity);
    }catch(UnsupportedEncodingException e){
        e.printStackTrace();
    }catch (IOException e) {
        // TODO: handle exception
        e.printStackTrace();
    }
catch(NullPointerException e){
   Toast.makeText(getApplicationContext(), "Connection to server failed.. Please try later..", Toast.LENGTH_LONG).show();
}
    return xml;

}

You can use the catch as shown above to show the user that he may not be connected to internet. The null pointer exception may occur at any line so it's not that accurate.

Or you can put if else condition with the response as:

 if(responce!=null){
   HttpEntity entity=responce.getEntity();
  }
else{
     Toast.makeText(getApplicationContext(), "Connection to server failed.. Please try later..", Toast.LENGTH_LONG).show();
}

Not mentioning the internet permission.

Comments

0

You are getting null string in your Asyntask response , which you are trying to use directly .And hence getting NPE.

Also give INTERNET permission in manifest to do network related operation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.