null pointer exception in phone contacts on device

81 views
Skip to first unread message

steve

unread,
Apr 17, 2014, 6:35:02 AM4/17/14
to codenameone...@googlegroups.com
Hi,

The code works well on the simulator (ie. it shows all the phone contacts and everything works as it's supposed to) but when I try it out on an actual device (Nokia N8), a null pointer exception is thrown.

What could be the problem?

//initialises the Container
 
public void init() {
        setLayout
(new BorderLayout());
        setScrollable
(false);


       
        cloneContactsArrayList
=new ArrayList<Hashtable>();

        contactsHash
=new Vector<Hashtable>();
       


        contactList
= new List(createGenenericListCellRendererModelData());


        contactList
.setRenderer(new GenericListCellRenderer(ContactBookRenderer(), ContactBookRenderer()));
        contactList
.addActionListener(new ActionListener() {


           
/**
             * deals with the contacts that are selected and/or deselected
             */

           
public void actionPerformed(ActionEvent evt) {
//code deals with selection of contacts and what should be done with them
           
}
       
});
       
        searchTextField
= new TextField();
        searchTextField
.setHint("Search Contacts");
        searchTextField
.addDataChangeListener(new DataChangedListener() {




           
public void dataChanged(int type, int index) {
//deals with searching the selected contacts
           
}
       
});


       
       
       
       
done = new Button("Done");
       
done.addActionListener(new ActionListener() {


           
public void actionPerformed(ActionEvent evt) {
//deals with what is to be done when user finishes their selection
           
}
       
});
        back
= new Button("Back");
        back
.addActionListener(new ActionListener() {


           
public void actionPerformed(ActionEvent evt) {
//deals with going back
       
});


        buttons
= new Container(new BoxLayout(BoxLayout.X_AXIS));
        buttons
.addComponent(done);
        buttons
.addComponent(back);






       
this.addComponent(BorderLayout.SOUTH, buttons);
       
this.addComponent(BorderLayout.NORTH, searchTextField);
       
this.addComponent(BorderLayout.CENTER, contactList);
   
}


   
/**
     * this method creates the list renderer
     *
     * @return the renderer container
     */

   
public Container ContactBookRenderer() {


        renderer
= new Container(new BorderLayout());


        name
= new Label();
        name
.setFocusable(true);
        name
.setName("Name");
        primaryPhoneNumber
= new Label();
        primaryPhoneNumber
.setFocusable(true);
        primaryPhoneNumber
.setName("Number");
        multiCheck
= new CheckBox();
        multiCheck
.setFocusable(true);
        multiCheck
.setName("Selected");
        normalMode
= new Container(new BoxLayout(BoxLayout.Y_AXIS));
        normalMode
.addComponent(name);
        normalMode
.addComponent(primaryPhoneNumber);




        renderer
.addComponent(BorderLayout.WEST, multiCheck);
        renderer
.addComponent(BorderLayout.CENTER, normalMode);




       
return renderer;






   
}


   
/**
     * this method creates a model for the list
     *
     * @return the model for the list renderer
     */

   
private Vector<Hashtable> createGenenericListCellRendererModelData() {
       
if(ContactsManager.getAllContacts().length>0){
        allContacts
= ContactsManager.getAllContacts();
       
        contacts
= new Hashtable[allContacts.length];
//        ContactModel model=new ContactModel(allContacts);
        phoneContact
= new ContactsManager();
       


       
Contact people = new Contact();


       
for (int i = 0; i < allContacts.length; i++) {
            people
= ContactsManager.getContactById(allContacts[i]);
            contacts
[i] = new Hashtable();
           
if(people==null){
            contacts
[i].put("Name", "loading..");
            contacts
[i].put("Number", "loading..");
            contacts
[i].put("Selected", Boolean.FALSE);
             
           
}else{
            contacts
[i].put("Name", people.getDisplayName());
            contacts
[i].put("Number", people.getPhoneNumbers().get("mobile"));
            contacts
[i].put("Selected", Boolean.FALSE);
           
}
            contactsHash
.add(contacts[i]);
       
}


       
return contactsHash;
       
}else{
           
return new Vector<Hashtable>();
       
}
   
}

Any help will be greatly appreciated.

Thanks in Advance,
Steve.

Shai Almog

unread,
Apr 17, 2014, 2:36:14 PM4/17/14
to codenameone...@googlegroups.com
Hi,
that's too much code to go thru. I will need an area that fails to help you here. Ideally a stack trace too.

steve

unread,
Apr 18, 2014, 2:59:37 PM4/18/14
to codenameone...@googlegroups.com
Hi,
I'm actually not sure where exactly the code does not work, as it loads the contacts in the simulator but does not do so on the actual device. In the device the application hangs and after a while a dialog showing null pointer error message appears.
Sadly the error message does not produce a stack trace, it only notifies the user that there has been a null pointer exception.
I know there isn't much to go on but any help will be greatly appreciated.

Shai Almog

unread,
Apr 19, 2014, 4:03:55 AM4/19/14
to codenameone...@googlegroups.com
Hi,
you will need to use logging and narrow down the location of the failure to a specific line.

steve

unread,
Apr 20, 2014, 3:43:37 PM4/20/14
to codenameone...@googlegroups.com
Hi,
im using logging and a try and catch statement on the method that gets the contacts, this is what i get:
[EDT]0:1:6,618-Exception: java.lang.NullPointerException-null
[EDT] 0:3:53,424-Exception: java.lang.NullPointerException-null
[EDT]0:10:3,624-Exception: java.lang.NullPointerException-null

Would you mind explaining this to me, or direct me to a tutorial on such.

Thanks in Advance.

Steve.

Shai Almog

unread,
Apr 21, 2014, 1:38:07 AM4/21/14
to codenameone...@googlegroups.com
Hi,
unless you have a pro account where you can use http://www.codenameone.com/how-do-i---use-crash-protection-get-device-logs.html
You will need to go over every line and print a log there. Then the line that is missing would be the place where the exception is thrown.

steve

unread,
Apr 27, 2014, 6:13:27 AM4/27/14
to codenameone...@googlegroups.com
Hi,

It seems that J2Me does not show Contact.getDisplayName(), that's where the error occurred cause if I use Contact.getFirstName() it works.

Thanks.

Shai Almog

unread,
Apr 27, 2014, 2:43:33 PM4/27/14
to codenameone...@googlegroups.com
Hi,
every entry can be null. You should check if the display name is null and if so use an alternative.
Reply all
Reply to author
Forward
0 new messages