B4J Question Exception loading data into TableView

TorontoJim

Member
Licensed User
Longtime User
The problem du jour is the error:

B4X:
java.lang.ArrayIndexOutOfBoundsException: 2

It is occuring when I try and add data to a tableview, specifically this:

B4X:
Sub LoadKVSData
	tableDataView.Items.Clear
	Dim listKeys As List
	listKeys.Initialize
	listKeys = kvs.ListKeys
	For i = 0 To listKeys.Size - 1
		Dim Row(2) As Object
		Row(0) = listKeys.Get(i)
		If txtPassword.Text <> "" Then
			Row(1) = kvs.GetEncryptedObject(listKeys.Get(i), txtPassword.Text)
		Else
		    Row(1) = kvs.GetSimple(listKeys.Get(i))
		End If
		tableDataView.Items.Add(Row)
	Next
	refreshTableView
End Sub

I've used Log statements to follow what's happening to figure out that the exception occurs with:

B4X:
tableDataView.Items.Add(Row)

I'm only adding two objects to the TableView record, there are only two items being assigned, there are only two columns defined.

I have also used Log statements to test the data being populated, to ensure there actually is content there.

Below is the full code, showing the TableView declaration and setting up the columns (only two of them)

B4X:
#Region  Project Attributes 
	#MainFormWidth: 950
	#MainFormHeight: 600 
	#AdditionalJar: sqlite-jdbc-3.7.2
	#AdditionalJar: bcprov-jdk15on-154
#End Region

Sub Process_Globals
	Private fx As JFX
	Private MainForm As Form
	Private kvs As KeyValueStore
	Private dialogue8 As Dialogs8
	Private strExistingFilePath As String
	Private tableDataView As TableView
	Private txtPassword As TextField
	Private txtFileField As TextField
	Private btnSelectFile As Button
	Private btnReload As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
	MainForm = Form1
	MainForm.SetFormStyle("UNIFIED")
	MainForm.RootPane.LoadLayout("main_form") 'Load the layout file.
	MainForm.Show
	dialogue8.Initialize
	tableDataView.SetColumnHeader(0, "Key")
	tableDataView.SetColumnHeader(1, "Contents")
	tableDataView.SetColumnWidth(0, 200.00)
	tableDataView.SetColumnSortable(0, True)
	tableDataView.SetColumnSortable(1, True)
End Sub

Sub GetDataFolder (AppName As String) As String
    Dim os As String = GetSystemProperty("os.name", "").ToLowerCase
    If os.Contains("win") Then
    	Dim wf As String = File.Combine(GetEnvironmentVariable("AppData", ""), AppName)
        File.MakeDir(wf, "")
        Return wf
	Else
		Return File.DirApp
	End If
End Sub

Sub btnSelectFile_Action
	LoadFile
End Sub

Sub btnReload_Action
	If txtFileField.Text <> "" Then
		Dim strFilePath As String = txtFileField.Text
		If strFilePath <> "" Then
			txtFileField.Text = strFilePath
		    Dim strPath As String
			Dim strFile As String
			strPath = File.GetFileParent(strFilePath)
			strFile = File.GetName(strFilePath)
			If File.IsDirectory(strPath, strFile) = True Then
				fx.Msgbox(MainForm, "You must select a file to import.", "No File Selected")
			Else
				strExistingFilePath = strPath
				If kvs.IsInitialized = True Then
					kvs.Close
				End If
				kvs.Initialize(strPath, strFile)
				LoadKVSData
			End If
		End If
	End If
End Sub

Sub LoadFile
    Dim saveopenFile As FileChooser
	saveopenFile.Initialize
	If strExistingFilePath <> "" Then
	    saveopenFile.InitialDirectory = strExistingFilePath
	Else
		saveopenFile.InitialDirectory = GetDataFolder("KVSViewer")
	End If
	Dim strFilePath As String
	strFilePath = saveopenFile.ShowOpen(MainForm)
	If strFilePath <> "" Then
		txtFileField.Text = strFilePath
	    Dim strPath As String
		Dim strFile As String
		strPath = File.GetFileParent(strFilePath)
		strFile = File.GetName(strFilePath)
		If File.IsDirectory(strPath, strFile) = True Then
			fx.Msgbox(MainForm, "You must select a file to import.", "No File Selected")
		Else
			strExistingFilePath = strPath
			If kvs.IsInitialized = True Then
				kvs.Close
			End If
			kvs.Initialize(strPath, strFile)
			LoadKVSData
		End If
	End If
End Sub

Sub LoadKVSData
	tableDataView.Items.Clear
	Dim listKeys As List
	listKeys.Initialize
	listKeys = kvs.ListKeys
	For i = 0 To listKeys.Size - 1
		Dim Row(2) As Object
		Row(0) = listKeys.Get(i)
		If txtPassword.Text <> "" Then
			Row(1) = kvs.GetEncryptedObject(listKeys.Get(i), txtPassword.Text)
		Else
		    Row(1) = kvs.GetSimple(listKeys.Get(i))
		End If
		tableDataView.Items.Add(Row)
	Next
	refreshTableView
End Sub

Sub refreshTableView
    ' Use a temporary list which holds the updated row (cells) objects as taken from the updated tableview list
    Dim tempData As List
    tempData.Initialize
    tempData.AddAll(tableDataView.Items)
    ' Clear all items in the tableview list
    tableDataView.Items.Clear
    ' And add the objects from the list to the tableviewStandard items
    For i = 0 To tempData.Size - 1
        Dim r() As Object = tempData.Get(i)
        tableDataView.Items.Add(Array As Object(r(0), r(1)))
    Next
    ' Set the selected row
    tableDataView.SelectedRow = 0
End Sub

I've used TableView in other small projects and not had this kind of problem with it. Using nearly identical code (for the loading of data to the tableview). This is telling me there is something not obvious (to me) that is causing this. I'm open to any suggestions to try out.

Image of the error is attached.
 

Attachments

  • kverr5.png
    kverr5.png
    22.5 KB · Views: 204
  • kvs_viewer_project.zip
    64.6 KB · Views: 224

TorontoJim

Member
Licensed User
Longtime User
I only added two names in the AppStart sub ... and with your question I had one of those flashbulb moments. I checked the designer. I never removed the THREE default column names (Column 1, Column 2, Column 3) from the controls properties. That is what the problem was ... and explains the error.

Thank you.
 
Upvote 0
Top