B4J Question [solved][B4XPages] Save & Restore pages error?

fredo

Well-Known Member
Licensed User
Longtime User
This solution is really helpful and easy to implement:

The sample project saves the last position correctly, but throws an error when you close the form:

B4X:
Public Sub SavePagePosition (Page As Object)
    Log($"Sub SavePagePosition, B4XPages.GetPageId(Page): ${B4XPages.GetPageId(Page)}"$)
    kvs.Put(B4XPages.GetPageId(Page), FormToPP(B4XPages.GetNativeParent(Page)))  
End Sub

Public Sub LoadPagePosition (Page As Object)
    Dim f As Form = B4XPages.GetNativeParent(Page)
    Dim pp As PagePosition = kvs.Get(B4XPages.GetPageId(Page))
    If pp = Null Then Return
    SetFormFromMap(pp, f)
End Sub

Private Sub FormToPP (f As Form) As PagePosition
    Log($"Sub FormToPP, f.WindowLeft: ${f.WindowLeft}"$)  ' <yyyyyyyyyyyyyyyyyyyyy <<<<<<<<<<<<<< LINE 55 
    Return CreatePagePosition(f.WindowLeft, f.WindowTop, f.WindowWidth, f.WindowHeight, f.As(JavaObject).GetFieldJO("stage").RunMethod("isIconified", Null))
End Sub

Waiting for debugger to connect...
Program started.
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
Sub SavePagePosition, B4XPages.GetPageId(Page): mainpage
Sub FormToPP, f.WindowLeft: 1783.0
Sub SavePagePosition, B4XPages.GetPageId(Page): page 2
Error occurred on line: 55 (B4XMainPage)
java.lang.NullPointerException
at b4j.example.b4xmainpage._formtopp(b4xmainpage.java:267)
at b4j.example.b4xmainpage._savepageposition(b4xmainpage.java:106)
at b4j.example.b4xpage2._b4xpage_background(b4xpage2.java:101)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:504)
at anywheresoftware.b4a.keywords.Common.CallSubNew(Common.java:451)
at b4j.example.b4xpagesmanager._backgroundstatechanged(b4xpagesmanager.java:740)
at b4j.example.b4xpagesmanager._mainform_closed(b4xpagesmanager.java:501)
at b4j.example.b4xpagesdelegator._mainform_closed(b4xpagesdelegator.java:49)
at b4j.example.main._mainform_closed(main.java:98)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

Is there anything I could try here?
 
Last edited:

fredo

Well-Known Member
Licensed User
Longtime User
It seems that for some reason f is null.

This is my temporary workaround:
B4X:
Private Sub FormToPP (f As Form) As PagePosition
    If f = Null Then
        Return CreatePagePosition(0, 0, 400dip, 600dip, False)
    Else
        Return CreatePagePosition(f.WindowLeft, f.WindowTop, f.WindowWidth, f.WindowHeight, f.As(JavaObject).GetFieldJO("stage").RunMethod("isIconified", Null))
    End If
End Sub

It works, but it's not very elegant...
 
Upvote 0
Top