'TypeError: 'builtin_function_or_method' object is not subscriptable' in binary searches - typeerror

For schoolwork I have to do binary searches, I followed the steps but I got the following error:
Traceback (most recent call last): File "X:/Computer
Science/Python/February 2018/25 02 18/binary searching.py", line 15,
in
end = aList.index[middle] TypeError: 'builtin_function_or_method' object is not subscriptable
Here is my code:
aList = [3,13,24,27,31,39,45,60,69]
end = len(aList) - 1
found = 0
start = 0
target = int(input("Please input the item you would like to search for:\t>"))
while start <= end and found == 0:
middle = int((start + end) / 2)
if aList[middle] == target:
found = 1
print(target , "is in the list.")
if target < aList[middle]:
end = aList.index[middle]
else:
start = aList.index[middle]
if found == 0:
print("The search item was not found.")

See here In Python, what does it mean if an object is subscriptable or not?
and I paraphrase: "[to be subscriptable object must] implement the getitem() method"
index[middle] means "pull the value from dictionary index that has key equals to the value of variable middle"
Line 15 should look like as bellow - a function call:
end = aList.index(middle)

Related

VBA Compile error inside if statement

If IsArray(payCsv(pay_id)) = False Then
'create tempArray
lc = 0
Debug.Print "create array"
End If
If IsArray(payCsv(pay_id)) = True Then
Debug.Print " array exists, we should be able to get ubound"
lc = UBound(payCsv(0)) - LBound(payCsv(0))
l = l + 1
End If
I am using the above code to determine whether I can use Ubound on my 2D array (i.e. if the 2nd dimension is created, get length (ubound - lbound).
However, I am getting a compile error, even though condition 2 is false, it does not recognise that the code will not be relevant.
I am testing one array and the result is if I comment out "lc = UBound(payCsv(0)) - LBound(payCsv(0))" is "create array".
If I leave this line in there, I get the error "compile error - expected array"
Is this a bug in VBA?
If you want to access the UBound of the 2nd dimension of an array, the format goes like this:
UBound(payCSV, 2)
The MSDN page on this function may be helpful.
When you access payCSV(0) as you currently are, the code assumes that you want the 1st element within the 1st dimension of the payCSV array.
Perhaps you might want to try this?
If IsArray(payCsv(pay_id)) = False Then
'create tempArray
lc = 0
Debug.Print "create array"
Else
Debug.Print " array exists, we should be able to get ubound"
lc = UBound(payCsv, 2) - LBound(payCsv, 2)
l = l + 1
End If

Error: Conversion from type 'DBNull' to type 'String' is not valid

I'm working on a project where I get data from a database.
Now I get this error message:
Conversion from type 'DBNull' to type 'String' is not valid
I know what the error means, I just need a way to fix it.
I have the following code:
Private Function ParseSeamansNames(ByVal seaman As ItemsDataSet.SeamenRow) As String
If (seaman Is Nothing) Then Return String.Empty
Dim name As String
Dim firstNames() As String = Split(seaman.FirstName.Replace("-", " "))
Dim index1 As Integer = CInt(seaman.GivenNameNumber.Substring(0, 1))
Dim index2 As Integer = CInt(seaman.GivenNameNumber.Substring(1, 1))
If (index1 > firstNames.Length - 1) Then
index1 = 0
End If
If (index2 > firstNames.Length - 1) Then
index2 = 0
End If
If (index1 = 0 And index2 = 0) Then
name = seaman.FirstName
ElseIf (index1 > 0 And index2 = 0) Then
name = firstNames(index1 - 1)
ElseIf (index1 = 0 And index2 > 0) Then
name = firstNames(index2 - 1)
Else
name = firstNames(index1 - 1) & "-" & firstNames(index2 - 1)
End If
name = name & " " & seaman.LastName
Return name
End Function
I tried to change it to If (seaman Is Nothing) Then Return DBNull but I get error:
dbnull is a type and cannot be used as an expression
I don't really know how to fix it. Can anyone help me?
UPDATE:
I get this error:
[InvalidCastException: Conversion from type 'DBNull' to type 'String'
is not valid.]
Microsoft.VisualBasic.CompilerServices.Conversions.ToString(Object
Value) +715847
BUMSSeamenWebb.ItemsService.SeamenRow.get_FirstName() in C:\BUMS
LOKAL\Dev\Projects\BUMSSeamenWebb\Web
References\ItemsService\Reference.vb:51036
That line is this code:
<Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), _
Global.System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")> _
Public Property FirstName() As String
Get
Try
Return CType(Me(Me.tableSeamen.FirstNameColumn),String)
Catch e As Global.System.InvalidCastException
Throw New Global.System.Data.StrongTypingException("The value for column 'FirstName' in table 'Seamen' is DBNull.", e)
End Try
End Get
Set
Me(Me.tableSeamen.FirstNameColumn) = value
End Set
End Property
In particular:
Return CType(Me(Me.tableSeamen.FirstNameColumn),String)
I can't see the problem here
UPDATE 2:
This is another function that checks other columns
Private Sub SetSeamanData(ByRef itemRow As ItemsDataSet.ItemsRow, ByVal seaman As ItemsDataSet.SeamenRow)
itemRow.PersonalIdentityNumber = seaman.PersonalIdentityNumber
itemRow.Name = ParseSeamansNames(seaman)
itemRow.CitizenShipCode = seaman.CitizenshipCode
If Not seaman.IsEmailNull() Then
itemRow.EmailAddress = seaman.Email
Else
itemRow.EmailAddress = Nothing
End If
If Not seaman.IsTelephoneNull() Then
itemRow.TelephoneNumber = seaman.Telephone
Else
itemRow.TelephoneNumber = Nothing
End If
If Not seaman.IsMobiletelephoneNull() Then
itemRow.MobilephoneNumber = seaman.Mobiletelephone
Else
itemRow.MobilephoneNumber = Nothing
End If
End Sub
These generated DataRow classes are coded in such a way that if you try to read a property (column value) that has a database null value, it will throw an exception.
You are expected to check for null before attempting to read from it.
As you can tell from the message, in this case it's complaining that you are trying to read the value of FirstName, but it's null (DbNull).
Usually, when a column can be null, these DataRow classes also include a convenience function to check for null on that specific column. So in this case, ItemsDataSet.SeamenRow should also have a IsFirstNameNull function. If so, call that first, and handle that specific case. If IsFirstNameNull returns false, then you can safely read from the FirstName property.
If you have other columns that can be null in your database, you will need to follow a similar pattern.

