Click here to Skip to main content
15,888,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, the following code runs perfectly in Android Studio but raises a NullPointerException using AIDE on my android tablet - the error occurs in the sendRequest function - I suspect it's the jersey client library not being compatible but I'm no Java expert - any ideas guys ? If there is an easier way to do this I'm all ears - all im trying to do is send and receive json from a local web server on my lan


irrelevant code removed but will post if needed

Java
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import java.net.*;

class JavaJSONSample
{
    private static final String SERVER = "http://10.10.1.11:9000/jsonrpc.js";

    static Client client;
   static WebResource resource;


    public static void main(String[] args)
    {
        try
        {
            client = Client.create();
            resource = client.resource(SERVER);
			resource.accept("application/json");

            JSONObject artistrequest = createRequest(null, new String[]{"artists", "0", "-1"});

            JSONObject artists = sendRequest(artistrequest);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    private static JSONObject sendRequest(JSONObject request)
    {
      return resource.post(JSONObject.class, request);
    }

    private static JSONObject createRequest(String playerId, String[] params)
    {
        JSONObject request = new JSONObject();
        try
        {
            request.put("id", 1L);
            request.put("method", "slim.request");
			
            JSONArray requestData = new JSONArray();
            requestData.put("-");

            JSONArray commandArray = new JSONArray();

            for (String param : params)
            {
                commandArray.put(param);
            }

            requestData.put(commandArray);

            request.put("params", requestData);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        return request;
    }
}

This is the error
java.lang.ExceptionInInitializerError
	at com.sun.jersey.core.spi.factory.MessageBodyFactory.getMessageBodyWriterMediaTypes(MessageBodyFactory.java:444)
	at com.sun.jersey.api.client.RequestWriter.getMediaType(RequestWriter.java:324)
	at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:282)
	at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:217)
	at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:153)
	at com.sun.jersey.api.client.Client.handle(Client.java:652)
	at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)
	at com.sun.jersey.api.client.WebResource.post(WebResource.java:253)
	at JavaJSONSample.sendRequest(Main.java:77)
	at JavaJSONSample.main(Main.java:26)
	at java.lang.reflect.Method.invoke(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:372)
	at com.aide.ui.build.java.RunJavaActivity$1.run(SourceFile:108)
	at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object javax.ws.rs.ext.RuntimeDelegate$HeaderDelegate.fromString(java.lang.String)' on a null object reference
	at javax.ws.rs.core.MediaType.valueOf(MediaType.java:119)
	at com.sun.jersey.core.header.MediaTypes.<clinit>(MediaTypes.java:65)
	... 14 more
Posted
Updated 22-Dec-15 1:52am
v5
Comments
Darren_vms 24-Dec-15 8:04am    
A guess but on the tablet did you change the SERVER address ?

pkfox 24-Dec-15 8:19am    
Hi there, no it has a static IP address and as I say it runs perfectly as a java module in Android Studio - the problem stems from the RuntimeDegate accessing a null string which must be non null in Android Studio but null on Android tablet
Darren_vms 24-Dec-15 9:12am    
Maybe you might get more help on https://groups.google.com/forum/?fromgroups#!forum/android-ide Id never heard of ide until I read this.
pkfox 24-Dec-15 12:35pm    
Do you mean Aide ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900