Results 1 to 3 of 3

Thread: Trying to POST and getting NullPointerExection

  1. #1
    Join Date
    Nov 2012
    Posts
    5

    Default Trying to POST and getting NullPointerExection

    I've read and tried just about every solution on the internet for doing this, and they all seem to produce an exception for me. I am successfully doing a GET but can't seem to get a POST to work.

    Here's my current code:


    Code:
    		String url = new String("http://www.myurl.com/scripts/json/v1/slipmanager.php");
    		
    		MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
    		formData.add("username", userName);
    		formData.add("password", userPassword);
    		formData.add("method", "getslips");
    
    		HttpHeaders requestHeaders = new HttpHeaders();
    		requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
    		HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(formData, requestHeaders);
    		RestTemplate restTemplate = new RestTemplate();
    
    
            //I TRIED THIS ONE TOO AND IT GIVES AN EXCEPTION AS WELL
    	    //restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
    
    		restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
    		ResponseEntity<String> response = null;
    		try {
    		response = restTemplate.exchange(url, HttpMethod.POST, requestEntity,
    				String.class);
    		} catch (Exception e) {
    			Log.e("get post", e.getMessage(), e);
    		}
    
    		JSONObject motdsJSONObject = new JSONObject( response.getBody());


    There error that I'm getting:

    Code:
        11-28 15:03:41.419: E/get post(7954): null
        11-28 15:03:41.419: E/get post(7954): java.lang.NullPointerException
        11-28 15:03:41.419: E/get post(7954): 	at org.springframework.http.client.SimpleClientHttpResponse.getStatusCode(SimpleClientHttpResponse.java:62)
        11-28 15:03:41.419: E/get post(7954): 	at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:46)
        11-28 15:03:41.419: E/get post(7954): 	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:476)
        11-28 15:03:41.419: E/get post(7954): 	at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:438)
        11-28 15:03:41.419: E/get post(7954): 	at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:414)
        11-28 15:03:41.419: E/get post(7954): 	at com.mytemp.myapp.TimeSlipAPI.readTimeSlipFromAPI(TimeSlipAPI.java:75)
        11-28 15:03:41.419: E/get post(7954): 	at com.mytemp.myapp.TimeSlipPicker.populateList(TimeSlipPicker.java:382)
        11-28 15:03:41.419: E/get post(7954): 	at com.mytemp.myapp.TimeSlipPicker.onCreate(TimeSlipPicker.java:79)
        11-28 15:03:41.419: E/get post(7954): 	at android.app.Activity.performCreate(Activity.java:5104)
        11-28 15:03:41.419: E/get post(7954): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
        11-28 15:03:41.419: E/get post(7954): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
        11-28 15:03:41.419: E/get post(7954): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        11-28 15:03:41.419: E/get post(7954): 	at android.app.ActivityThread.access$600(ActivityThread.java:141)
        11-28 15:03:41.419: E/get post(7954): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
        11-28 15:03:41.419: E/get post(7954): 	at android.os.Handler.dispatchMessage(Handler.java:99)
        11-28 15:03:41.419: E/get post(7954): 	at android.os.Looper.loop(Looper.java:137)
        11-28 15:03:41.419: E/get post(7954): 	at android.app.ActivityThread.main(ActivityThread.java:5039)
        11-28 15:03:41.419: E/get post(7954): 	at java.lang.reflect.Method.invokeNative(Native Method)
        11-28 15:03:41.419: E/get post(7954): 	at java.lang.reflect.Method.invoke(Method.java:511)
        11-28 15:03:41.419: E/get post(7954): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        11-28 15:03:41.419: E/get post(7954): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        11-28 15:03:41.419: E/get post(7954): 	at dalvik.system.NativeStart.main(Native Method)
    Running on my Galaxy Nexus running 4.2.1

  2. #2
    Join Date
    Nov 2012
    Posts
    5

    Default

    well, it appears I copied a few bad examples as this line was missing: template.setRequestFactory(new HttpComponentsClientHttpRequestFactory());


    adding that line in fixes the issue

  3. #3
    Join Date
    Nov 2010
    Posts
    175

    Default

    @kireol it appears you are experiencing an issue that has been resolved and will be available in the next release. RestTemplate supports multiple HTTP clients, including the standard J2SE (default) and the HttpComponents clients. The exception you are seeing is related to the standard J2SE client, so switching to the Http Components client, as you did, should resolve this for now. This should be working in the latest build snapshot too.

    https://jira.springsource.org/browse/ANDROID-102
    Roy Clarkson
    Spring Mobile Projects Lead

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •