Android Question Error: object should first be initialized

MarcioCC

Member
Licensed User
Longtime User
Good evening, I have the following code that is generating the following error: object should first be initialized.
What am I doing wrong??


B4X:
Sub BtnGeraInv_Click

Qry="Select * from PRODUTOS order by CODBARRAS,QUANTIDADE"
Cursor1=SQL.ExecQuery(Qry)
If Cursor1.RowCount=0 Then
   Cursor1.Close
   Return
End If

TextWriter1.Initialize(File.OpenOutput(File.DirDefaultExternal, "coletor.csv", False))

For i = 0 To Cursor1.RowCount-1
   Cursor1.Position=i
   CODBARRAS= Cursor1.GetString("CODBARRAS") 
   QUANTIDADE = Cursor1.GetString("QUANTIDADE") 
   
   TextWriter1.Write(CODBARRAS & "," & QUANTIDADE & Chr(13) & Chr(10))
   
   'TextWriter1.Write(Datex & "," & CC & "," & PC & "," & Qty & "," & Cost & "," & Sent & Chr(13) & Chr(10))
   
Next
TextWriter1.Flush
TextWriter1.Close
Cursor1.Close
 

mangojack

Expert
Licensed User
Longtime User
Can you post the full error line .. (right click > copy) . It should hint what object is not initialized.
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
No .. can you post the full LOG error message ..(right click > copy)

Also , where have you declared / initialised the Cursor1 and SQL object ?
 
Upvote 0

MarcioCC

Member
Licensed User
Longtime User
Follows the image of the error message that is giving
 

Attachments

  • Screenshot_2017-07-17-00-16-13.png
    73.8 KB · Views: 281
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Can you upload your project .. IDE menu File > Export asZip
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Before doing anything.. Does your project include a Starter service ?
If not , Add new service module . Name it 'Starter'
Declare and initialise your SQL object and cursor to this service. (see post #5) Other modules can reference the objects with Starter.SQL etc.
If this does not solve your problem, maybe upload and supply link to external file transfer option .. Dropbox etc.
 
Upvote 0

MarcioCC

Member
Licensed User
Longtime User
I can not declare the SQL object and Cursor in the starter module I declare and still gives the error (java.lang.RuntimeException: Object should first be initialized.)
How do I declare and initialize the object in the starter module ??
My project is attached (see post #12)
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
I could not run the project. All layouts are missing and the ZXScanner lib did not like my setup .. Missing B4a Core files ??
I have moved SQL object to Starter Service. Add your images and main_coleta layout to the project and see if it errors.

I cannot see anything else in the BtnGeraInv click that could cuase the error.
 

Attachments

  • ZXScannerTest.zip
    8.4 KB · Views: 214
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Unless you need to use the results from a single query across multiple subs (which it seems you don't in your code), you should probably declare Cursor1 locally in each sub where you execute a query. This will ensure that the cursor is only created where it's needed & will then be destroyed when the sub exits, so you don't have the overhead of a globally declared cursor, plus you don't run the risk of the cursor inadvertently having data from the wrong query in it.

Also - mangojack is right. Your issue was that although you were declaring SQL1 in your main activity, you weren't initializing it - ie:
B4X:
SQL1.Initialize(Dir as String, FileName as String, CreateIfNecessary as Boolean)

You don't have to declare it in Starter, however doing so means that you can access the one instance of it from any activity, class or code module in your app.

- Colin.
 
Upvote 0

MarcioCC

Member
Licensed User
Longtime User
One more question the view listview has some supported limit for information it can handle more than 15,000 records ??
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
The Listview can handle very large lists .. but do you really need to display all records at once ? ... will the user really scroll through 15,000 + records ?

Maybe look at loading a minimum amount of records and should the user want more .. load more. There are numerous ways you could do this.

This might be of interest .. Start a new thread for any futher questions you might have.
CustomListView with Pull to Refresh ...
 
Upvote 0

MarcioCC

Member
Licensed User
Longtime User
Good night friend, the app will work like this, it will read the product barcode and play in the listview only this, it will have 2 fields (BARCODE, QUANTITY) listed in the listview.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
should be no problem. I aggree with @mangojack that there is no reason to display 15k+ records in a listview. It may be not funny for the user to search a item in 15.000 lvitems... Or even to have 15k items displayed at all.
I would definetively limit it and load 100 more if the end is reached or so
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…