BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 07-02-2009, 09:40 AM   #1 (permalink)
New Member
 
Join Date: Jun 2009
Model: 8900
PIN: N/A
Carrier: XL
Posts: 3
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Having trouble with FolderListener and ListField

Please Login to Remove!

Hi all, I'm a newbie on developing application in Blackberry. I have some issues when I try to add items to my ListField.

I've created a class that extends ListField and implements FolderListener interface. All I want is listening to the device inbox , when there a message arrive, I want to append it into my ListField class. Here is my code:

Code:
public void messagesAdded(FolderEvent e) {
Store store = Session.getDefaultInstance().getStore();
        Folder[] f = store.list(Folder.INBOX);
        if (f != null && f.length > 0) {
            try {
                //retrieve only the first inbox
                Folder _inbox = f[0];

                Message _msgInbox[] = _inbox.getMessages();
                for (int i = 0; i < _msgInbox.length; i++) {
                    if (_msgInbox[i].getMessageType() == Message.EMAIL_MESSAGE) {
                        System.out.println("Sender :" + _msgInbox[i].getFrom().getName());
                        data.insertElementAt(_msgInbox[i], i);
                        this.insert(i);
                    }
                }
                this.invalidate();
                size = data.size();
                System.out.println("Data size :"+size);

            } catch (MessagingException ex) {
                ex.printStackTrace();
            } catch(IllegalStateException ex){
                System.out.println(ex.getMessage());
            }
        } 
}

I got an
Code:
 IllegalStateException
when inserting the data
(
Code:
this.insert(i)
)


Can someone give me a clue why this happened ?
Offline  
Old 07-02-2009, 01:44 PM   #2 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

The UI is a single-entrant single-threaded component, and can only be accessed from the event thread.

The FolderListener thread actually belongs to another app (the message app).

A common practice is to:

1. Fire a custom global event to your application, which implements GlobalEventListener
2. From the GlobalEventListener, create a runnable object that performs the UI operation, and call it using UiApplication.invokeLater().
Offline  
Old 07-06-2009, 03:17 AM   #3 (permalink)
New Member
 
Join Date: Jun 2009
Model: 8900
PIN: N/A
Carrier: XL
Posts: 3
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

thx doug...... I think you are right. I moved the listener into my main App and it suddenly works... But now I'm facing different problem. When I tested and run it in 9530 simulator, it works fine. But when I deployed on actual device I got java.lang.NullPointerException


Here is my main application code :
Code:
public class App extends UiApplication implements FolderListener {

    private MailBoxScreen _main;

    public static void main(String[] args) {
        App instance = new App();
        instance.enterEventDispatcher();
    }

    /** Creates a new instance of App */
    public App() {
        _main = new MailBoxScreen(this);
        pushScreen(_main);
    }

    public void messagesAdded(FolderEvent e) {
        //TODO: append to the vector
        final Message incoming = e.getMessage();
        if (incoming != null) {
            try {
                while (App.this == null) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }
                App.this.invokeLater(new Runnable() {
                    public void run() {
                        //Dialog.alert("1 new mail received");
                        LED.setState(LED.STATE_BLINKING);
                        Alert.startVibrate(2000);
                        MailBoxScreen.getMailListInstance().refreshMail(incoming);
                    }
                });
            } catch (NullPointerException ex) {
            }
        }
    }

I also has surround each code on method that called from the main application with Exception. Still cannot find where i got the NullPointerException
Offline  
Old 07-06-2009, 11:14 AM   #4 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

Are you running on a 9530 device?

Look at the event log on the device and get the stack trace.

On a trackball device:

<alt> L G L G

On a Storm:

press and hold <?123>, then type " , 5 , 5 "
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.