Cancelling Camera on J2ME causes nullPointerException

23 views
Skip to first unread message

cubiq1

unread,
Aug 4, 2014, 11:01:46 AM8/4/14
to codenameone...@googlegroups.com
Hi,

The following code causes a null pointer exception when running on my Nokia N8, but other platforms work fine:

        // Take photo checkbox
        findTakePhotoWaterCheckBox
().addActionListener(new ActionListener() {
           
           
String filePath = "";
           
           
// When checkbox is clicked
           
@Override
           
public void actionPerformed(ActionEvent evt) {

               
if (findTakePhotoWaterCheckBox().isSelected()) {
                   
                   
// Capture photo at resolution ? x 768 (preserving sensor aspect ratio)
                    filePath
= Capture.capturePhoto(1024, -1);
                   
                   
// Photo captured
                   
if (filePath != null) {
                       
Dialog.show("Camera", "Photo successfully captured: " + filePath, "Okay", null);
                       
                   
// Photo not captured
                   
} else {
                       
                       
// PROBLEM CAUSED BY THIS LINE
                        findTakePhotoWaterCheckBox
().setSelected(false);
                   
}
               
}
           
}
       
});

I have a good feeling it's because when I cancel the camera without taking a photo the code tries to set the CheckBox selected to false.

Is there any work around for this?

Thanks

cubiq1

unread,
Aug 4, 2014, 2:40:56 PM8/4/14
to codenameone...@googlegroups.com
The following method acts as a work around.

For some reason on the Nokia N8 findTakePhotoCheckBox.setSelected(false) is called before the form is even shown, whereas on other platforms this isn't the case.

private void untickTakePhotoCheckBox(final String formName) {
       
       
if (!formName.equals(Display.getInstance().getCurrent().getName())) {
           
           
Display.getInstance().callSerially(new Runnable() {
               
public void run() {
                    untickTakePhotoCheckBox
(formName);
               
}
           
});
           
return;
       
}
       
        findTakePhotoWaterCheckBox
().setSelected(false);
   
}

Cheers

Shai Almog

unread,
Aug 5, 2014, 1:28:53 AM8/5/14
to codenameone...@googlegroups.com
Always use the find* method that takes a component as an argument.

cubiq1

unread,
Aug 5, 2014, 3:36:40 AM8/5/14
to codenameone...@googlegroups.com
Thanks Shai, your suggestion is a much better option and works perfectly.
Reply all
Reply to author
Forward
0 new messages