Return an array of objects?

PaulR

Active Member
Licensed User
Longtime User
Hi,

I am in the process of tidying up a project as it gets bigger and am currrently trying to call a subroutine in a code module with.....
B4X:
Dim p(4) As Point

p = Touches.GetDirection(G, p)
.... (G is a global Gestures variable).

In the code module I am.....
B4X:
Sub GetDirection(G As Gestures, p() As Point) As Point
'snip
     return p
'snip
End sub
.... which results in the following compilation error....
B4X:
Compiling generated Java code.          Error
B4A line: 164
p = Touches.GetDirection(G, p)
javac 1.6.0_29
src\jofsoftware\osc\reaperosc\navigationcontrol.java:837: inconvertible types
found   : jofsoftware.osc.reaperosc.navigationcontrol._point
required: jofsoftware.osc.reaperosc.navigationcontrol._point[]
_p = (navigationcontrol._point[])(mostCurrent._touches._getdirection(mostCurrent.activityBA,mostCurrent._g,_p));Debug.locals.put("p", _p);
                                 ^
1 error

What would be the correct syntax for this? p() doesn't work either.

Thanks for any help.
 

Penko

Active Member
Licensed User
Longtime User
Your GetDirection() method returns Point while you are expecting Point().

I suggest that you change your code and return List of Point.

B4X:
Dim points as List : points.Initialize

Dim newPoint as Point
newPoint = GetDirection(params....)
points.add(newPoint)

You can store any object as a List item.

Looping through objects is pretty straightforward too.

B4X:
For i = 0 To points.Size - 1

Dim thisPoint as Point : thisPoint = points.Get(i) ' get the i'th item.

Next

Hope this helps.
 
Upvote 0

PaulR

Active Member
Licensed User
Longtime User
Thanks, the main problem was indeed that I was expecting Point(), that is changed now (as well as the Sub) and the app is compiling but crashing when I try to access something in the returned array....
B4X:
p = Touches.GetDirection(TouchMap,G, p)
   
If p(0).origX > LeftZone.Width Then
... it crashes on that last line. What is causing this?

(The List idea looks interesting, but I need to compare values for different points (moving away from each other, distance etc) so I don't know how well that would work for me. If I can get an accessible array of points returned I will be happy anyway. :) )
 
Upvote 0

Penko

Active Member
Licensed User
Longtime User
You said that it crashes here. How did you get to know that? If you are not sure, there are several options.
1 - use Log commands to store the variable values and have a check on the Logs tab.
- Compile in Debug and put a breakpoint at the suspiscious place.
3 - if it just doesn't compile, copy the error here as you have done above.

Sent from my HTC Desire using Tapatalk
 
Upvote 0

PaulR

Active Member
Licensed User
Longtime User
You said that it crashes here. How did you get to know that? If you are not sure, there are several options.
1 - use Log commands to store the variable values and have a check on the Logs tab.
- Compile in Debug and put a breakpoint at the suspiscious place.
3 - if it just doesn't compile, copy the error here as you have done above.

Sent from my HTC Desire using Tapatalk
Thanks, I have been compiling in debug and when I run it, it is displaying the error message "Program paused on line 166". And showing me the line where I attempt to access the returned array of points (line 166)....
B4X:
If p(0).origX > LeftZone.Width Then
 
Upvote 0

PaulR

Active Member
Licensed User
Longtime User
No, unfortunately not :) , that's just what it says when it runs after compiling in Debug mode.

I have tried compiling it in Release mode and it gives the error...
B4X:
An error has occured in sub:navigationcontrol_onetouch (jave line: 602)
jave.lang.NullPointerException

So it looks like...
B4X:
p = Touches.GetDirection(TouchMap,G, p)
... isn't getting the array of Points I expect it to get?
 
Upvote 0

PaulR

Active Member
Licensed User
Longtime User
Sure thing (as long as you promise not to laugh (too much) at my code), here is the Activity I am trying this from (the troublesome lines are in the Gestures Region, in the OneTouch Sub, followed by the Code Module.....


CODE MODULE
B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim Left, Right, Up, Down As Int
   Up = 0 : Right = 1 : Down = 2 : Left = 3
End Sub

Sub GetDirection(TouchMap As Map, G As Gestures, p() As Point) As Point()
   Dim px, py As Int
   Dim direction As Float
   For i = 0 To TouchMap.Size - 1
      p(i) = TouchMap.GetValueAt(i)
   Next
   px = G.GetX(0)
   py = G.GetY(0)
   
   If p(0).prevX - px > 2 AND Abs(px - p(0).origX) > 5 Then
      direction = Left
      Return p
   Else If px - p(0).prevX > 2 AND Abs(px - p(0).origX) > 5 Then
      direction = Right
      Return p
   Else If py - p(0).prevY > 2 AND Abs(py - p(0).origY) > 5 Then
      direction = Down
      Return p
   Else If p(0).prevY - py > 2 AND Abs(py - p(0).origY) > 5 Then
      direction = Up
      Return p
   End If
End Sub
 
Last edited:
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
There are many cases in which GetDirection will not return anything. Are you sure this is intended behaviour? You might want to return null or something atleast rather than return nothing at all.

Also, this line:
B4X:
p = Touches.GetDirection(TouchMap,G, p)
Here p is a non-primitive and will be passed by ref. If your sub GetDirection modifies 'p' it will change, you do not need to assign it back to 'p' again.
Just want to be sure you know what you are doing.
 
Upvote 0

PaulR

Active Member
Licensed User
Longtime User
There are many cases in which GetDirection will not return anything. Are you sure this is intended behaviour? You might want to return null or something atleast rather than return nothing at all.

Also, this line:
B4X:
p = Touches.GetDirection(TouchMap,G, p)
Here p is a non-primitive and will be passed by ref. If your sub GetDirection modifies 'p' it will change, you do not need to assign it back to 'p' again.
Just want to be sure you know what you are doing.
Thank you very much, it was the (wrong) AND parts of the If statements that I had gleefully added to the working code last night that meant nothing was true so Return p never happened.

I am planning to update the pointer array with values from the subs so thanks for reminding me that non-primitives are passed by reference, it is only a couple of days since I totally knew that. One circuit of the goldfish bowl later....

Thanks again. :)
 
Upvote 0
Top