0

That's my activity, where I want to send a Post request with data JSON formatted.

public class OrderActivity extends Activity {


        List<Piatto> ordine = new ArrayList<Piatto>();
        Context context;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.order_layout);




                TextView disptext = (TextView) findViewById(R.id.dispid);


                ordine = getIntent().getParcelableArrayListExtra("ordinelist");

                context = getApplicationContext();




                String json = "";
                Integer contatore = 0;
                for (Integer i = 0; i < ordine.size(); i++) { 
                    if (ordine.get(i) != null){
                        if (contatore > 0){
                            json = json+", ";
                        }
                        json = json+ordine.get(i).getJSON();

                    }
                }
                disptext.setText(json);

                POST("http://some.altervista.org/order.php", json);

                }



        public static String POST(String url, String json){
            InputStream inputStream = null;
            String result = "";
            try {

                // 1. create HttpClient
                HttpClient httpclient = new DefaultHttpClient();

                // 2. make POST request to the given URL
                HttpPost httpPost = new HttpPost(url);


                // 5. set json to StringEntity
                StringEntity se = new StringEntity(json);

                // 6. set httpPost Entity
                httpPost.setEntity(se);

                // 7. Set some headers to inform server about the type of the content   
                httpPost.setHeader("Accept", "application/json");
                httpPost.setHeader("Content-type", "application/json");

                // 8. Execute POST request to the given URL
                HttpResponse httpResponse = httpclient.execute(httpPost);


            } catch (Exception e) {
                Log.d("InputStream", e.getLocalizedMessage());
            }

            // 11. return result
            return result;
        }


}

Then, this is the error I receive

07-23 16:09:21.450: E/AndroidRuntime(5806): FATAL EXCEPTION: main
07-23 16:09:21.450: E/AndroidRuntime(5806): java.lang.RuntimeException: Unable to start activity ComponentInfo{it.sii.mywaiter/it.sii.mywaiter.OrderActivity}: java.lang.NullPointerException: println needs a message
07-23 16:09:21.450: E/AndroidRuntime(5806):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
07-23 16:09:21.450: E/AndroidRuntime(5806):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
07-23 16:09:21.450: E/AndroidRuntime(5806):     at android.app.ActivityThread.access$700(ActivityThread.java:134)
07-23 16:09:21.450: E/AndroidRuntime(5806):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
07-23 16:09:21.450: E/AndroidRuntime(5806):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 16:09:21.450: E/AndroidRuntime(5806):     at android.os.Looper.loop(Looper.java:137)
07-23 16:09:21.450: E/AndroidRuntime(5806):     at android.app.ActivityThread.main(ActivityThread.java:4867)
07-23 16:09:21.450: E/AndroidRuntime(5806):     at java.lang.reflect.Method.invokeNative(Native Method)
07-23 16:09:21.450: E/AndroidRuntime(5806):     at java.lang.reflect.Method.invoke(Method.java:511)
07-23 16:09:21.450: E/AndroidRuntime(5806):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
07-23 16:09:21.450: E/AndroidRuntime(5806):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
07-23 16:09:21.450: E/AndroidRuntime(5806):     at dalvik.system.NativeStart.main(Native Method)
07-23 16:09:21.450: E/AndroidRuntime(5806): Caused by: java.lang.NullPointerException: println needs a message
07-23 16:09:21.450: E/AndroidRuntime(5806):     at android.util.Log.println_native(Native Method)
07-23 16:09:21.450: E/AndroidRuntime(5806):     at android.util.Log.d(Log.java:155)
07-23 16:09:21.450: E/AndroidRuntime(5806):     at it.sii.mywaiter.OrderActivity.POST(OrderActivity.java:133)
07-23 16:09:21.450: E/AndroidRuntime(5806):     at it.sii.mywaiter.OrderActivity.onCreate(OrderActivity.java:71)
07-23 16:09:21.450: E/AndroidRuntime(5806):     at android.app.Activity.performCreate(Activity.java:5047)
07-23 16:09:21.450: E/AndroidRuntime(5806):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-23 16:09:21.450: E/AndroidRuntime(5806):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
07-23 16:09:21.450: E/AndroidRuntime(5806):     ... 11 more

Do you have some ideas about that? I have no direct code about println. Or do you have other suggestion about implementing something like that?

Thanks a lot

tilusup
  • 65
  • 1
  • 6
  • println is called by log.d, which you call. look at the stacktrace. your exception does not have a localized message. this is not a good way of logging an exception, you should use printStackTrace instead. you'll probably see that it is a networkonmainthreadexception, which is largely documented already. also don't hesitate to use the debug tools directly accessible from your favorite IDE. – njzk2 Jul 23 '14 at 14:50

1 Answers1

2

The problem is related also to the Log class that internally calls println. For instance, you have

Log.d("InputStream", e.getLocalizedMessage());

if e.getLocalizedMessage() returns, your app will crash with that exception

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • and do you know why it doesn't send an effective http request? Thanks a lot – tilusup Jul 23 '14 at 15:33
  • the application is probably crashing for `NetworkOnMainThreadExecption`. Try printing `Log.d("InputStream", "Error", e);` or `e.printStackTrace()` to see what the real cause of the exception – Blackbelt Jul 23 '14 at 15:35
  • @Blackbelt Can you tell me why you marked my question http://stackoverflow.com/q/34155492/5524159 as duplicate. Before doing so you have to go through the problem. You just had a look at the logcat and marked it as duplicate. But there is a different issue with my question. You can have suggested me to have a look at this question. Would have appreciated it – user5524159 Dec 09 '15 at 05:54