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");


        } finally {

    } catch (ULjException ex) {
    } catch (Error er) {

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?

asked 14 Jan '13, 16:24

katalun4o's gravatar image

accept rate: 85%

edited 14 Jan '13, 16:26

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.

(15 Jan '13, 10:25) Andy Quick

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

(15 Jan '13, 11:17) katalun4o

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.

permanent link

answered 17 Jan '13, 17:31

katalun4o's gravatar image

accept rate: 85%

So it was the PreferencesUtil variables that became null?

(17 Jan '13, 18:58) Graham Hurst

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

(18 Jan '13, 03:25) katalun4o

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 ) {
} 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().

permanent link

answered 17 Jan '13, 15:32

Graham%20Hurst's gravatar image

Graham Hurst
accept rate: 28%

edited 17 Jan '13, 15:33

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:


question asked: 14 Jan '13, 16:24

question was seen: 748 times

last updated: 18 Jan '13, 03:25