Runtime error 5 while using Mid()

I am working with string in vba. I would like to do something like this:
Dim matchprodat, matchtxt As MatchCollection
rega2lhex.Pattern = "[\\][\w]*"
regprodat.Pattern = "(Datensatz:|Projekt:)[\s\w*,*]*[\\\w]*"
\\ I am reading each line of a text file
Set matchprodat = regprodat.Execute(line)
If matchprodat.Count <> 0 Then
Set matcha2lhex = rega2lhex.Execute(matchprodat.Item(0))
DCMname = DCMfilename + "<>" + Mid(matcha2lhex.Item(0), 2)
End if
but I am getting the error: Run-time error '5': "Invalid procedure call or argument"
the matcha2lhex.Item(0) is not empty and has the length of 8. What cause this error in Mid()?
If I am watching the value:

No database error on BlockInsert VBA AutoCad

I'm using AutoCAD 2013 and I want to import block from another file. I wrote the code below:
Dim Zero(0 To 2) As Double
Dim i As Integer
For i = 0 To 2
Zero(i) = 0
Next i
Dim BlockRef As AcadBlockReference
FileToInsert = "D:\blocks.dwg"
Set BlockRef = ThisDrawing.ModelSpace.InsertBlock(Zero, FileToInsert, 1#, 1#, 1#, 0)
but I get the following error:
Run-time error '-2145386390 (8020006a)'
No database
I find it very strange because the same code worked a week ago and I did't touch none of the files since then...
If I open blocks.dwg there are a few blocks in it, just as they should.
If I comment out this part of the code, the same error seems to move to the next
Set ... = ThisDrawing.ModelSpace. ...
I would very much appreciate your help,
Špela
If the drawing already contains the block, you need to call InsertBlock with the block name only. Something like this:
Dim exist as Boolean
exist = False
For i = 0 To ThisDrawing.Blocks.Count - 1
If StrComp(ThisDrawing.Blocks.Item(i).Name, "blocks", vbTextCompare) = 0 Then
exist = True
Exit For
End If
Next i
ThisDrawing.ModelSpace.InsertBlock(point, IIf(exist, "blocks", "D:\blocks.dwg"), 1, 1, 1, 0)

Problem storing value in array

I want to store value in array, from my database. I am using following code but it return error: "Object reference not set to an instance of an object."
Code is:
Dim w as integer=0
Do While DsChooseSQsNow.tblChooseSQs.Rows.Count > w
vrSQsNoChosen(w) = DsChooseSQsNow.tblChooseSQs.Rows(w).Item("QNo")
vrTotalSQsChosen = vrTotalSQsChosen + 1
w = w + 1
Loop
Error comes on "vrSQsNoChosen(w) = DsChooseSQsNow.tblChooseSQs.Rows(w).Item("QNo")"
try to print the value DsChooseSQsNow.tblChooseSQs.Rows(w).Item("QNo")
or debug the code
"Object reference not set to an instance of an object." means Item("QNo") may be null
There are many reasons why this code fails. I would start checking from:
Size (and boundings) of vrSQsNoChosen array.
w value when error occurs (is valid index for vrSQsNoChosen array and Rows collection?).
Item("QNo") value when error occurs (maybe is null?).
Also -- VB.NET implements += operator so you can write:
w += 1
instead of:
w = w + 1