3

I am trying to run the simple Jaunt example from the website and got an error for Null Pointer Exception. I am not sure what to do because there is very little support for using Jaunt in Android Studio. Here is my code:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try{
        UserAgent userAgent = new UserAgent();
        userAgent.visit("http://jaunt-api.com/examples/signup.htm");         
    }
    catch(JauntException e){
        System.out.println(e);
    }

}

}

Here is the error I got when I ran it:

java.lang.NullPointerException: Attempt to invoke interface method 'void com.android.okhttp.internal.http.Transport.writeRequestHeaders(com.android.okhttp.Request)' on a null object reference

The error was on the userAgent.visit line.

This is where I got the code: http://jaunt-api.com/jaunt-tutorial.htm

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
LUKER
  • 540
  • 1
  • 5
  • 23

3 Answers3

0

This problem usually occurs on Android if you failed to connect your HttpURLConnection first - or can be a side effect if you forgot to add the internet permission to your manifest

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
0

First make sure your url is correct

http://jaunt-api.com/examples/signup.htm 

this is definitely wrong url

then

Here is a workaround class for NPE that you can use in OkHttp

public class NullOnEmptyConverterFactory extends Converter.Factory {


    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
        final Converter<ResponseBody, ?> delegate = retrofit.nextResponseBodyConverter(this, type, annotations);
        return (Converter<ResponseBody, Object>) body -> {
            if (body.contentLength() == 0) return null;
            return delegate.convert(body);
        };
    }
}

In your OkHttp client builder addNetworkInterceptor first, order matters

Ege Kuzubasioglu
  • 5,991
  • 12
  • 49
  • 85
-1

Try add the permissions in your AndroidManifest.xml:

<uses-permission
     android:name="android.permission.WRITE_EXTERNAL_STORAGE"
     android:maxSdkVersion="18" />
<uses-permission
     android:name="android.permission.INTERNET"
     android:maxSdkVersion="18" />
Bruno Carletti
  • 273
  • 1
  • 3
  • 18