cancel
Showing results for 
Search instead for 
Did you mean: 

Passing an array to a funcitn keeps crashing - Argument 'Array' is Nothing!

Phenom
Phenom

 

To all

 

I am passing an array of data extracted from a record in an .afu to extract specific data and the function called keeps crashing with the error: Parameter Name: Argument 'Array' is Nothing

and yet all the (simple) checks, see below, I can think off seems to suggest that the array is fully populated

 

'CHECK 1
if FxData is Nothing then
  theLW.WriteLine ("--- FxData array is nothing")
else
 theLW.WriteLine ("--- FxData array is NOT nothing")	
END IF

'CHECK 2 - BRUTE FORCE. LIST SOME OF THE DATA
For i = LBound(FxData) To 20 'UBound(FxData)
 theLW.WriteLine ("--- FxData " & FxData(i))
Next
	

 extract of the code which works

 

Dim theXvalues() As Double
Dim theYvalues() As Double
Dim TData(),FxData() As Double
'EXTRACT data from the required record
theSession.AfuManager.GetAfuData(pathAFUFile, RecordID, myAfuData)theYvalues = myAfuData.GetRealData(theXvalues)

'sise the arrays TData and FxData
'and transfer all the values using the function FnCopyArray()
ReDim TData(UBound(theXvalues))
ReDim FxData(UBound(theYvalues))
TData=FnCopyArray(TheXvalues)
FxData=FnCopyArray(TheYvalues)


Function FnCopyArray(ByRef InputArray() As Double) As Double()
'The function copy all the value from the InputArray() to the array ArrayToPass ()
Dim i As Long
Dim ArrayToPass() as Double
ReDim ArrayToPass(UBound(InputArray))
For i = LBound(InputArray) To UBound(InputArray)
  ArrayToPass(i) = InputArray(i)		
Next I
Return  ArrayToPass 
End Function

the programm crashes when I called a 2nd function : GetMaxMin(FxData)

 

 

Function GetMaxMin(ByRef InputArray() As Double) As Double()
   Dim MaxValue, MinValue As Long
   Dim i, RankMax, RankMin As Long
   Dim DataToReturn(0 To 3) as Double
 
   For i = LBound(InputArray) To UBound(InputArray)
         'do something
   Next i
 End Function

 

Could this be due to the fact that I gave the array FxData() a specific size (using the Redim)?  The FnCopyArray() works fine! All the testing I did to develope the code worked fine

 

Any hints/suggestins/corrections are welcome

 

Thanks

 

Regards

 

JXB

 

 

Production: NX9.0.3.4, NX10.0.2.6
Development: VB.NET (amateur level !)
7 REPLIES

Re: Passing an array to a funcitn keeps crashing - Argument 'Array' is Nothing!

Siemens Phenom Siemens Phenom
Siemens Phenom

I don't see where you are populating theXvalues().

 

Why do you need a custom function to copy an array?

Re: Passing an array to a funcitn keeps crashing - Argument 'Array' is Nothing!

Phenom
Phenom

JimB

 

theXvalues is an output from (and seems to work!)

Dim myAfuData As CAE.AfuData

theSession.AfuManager.GetAfuData(pathAFUFile, RecordID, myAfuData) 
theYvalues = myAfuData.GetRealData(theXvalues)

 

custom function to copy an array

 I read/was told that one cannot equate array. I thought that may have been the source of the crash

 

While still playing with this issue I have found that the array FxData() may actually be overwritten as the programm does 3 loops. Will post something on that a bit later

 

Thanks

 

Regards

 

JXB

 

Production: NX9.0.3.4, NX10.0.2.6
Development: VB.NET (amateur level !)

Re: Passing an array to a funcitn keeps crashing - Argument 'Array' is Nothing!

Phenom
Phenom

Do you need to copy the array at all? Your GetMaxMin function appears to be only querying the values, not changing them...

 

Oh, and if you do need to copy the array, there is a .NET function for that:

http://msdn.microsoft.com/en-us/library/k4yx47a1%28v=vs.110%29.aspx

Re: Passing an array to a funcitn keeps crashing - Argument 'Array' is Nothing!

Siemens Phenom Siemens Phenom
Siemens Phenom

 

" I read/was told that one cannot equate array."

 

Actually you can.  This simple code:

 

        Dim firstArray(9) As Double

        For i = 0 To 9
            firstArray(i) = i * 1.1
        Next

        Dim copiedArray(firstArray.GetUpperBound(0)) As Double

        copiedArray = firstArray

        For i = 0 To 9
            Echo(copiedArray(i).ToString())
        Next

 

Provides this output:

 

0
1.1
2.2
3.3
4.4
5.5
6.6
7.7
8.8
9.9

 ================== HOWEVER...

 

If you do it that way, and then modify any of the elements in the first array, they are also modified in the copied array.  To make a completely separate copy that will not have this limitation, use

 

copiedArray = firstArray.Clone()

 

instead.  Then anything you do to the original is not reflected in the copy

Re: Passing an array to a funcitn keeps crashing - Argument 'Array' is Nothing!

Phenom
Phenom

thanks for that. Will look into into.

I think I was confusing myself and found that the followign may work

 

RecordID = LastRecordID+1
For i = 1 to 3
 theLW.WriteLine("--Record ID is: " & RecordID.ToString)
 theSession.AfuManager.GetAfuData(pathAFUFile, RecordID, myAfuData)
 If i = 1 then FxData = myAfuData.GetRealData(TData)
 If i = 2 Then FyData = myAfuData.GetRealData(TData)
 If i = 3 Then FzData = myAfuData.GetRealData(TData)
 RecordID = RecordID+2
Next 

 

Production: NX9.0.3.4, NX10.0.2.6
Development: VB.NET (amateur level !)

Re: Passing an array to a funcitn keeps crashing - Argument 'Array' is Nothing!

Siemens Phenom Siemens Phenom
Siemens Phenom

In .NET, an array is a so-called "reference type". This means it's really just a pointer (a memory location).

 

So, suppose you write:

 

Dim copiedArray As Double() = originalArray

The originalArray thing is really the location in memory of the start of a sequence of doubles. The statement above sets copiedArray equal to this same location. So, now you have two variables pointing to this same location. Then, as Steve said, changing an element of originalArray will also change the corresponding element of copiedArray. This happens because these two elements are one and the same thing -- they are in the same location in memory. You haven't really copied the array at all, you have just invented a duplicate name for its location in memory.

 

If you really want a new and independent copy, use the Clone function.

 

Using Redim is not the recommended practice, these days. If you don't know in advance how many elements you'll be dealing with, it's usually better to use a List, instead of an array. You can keep adding elements to the List, and it will expand as needed. If you really want an array, you can get one from the List by using the ToArray function.

 

山田
yamada

Re: Passing an array to a funcitn keeps crashing - Argument 'Array' is Nothing!

Phenom
Phenom
Thanks Yamada for the input. Much appreciated. The few lines of code I posted seems to do the job. Going back to the original post, the array FxData() was indeed nothing by the time I reached the 3rd loop
Production: NX9.0.3.4, NX10.0.2.6
Development: VB.NET (amateur level !)