cancel
Showing results for 
Search instead for 
Did you mean: 

SyncParams.getStreamParams.setPort NullPointerException when wi-fi stopped

Former Member
2,447

Hello, I have a working Mobilink synchronization in my Android app. Everything works fine until I stop the wi-fi of my phone. Then appears a strange error(but not always, sometimes I get only the Mobilink communication error which is handled without a problem with try and catch):


01-14 22:54:08.698: E/AndroidRuntime(1374): FATAL EXCEPTION: LocalService
01-14 22:54:08.698: E/AndroidRuntime(1374): java.lang.NullPointerException
01-14 22:54:08.698: E/AndroidRuntime(1374): at DAL.DBUtil.SyncVouchers(DBUtil.java:434)
01-14 22:54:08.698: E/AndroidRuntime(1374): at com.mycompany.myapp.LocalService$1.run(LocalService.java:62)
01-14 22:54:08.698: E/AndroidRuntime(1374): at java.lang.Thread.run(Thread.java:864)


Here is the source code where the error appears, it is called from a service that is started with alarm manager, the exception is thrown always at SetPort method:

try {  
        Connection conn = DBUtil.CreateConnection(context);  
        try {  
            SyncParms syncParms = conn.createSyncParms(
                    SyncParms.HTTP_STREAM, "user", "Version");

            syncParms.getStreamParms().setHost(PreferencesUtil.IP);
            syncParms.getStreamParms().setPort(PreferencesUtil.Port);
            syncParms.setPublications("myPublication");
            syncParms.setAuthenticationParms(String
                    .valueOf(app_settings.CurrentUserID));

            conn.synchronize(syncParms);
            conn.commit();
        } finally {
            conn.release();
        }

    } catch (ULjException ex) {
        ex.printStackTrace();
    } catch (Error er) {
        er.printStackTrace();
    }}

And despite i have catch block at the end, the application crashes and doesn't handle the error.. After that the alarm manager is still being called but in log cat appears - Unable to launch app... process is bad. Do you have any idea what is happening?

Former Member
0 Kudos

It is difficult to see from this code how syncParms or syncParms.getStreamParms() could be null at the setPort() line and not at the previous line. The setPort(int port) method simply assigns the integer argument to a member variable. I don't know what PreferencesUtil is, or whether it could be null.

In a nutshell, I don't have enough context to give an answer. You may have to open a support case and supply a small reproducible test.

Former Member
0 Kudos

PreferencesUtil is a simple class with methods to write in Preferences and Port and IP are static variables. I tried to check if syncParams == null and syncParams.getStreamParams() == null but I got the same exception again. I am very confused because the error is not catched by the Try/Catch block - always crashes

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

I finally got it! Thanks to Graham Hurst 🙂 When I stop the Wi-Fi I got the ordinary connection exception once and after that all static variables are lost and I got null reference exceptions. The solution was to save the variables that I need in SharedPreferences and take them every time the service is started.

Former Member
0 Kudos

So it was the PreferencesUtil variables that became null?

Former Member
0 Kudos

Yes, after getting an exception in the service all static properties become null..

Answers (1)

Answers (1)

Former Member

You are never catching the NullPointerException because it is not an instance of either Error or ULjException, and you are only catching those. It is an instance of RuntimeException, so is an unchecked exception and does not need to be in a method or constructor's throws clause.

Instead of calling syncParms.getStreamParms() twice, why not assign it to a variable that you can test for nullness before calling setHost() and setPort().

StreamParms streamParms = syncParms.getStreamParms();
if ( streamParms != null ) {
    streamParms.setHost(PreferencesUtil.IP);
    streamParms.setPort(PreferencesUtil.Port);
} else {
    // Handle null streamParms
}

However I doubt that would fix the underlying problem, unless it is due to garbage collection occuring between your two calls to getStreamParms().