I remember a Googler telling me volatile doesn't do anything in the dalvik VM.
My suggestion would be use an AsyncTask and create the object in the onPreExecute method. You can pass any data you need for the object creation to the AsyncTask constructor. Al. On 27 Sep 2011, at 20:21, Matt Kanninen <[email protected]> wrote: > My guess is you need to use the volatile keyword on your variable. > > http://www.google.com/search?q=java+volatile > > On Sep 26, 6:10 pm, sebastian_bugiu > <[email protected]> wrote: >> I am absolutely positive that the object is initialized before the >> thread accessing it starts. I moved the initialization in the >> constructor that completes before the thread starts. I removed static >> final. Nothing. The thread UI calls surfaceCreated and in it I create >> this object and start a thread that later accesses the object and >> somehow accessing the field in the run() method causes >> NullPointerException. If I access it from the UI thread it works. Can >> a field be thread specific? And for everybody else be invisible? And >> why only on HTC Desire (haven't tested other HTC phones). I thought it >> might be some race condition or something since it has a faster CPU >> but the code really looks like this: >> >> status = new Status(); >> Thread thread = new Thread() { >> public void run() { >> if (status == null) { >> throw new NullPointerException(); >> } >> >> }.start(); >> >> And on HTC is always null. >> >> On Sep 27, 3:44 am, Tim Mensch <[email protected]> wrote: >> >> >> >> >> >> >> >>> On 9/26/2011 6:36 PM, sebastian_bugiu wrote: >> >>>> I don't think there is an issue with my code as it works >>>> just fine in the emulator and on other phones. After all accessing a >>>> private static final initialized field should not yield a >>>> NullPointerException in any circumstances... >> >>> Except if the class statics haven't been initialized yet? We're talking >>> threads here. Aside from that: Are you POSITIVE that the null pointer is >>> the static final field, and not something it's using? >> >>> Java isn't magic. You still have to write thread-aware code -- and you >>> need to know how Java works with static initializers. I don't, which is >>> why I recommended you ask that question in a Java forum, or one of the >>> Android developer forums. >> >>> I just have to disagree with your premise: There simply isn't likely to >>> be a difference in how Java behaves between platforms EXCEPT FOR TIMING >>> ISSUES when you have a faster CPU. That's where threads can get you in >>> trouble. >> >>> Tim > > -- > You received this message because you are subscribed to the Google Groups > "Android Discuss" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/android-discuss?hl=en. > -- You received this message because you are subscribed to the Google Groups "Android Discuss" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/android-discuss?hl=en.
