Null pointer exception on rotate screen with image fields in blocks.

90 views
Skip to first unread message

Jorge gusa

unread,
May 20, 2017, 1:58:02 PM5/20/17
to Blockly
Hello:

I am usingo blockly with Android device generating blocks with JSON structure. Like this:
[
 
{
   
"type": "light",
   
"message0": "%1 Light",
   
"args0": [
     
{
       
"type": "field_image",
       
"src": "images/led.png",
       
"width": 30,
       
"height": 30,
       
"alt": "*"
     
}
   
]
]

I have seen that the system is working properly until I have added "field_image" arguments. When I add a block and rotate the tablet I get next exception:

05-20 19:44:06.342 31683-31683/com E/AndroidRuntime: FATAL EXCEPTION: main
 
Process: com, PID: 31683
 java
.lang.NullPointerException: Attempt to invoke virtual method 'int com.google.blockly.model.FieldImage.getWidth()' on a null object reference
     at com
.google.blockly.android.ui.fieldview.BasicFieldImageView.updateViewSize(BasicFieldImageView.java:174)
     at com
.google.blockly.android.ui.fieldview.BasicFieldImageView$2.onPostExecute(BasicFieldImageView.java:141)
     at com
.google.blockly.android.ui.fieldview.BasicFieldImageView$2.onPostExecute(BasicFieldImageView.java:117)
     at android
.os.AsyncTask.finish(AsyncTask.java:651)
     at android
.os.AsyncTask.-wrap1(AsyncTask.java)
     at android
.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
     at android
.os.Handler.dispatchMessage(Handler.java:102)
     at android
.os.Looper.loop(Looper.java:148)
     at android
.app.ActivityThread.main(ActivityThread.java:5417)
     at java
.lang.reflect.Method.invoke(Native Method)
     at com
.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
     at com
.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


Could you help me?. Thank you very much.

Andrew n marshall

unread,
May 21, 2017, 2:29:44 AM5/21/17
to blo...@googlegroups.com
While I did find another issue (#604), was not able to recreate your described issue. I added your block definition to our DevTests activity, both with "images/led.png" present in assets/ and not, and could not reproduce your error when rotating the device (emulator). I was able to rotate it without error in both master and develop branches, on APIs 19 and 25.

Looking at the source of BasicFieldImageView, it is hard to image how the mImageField in updateViewSize() could be null for either call. That said, it shouldn't hurt to add a if (mImageField != null) around the contents of updateViewSize(), if you wanted to.

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jorge

unread,
May 21, 2017, 5:34:34 AM5/21/17
to blo...@googlegroups.com
Hello:

Thank you very much. There is no bug in Blockly. The problem is the way I was using Activity Lifecicle. I have put
protected void  onCreate(Bundle savedInstanceState){
 loadWorkspaceFromAppDir
(fileName);
}

This is WRONG. Each time that the screens rotates it reload the file so it forces the image to unlink  "unlinkField()" that is inside "BasicFieldImageView.java" . Once the image needs to the size here you have the null pointer.

It is not necessary to reload the file each you call it. Just doing:

protected void  onCreate(Bundle savedInstanceState){
if(savedInstanceState == null){
  loadWorkspaceFromAppDir
(fileName);
}
}

It will control if it is the first time or not you call it. Thank you very much. Please mark this error as closed due to is not a real bug.

CLOSED

Jorge



--
You received this message because you are subscribed to a topic in the Google Groups "Blockly" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/blockly/lC91XADUiI4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to blockly+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages