Possible bug in code compilation

Penko

Active Member
Licensed User
Longtime User
Hello!

I wrote the following sub:

- this sub is located in a code Module Common
B4X:
   Sub loadToListView(theListView As ListView)
      
      Dim result As Cursor
      Dim i As Int
      
      Dim clmList As String
   
      
      result = loadDBFiles
      
      ' Adding the rows below makes the compiler break during Java compilation
      
       For i = 0 To result.RowCount - 1
         
         result.Position = i
         
         clmList = result.GetString("LIST")
         
         theListView.AddSingleLine2(clmList, 1)
         
       Next i 
       
       ' End of rows that the compiler refuses to compile
      
      result.Close
      
   End Sub

If I add one of these rows, the compiler fails to compile successfully.

B4X:
 For i = 0 To result.RowCount - 1
         
         result.Position = i
         
         clmList = result.GetString("LIST")
         
         theListView.AddSingleLine2(clmList, 1)
         
       Next i

The error:

Compiling code. 0.23
Generating R file. 0.01
Compiling generated Java code. Error
B4A line: 346
theListView.AddSingleLine2(clmList, 1)
javac 1.6.0_23
src\com\1\2\common.java:841: not a statement
}_i;
^

Please note that if I remove the .AddSingleLine2, then the error will be for GetString.

Is this a bug or I am doing things in a wrong way?

If it matters, here is the code for loadDBFiles()

B4X:
Sub ExecuteForResult(query As String) As Cursor
   Dim cursor1 As Cursor
   
   Common.LogT("RESULT QUERY: " & query, "QUERY")
   
   Try
      cursor1 = SQL1.ExecQuery(query)
   Catch
      Common.LogT("BAD RESULT QUERY: " & query & " --- " & LastException, "QUERY")
   End Try
   
   Return cursor1
   
End Sub

SQL1, ExecNonQuery, ExecuteForReusult as located in a Code Module "SQLL", my LIST db field is "VARCHAR(100)".


According to my logs, the DB is initialized.

But, this is a compiling issue. Haven't noticed the compiler to refuse to compile because of logical errors.

Edit: The generated JAVA source code is :
B4X:
public static String  _loadtolistview(anywheresoftware.b4a.BA _ba,anywheresoftware.b4a.objects.ListViewWrapper _thelistview) throws Exception{
anywheresoftware.b4a.sql.SQL.CursorWrapper _result = null;
int _i = 0;
String _clmlist = "";
 //BA.debugLineNum = 328;BA.debugLine="Sub loadToListView(theListView As ListView)";
 //BA.debugLineNum = 330;BA.debugLine="Dim result As Cursor";
_result = new anywheresoftware.b4a.sql.SQL.CursorWrapper();
 //BA.debugLineNum = 331;BA.debugLine="Dim i As Int";
_i = 0;
 //BA.debugLineNum = 333;BA.debugLine="Dim clmList As String";
_clmlist = "";
 //BA.debugLineNum = 336;BA.debugLine="result = loadDBFiles";
_result = _loaddbfiles(_ba);
 //BA.debugLineNum = 340;BA.debugLine="For i = 0 To result.RowCount - 1";
{
final double step193 = 1;
final double limit193 = (int)(_result.getRowCount()-1);
for (_i = (int)(0); (step193 > 0 && _i <= limit193) || (step193 < 0 && _i >= limit193); _i += step193) {
 //BA.debugLineNum = 342;BA.debugLine="result.Position = i";
_result.setPosition(_i);
 //BA.debugLineNum = 344;BA.debugLine="clmList = result.GetString(\"LIST\")";
_clmlist = _result.GetString("LIST");
 //BA.debugLineNum = 346;BA.debugLine="theListView.AddSingleLine2(clmList, 1)";
_thelistview.AddSingleLine2(_clmlist,(Object)(1));
 }
}_i; // THIS IS common:841 ********************
 //BA.debugLineNum = 352;BA.debugLine="result.Close";
_result.Close();
 //BA.debugLineNum = 354;BA.debugLine="End Sub";
return "";
}
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
Hi,

you don't need to specify the index in the Next statement of the For Next loop. So
B4X:
Next i
should be just
B4X:
Next
.
 

Kevin

Well-Known Member
Licensed User
Longtime User
I don't really know the reasons for it, but this kind of thing happens pretty frequently for me, where it gives you a line that is supposedly in error but it is another line that is actually causing the error.

I think for me the most common cause of these erroneous errors (ha ha) is when I have several nested If / EndIf statements and I forget an EndIf. Usually when I am modifying a bunch of nested If / EndIf statements that I had previously done and I am adding more. It tends to get complicated sometimes when nesting several If / Else / EndIf inside other If / Else / EndIf and for some reason those faint lines that 'connect' them do me no good. :D

Erel (or someone else who may know):
Out of curiosity, why do we get these error messages that are telling us the wrong line that caused it?
 

Kevin

Well-Known Member
Licensed User
Longtime User
Your explanation makes perfect sense. I don't happen to have an example right now but if I remember I will post one on the forum here next time it happens.

I'm sure it would be pretty complicated, but along the lines of syntax highlighting (that B4A now already does), would it be possible to show If statements as red (or some other color) if no accompanying EndIf is found before an End Sub?

Also handy would be the ability to select an IF statement and have the code editor automatically highlight the accompanying EndIf or vice-versa but I imagine that would be much harder still.

Anyhoo, I'll submit a sample next time I run into it. It's usually If/EndIf as I mentioned above but I have seen it happen on other occasions as well.
 
Top