Java, Android : Jetty scheduler method throwing NP

2019-09-08 08:12发布

问题:

I am working on an Android application in which I am trying to initialize a LongPollingTransport as mentioned below. But I am getting an error from Jetty scheduler. I am not calling the Jetty Scheduler class myself. How can I fix this. Any help would be nice.

Code :

public class ConsoleChatClient {


    private volatile String nickname = "kernel";
    private volatile BayeuxClient client;
    private final ChatListener chatListener = new ChatListener();
    private final MembersListener membersListener = new MembersListener();
    HttpClient httpClient = new HttpClient();

    public void run() throws IOException
    {


        String defaultURL = "http://localhost:8080/cometd/cometd";

        client = new BayeuxClient(defaultURL, new LongPollingTransport(null,httpClient));
        client.getChannel(Channel.META_HANDSHAKE).addListener(new InitializerListener());
        client.getChannel(Channel.META_CONNECT).addListener(new ConnectionListener());
//Line below throws an error
-->        client.handshake(); 
}

Error log :

11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{internetlegion.twentynotes/internetlegion.twentynotes.Login.LoginActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'org.eclipse.jetty.util.thread.Scheduler$Task org.eclipse.jetty.util.thread.Scheduler.schedule(java.lang.Runnable, long, java.util.concurrent.TimeUnit)' on a null object reference
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:144)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:155)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5696)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'org.eclipse.jetty.util.thread.Scheduler$Task org.eclipse.jetty.util.thread.Scheduler.schedule(java.lang.Runnable, long, java.util.concurrent.TimeUnit)' on a null object reference
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at org.eclipse.jetty.client.TimeoutCompleteListener.schedule(TimeoutCompleteListener.java:53)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at org.eclipse.jetty.client.HttpRequest.send(HttpRequest.java:675)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at org.cometd.client.transport.LongPollingTransport.send(LongPollingTransport.java:187)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at org.cometd.client.BayeuxClient$BayeuxClientState.transportSend(BayeuxClient.java:1430)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at org.cometd.client.BayeuxClient$BayeuxClientState.send(BayeuxClient.java:1425)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at org.cometd.client.BayeuxClient.sendHandshake(BayeuxClient.java:403)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at org.cometd.client.BayeuxClient$HandshakingState.execute(BayeuxClient.java:1526)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at org.cometd.client.BayeuxClient.changeState(BayeuxClient.java:1082)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at org.cometd.client.BayeuxClient.handshake(BayeuxClient.java:339)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at org.cometd.client.BayeuxClient.handshake(BayeuxClient.java:315)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at internetlegion.twentynotes.Activity.ConsoleChatClient.run(ConsoleChatClient.java:38)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at internetlegion.twentynotes.Login.LoginActivity.onCreate(LoginActivity.java:66)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5958)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) 
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:144) 
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) 
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:155) 
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5696) 
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) 
11-12 10:56:38.317 19972-19972/internetlegion.twentynotes E/AndroidR

Build.gradle (For app)

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.fasterxml.jackson.core:jackson-core:2.6.0'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.6.0'
    compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
    compile 'org.springframework.android:spring-android-core:1.0.1.RELEASE'
    compile('org.springframework.android:spring-android-auth:1.0.1.RELEASE') {
        exclude group: 'org.springframework', module: 'commons-logging'
        exclude group: 'org.springframework', module: 'spring-core'
        exclude group: 'org.springframework', module: 'spring-web'
    }
    compile 'com.fasterxml.jackson.core:jackson-annotations:2.6.0'
    compile 'com.google.android.gms:play-services:7.5.0'
    compile 'com.tonicartos:superslim:0.4.13'
    compile 'commons-codec:commons-codec:1.9'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'org.cometd.java:bayeux-api:3.0.6'
    compile 'org.cometd.java:cometd-java-server:3.0.6'
    compile 'org.cometd.java:cometd-java-client:3.0.6'
    compile 'org.cometd.java:cometd-java-annotations:3.0.6'
}

Any help would be nice. Thanks a lot. :-)

Edit

Finally I debugged it and found that HandshakeState.toString is throwing an NPE, but as this is a library method, I have no idea what to do about it.

Error :

Method threw 'java.lang.NullPointerException' exception. Cannot evaluate org.cometd.client.BayeuxClient$HandshakingState.toString()

Failing method :

   private class HandshakingState extends BayeuxClientState
    {

--->        private HandshakingState(Map<String, Object> handshakeFields, ClientSessionChannel.MessageListener callback, ClientTransport transport)
        {
            super(State.HANDSHAKING, handshakeFields, callback, null, transport, null, 0);
        }

回答1:

I doubt HandshakingState.toString() throws an NullPointerException, since the only thing toString() does is to forward the call to the enum type field.

What seems to me from your code is that the HttpClient is not started.

The HttpClient instance is created as a field in class ConsoleChatClient, but you never call start() on it.

This is important because you can have multiple BayeuxClient instances referring to the same HttpClient instance, so the lifecycle of HttpClient must be started before you are using BayeuxClient.