This is my first post so I'm not sure if this should be in the .NET forum or the VB forum. Anyway I'm using VB .NET and trying to call the Percentile function. The function needs an Object as a parameter, but I'm trying to pass it an Array. Here's some of my code:
Imports Microsoft.Office.Interop.Excel
Dim worksheetFunction1 As WorksheetFunction
percentileValue = worksheetFunction1.Percentile(intenArray, inputPerValue)
I get the error System.NullReferenceException when trying to execute the Percentile function call.
Is this due to the array not being an Object data type? Is there a way to convert or cast the array (it's an array of Doubles) to an Excel Object data type or something the Percentile function will be ok with?
I'm pretty new to VB and programming in general. Any help will be appreciated.
7 2714
Hi,
You can send the array as an Object.So ,I think that must not be giving error.Are you using dynamic array? If so , it may generate Runtime exception
if not initialised.
How you pass the array to that method ?
The usual method is,
string[] weekDays = new string[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
PrintArray(weekDays);
Print array is a user defined method.
How would I pass the array as an Object? Could you give me an example.
I'm pretty sure the array is initialized properly.
I'll post more of my code so you can see what's going on. I'm reading in a bitmap image, getting the brightness values of each pixel, and storing the values in an array of doubles. I then want to find a certain percentile value of the array. Rather than write my own percentile function I'm trying to use the Excel Worksheet Function. Then I make all pixels <= the percentile brightness black else all other pixels are made white. -
-
Public Function posterizeImage(ByVal gImage As Bitmap) As Image
-
-
Dim pImage As Bitmap = gImage
-
'prep soon to be posterized image
-
-
Dim wid As Integer = gImage.Width
-
Dim hgt As Integer = gImage.Height
-
Dim totalPixels As Integer = Math.BigMul(wid, hgt) 'could be a very large number
-
Dim tempcolor As Color
-
Dim intenArray(totalPixels - 1) As Double
-
Dim counter As Integer = 0
-
-
'new variables for Excel percentile function
-
Dim inputPerValue As Double
-
Dim percentileValue As Double
-
Dim worksheetFunction1 As WorksheetFunction
-
-
'fill array intenArray with brightness (intesity) values
-
'brightness values range from 0 (black) to 1 (white)
-
For x As Integer = 0 To wid - 1
-
For y As Integer = 0 To hgt - 1
-
tempcolor = gImage.GetPixel(x, y)
-
intenArray(counter) = tempcolor.GetBrightness
-
counter += 1
-
Next
-
Next
-
-
'sort the array - I don't know if this is necessary for the Excel function
-
intenArray.Sort(intenArray)
-
-
'find percentile value - uses Excel percentile function
-
inputPerValue = txtPerValue.Text / 100.00
-
-
'Excel percentile function
-
percentileValue = worksheetFunction1.Percentile(intenArray, inputPerValue)
-
-
For x As Integer = 0 To wid - 1
-
For y As Integer = 0 To hgt - 1
-
tempcolor = gImage.GetPixel(x, y)
-
If tempcolor.GetBrightness <= percentileValue Then
-
pImage.SetPixel(x, y, Black)
-
Else
-
pImage.SetPixel(x, y, White)
-
End If
-
Next
-
Next
-
-
Return pImage
-
End Function
-
-
Either intenArray or inputPerValue are null, write values to discover which and work backwards debugging from there.
Neither parameter is null. None of the elements in the array are null.
Here is the link to the Percentile method: http://msdn2.microsoft.com/en-us/lib...le(VS.80).aspx
It asks for an Object and I'm giving it an array of doubles. I believe this is the problem. So what can I pass to the method so it will work?
Is there a way to make an array of Doubles as an array of Objects?
Hi,
This is the way you can do it,
Dim intenarry() as Double
Redim intenarray(totalpixels-1)
WorksheetFunction1.Percentile(intenarray)
Percentile(Byval o1 as Object)
'Access object as an array
End Sub
ReDim didn't work as a workaround. I did figure it out finally though. Here is the different syntax that worked.
Found it at: http://p2p.wrox.com/topic.asp?TOPIC_ID=41465
Dim xl As Object
xl = CreateObject("Excel.Application")
'rather than
'Dim worksheetFunction1 As WorksheetFunction
then use
percentileValue = xl.Percentile(intenArray, inputPerValue)
which works perfectly!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Kumar |
last post by:
Hi Folks,
I have a question regarding my windows c# application.
This application just reads MS Excel file and puts the data in to sql server
database.
In that excel file ,it has one named cell...
|
by: msnnews.msn.com |
last post by:
hi there,
i've got a form that populates a datagrid, and a button that calls a
function to export to an excel file.
All is well with the export, no errors are returned, but the Excel instance...
|
by: Tim Frawley |
last post by:
I created a .NET Com Class object for use in ASP reports to export
database results directly to Excel. I have it all working just find
but I cannot get the Excel process to go away after the job...
|
by: MIKQ |
last post by:
Hi,
ich habe ein eindimesionales Array dessen ca. 5 Werte (01234) ich in
_eine_ Zelle schreiben will.
Um in Excel zu schreiben habe ich folgende Sub geschrieben:
Private Sub...
|
by: Peter |
last post by:
Is there a fast way to move data from DataTable into double array, or do I have to spin through every record and column?
I have five tables and I need to merge these tables column wise, each table...
|
by: blainegray |
last post by:
Greetings
This is one of those Access is not closing Excel problems.
The first time through the code works fine. The second time there is a problem.
After lots of combinations, I finally...
|
by: =?Utf-8?B?Y29uc3RhbnRpbg==?= |
last post by:
How to build an array to make it easy to read the data from Excel worksheet
table then load the data into oracle.The table looks like this have 4
columns:1-entity name with two...
|
by: =?Utf-8?B?Y29uc3RhbnRpbg==?= |
last post by:
I know my question seems easy,how to build an array to read the data from
Excel worksheet table then load the data into oracle.The table(employee)
looks like this have 4 columns:1-entity name with...
|
by: =?Utf-8?B?c3Bhcmtlcg==?= |
last post by:
Has anybody had the occasion to to build a VB.NET 2008 application that will
manipulate an Excel workbook including the Hyperion Essbase Plugin? I was not
sure which forum to post this question in...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |