Catch a java.lang.nullpointer Exception?

RiverRaid

Active Member
Licensed User
Longtime User
Hi!

Im' trying to write a small translation-library so that devs can use the "Standard" - Translation of Android (via the strings.xml in the folder values).

I'm using this function:
B4X:
Sub GetResourceString(Key As String) As String
    Dim r As Reflector
    Dim package As String
    Dim id As Int
      Try
        package = r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
        id = r.GetStaticField(package & ".R$string", Key)
        r.Target = r.GetContext
        r.Target = r.RunMethod("getResources")
        Return r.RunMethod2("getText", id, "java.lang.int")
    Catch 
        ToastMessageShow (NoTransFound & Key,True)
        Return NoTransFound
    End Try
End Sub

If the ressource string is not found in the strings.xml files, the function should return the NoTransFound -String. Instead, it shows me the attached error-message..

Is there a way to catch the error?

Thank you!
 

Attachments

  • error.png
    error.png
    19.4 KB · Views: 262

agraham

Expert
Licensed User
Longtime User
That code looks fine to me and should trap the NoSuchFieldException for an invalid Key, and also any NullPointerException in the Try block.

I guess that the untrapped Exception is being thrown in the Catch block. Look at the generated Java source code under <ProjectName>/Object/src/... with a line numbering editor like Notepad++, find the line specified in the error message and see what is throwing the exception. The Basic4android code for that line will be above it, either as a comment or as a string assignment depending upon the compilation mode.
 
Upvote 0

RiverRaid

Active Member
Licensed User
Longtime User
Thanks for the help!

This line is causing the exception:
B4X:
            ba.setLastException(e17); //BA.debugLineNum = 21;BA.debugLine="ToastMessageShow (NoTransFound & Key,True)";

Do you have any idea what this could be?

The whole block:
B4X:
try { //BA.debugLineNum = 15;BA.debugLine="package = r.GetStaticField(\"anywheresoftware.b4a.BA\", \"packageName\")";
_package = String.valueOf(_r.GetStaticField("anywheresoftware.b4a.BA","packageName"));
 //BA.debugLineNum = 16;BA.debugLine="id = r.GetStaticField(package & \".R$string\", Key)";
_id = (int)(BA.ObjectToNumber(_r.GetStaticField(_package+".R$string",_key)));
 //BA.debugLineNum = 17;BA.debugLine="r.Target = r.GetContext";
_r.Target = (Object)(_r.GetContext(ba));
 //BA.debugLineNum = 18;BA.debugLine="r.Target = r.RunMethod(\"getResources\")";
_r.Target = _r.RunMethod("getResources");
 //BA.debugLineNum = 19;BA.debugLine="Return r.RunMethod2(\"getText\", id, \"java.lang.int\")";
if (true) return String.valueOf(_r.RunMethod2("getText",BA.NumberToString(_id),"java.lang.int"));
 } 
       catch (Exception e17) {
            ba.setLastException(e17); //BA.debugLineNum = 21;BA.debugLine="ToastMessageShow (NoTransFound & Key,True)";
__c.ToastMessageShow(_notransfound+_key,__c.True);
 //BA.debugLineNum = 22;BA.debugLine="Return NoTransFound";
if (true) return _notransfound;
 };
 //BA.debugLineNum = 24;BA.debugLine="End Sub";
return "";
}
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
There is either something very strange happening or something that would be obvious if I could see the whole context. Can you post a complete project that shows this problem so I can see all the detail.

You have written "strings.xml" twice, did you mean this as you are opening the class "string" in your code? I assume that you have a read-only string.xml file in your Objects/res/xml folder - be sure to include that so I have exactly the same code as fails for you.
 
Upvote 0

RiverRaid

Active Member
Licensed User
Longtime User
Hi!

The complete Sourcecode is attached to this post.

The goal for this library is to have use the "natural android" - Translation feature (strings.xml in values-folder) so that the devs are able to use translation-internetservices as getlocalisation.com etc..

Thanks for your help :)
 

Attachments

  • tanslate.zip
    7.5 KB · Views: 185
Upvote 0

RiverRaid

Active Member
Licensed User
Longtime User
Oh my god... :sign0137:

I was so focused on the class that i did'nt check the activity...

THANK YOU!!!!!!!
 
Upvote 0

gehrlekrona

Member
Licensed User
Longtime User
I have just started getting this error too in the activity_resume and all it does is to initialize the camera and set the orientation. It used to work and now I don't have a clue to what's wrong.
The code is like Camera1.initialize(Panel1, "Camera1")
The Panel1 is on a designer and is initialized. What do I need to look for?
Another thing, is there a way to log to the phone so I can see what happened?
 
Last edited:
Upvote 0

eps

Expert
Licensed User
Longtime User
Check to see if the item you are initialising isn't already initialised...

if(blah.isinitialised is false) then
initialise blah
end if
 
Upvote 0
Top