about Visio add-on problem by C# coding

E

Eunice

Hi, I am Eunice and just a begginer.
I coded some basic by C# and tried to add-on in Visio 2003. My purpose on
the program is that I try to get some shape properties such as text, ID,
from-shape ID and to-shape ID and then write the information to a text format
file. I added-on the file under the tool menu in Visio 2003. During compling
the code in visual studio. Net 2003, I don't have error. But whenever I click
the menu added-on under tool menu in visio 2003, I got the following error.

***************
An unhandled exception of type 'System.NullReferenceException' occurred
Additional information: Object reference not set to an instance of an object.
***********************

Could you help me out to solve this error? I will attach my simple code as
well.
The code has my all trials. So sorry to look mess up.
Thanks a lot. Have a nice day.

Eunice.

************** <VisioExEAddon.cs>*************

using System;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Collections;
using System.Resources;
using System.ComponentModel;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Visio;

namespace MatchMaker1 {

public class VisioEXEAddon {

public static Microsoft.Office.Interop.Visio.Application visioApplication;
static void Main(string[] args)
{

string message = "";
//int argIndex = 0;
AddonDialog addonDialog = null;
ResourceManager resourceMgr = null;
//ArrayList shapeValueArray = new ArrayList();

//Shape textinfo;
//Microsoft.Office.Interop.Visio.Application visioApplication;

resourceMgr = new
ResourceManager"MatchMaker1.ProjectStrings",
typeof(VisioEXEAddon).Assembly);

// Create a string to display argument details.
//message += resourceMgr.GetString("strArgumentCount") +
args.Length.ToString() + "\r\n\r\n";
//message += resourceMgr.GetString("strArgumentDetails") +
"\r\n";

//for (argIndex = 0; argIndex < args.Length; ++argIndex) {
// message += args[argIndex] + "\r\n";
//}

//shape1.Add(visioApplication.ActivePage.Shapes[1].ID.ToString());
//shape1.Add(visioApplication.ActivePage.Shapes[1].NameU);
//shape1.Add(visioApplication.ActivePage.Shapes[1].Text);
//shape1.Add(visioApplication.ActivePage.Shapes[1].Type.ToString());
//shape1.Add(visioApplication.ActivePage.Shapes[1].Master.ToString());


try
{

//textinfo =

StoreShapeInfo.GetShapeItem(visioApplication.ActivePage.Shapes, 1);
//message += "ShapeID : "+ textinfo.ID + "\r\n\r\n";
//message += "Text : "+ textinfo.Text + "\r\n\r\n";
//message += "NameU : "+ textinfo.NameU + "\r\n\r\n";
//message += "Type : "+ textinfo.Type + "\r\n\r\n";
//message += "Master : "+ textinfo.Master + "\r\n\r\n";

//message += "ShapeID :" + visioApplication.ActivePage.Shapes[1].ID +

"\r\n\r\n";
//message += "NameU :" + visioApplication.ActivePage.Shapes[1].NameU +

"\r\n\r\n";
//message += "Text :" + visioApplication.ActivePage.Shapes[1].Text +

"\r\n\r\n";
//message += "Master :" + visioApplication.ActivePage.Shapes[1].Master +

"\r\n\r\n";
//message += "Type :" + visioApplication.ActivePage.Shapes[1].Type +

"\r\n\r\n";



//for (int s = 0; s< shape1.Count ; s++)
//{
//string type= "v ";
//string blank= " ";
//pub.Text += type + NetronLight.Store.objectIDs + blank +

NetronLight.Store.texts+"\n";
//string vertex = type + NetronLight.Store.objectIDs + blank +

NetronLight.Store.texts;
//message += "Shape Values : " + shape1 + "\r\n\r\n";

//sw.WriteLine(vertex);

//}

System.Array shapeValueArray = Array.CreateInstance( typeof(string), 12);



shapeValueArray.SetValue(visioApplication.ActivePage.Shapes[1].ID.ToString(), 1);


shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionObject.ToString(), 2);


shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisRowIndices.visRowXFormOut.ToString(), 3);


shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisCellIndices.visXFormWidth.ToString(), 4);



shapeValueArray.SetValue(visioApplication.ActivePage.Shapes[2].ID.ToString(), 5);


shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionObject.ToString(), 6);


shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisRowIndices.visRowXFormOut.ToString(), 7);


shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisCellIndices.visXFormWidth.ToString(), 8);




shapeValueArray.SetValue(visioApplication.ActivePage.Shapes[3].ID.ToString(), 9);


shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionObject.ToString(),

10);


shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisRowIndices.visRowXFormOut.ToString(), 11);


shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisCellIndices.visXFormWidth.ToString(), 12);



//Return the formulas of the cells.
//Dim avarFormulaArray() As Variant
//ArrayList formulaArray = new ArrayList();


System.Array formulaArray = Array.CreateInstance( typeof(string), 12);
//System.Array formulaArray;

visioApplication.ActivePage.GetFormulasU(ref shapeValueArray, out

formulaArray);

//Debug.Print "Shape 1 width is "; avarFormulaArray(0)
//Debug.Print "Shape 2 height is "; avarFormulaArray(1)
//Debug.Print "Shape 3 angle is "; avarFormulaArray(2)

for (short index = 0; index < 12; index++)
{

//message += "Shape information :" + formulaArray.GetValue(index)

+ "\r\n\r\n";
message += "Shape information :" + formulaArray.GetValue(index) +

"\r\n\r\n";

}

// Display the argument details in a dialog box.
addonDialog = new AddonDialog();
addonDialog.Args.Text = message;
addonDialog.ShowDialog();

}
catch (Exception err)
{

StoreShapeInfo.DisplayException(visioApplication.AlertResponse,

err.Message);

}

}
}
}
 
S

shashi behari

nullreference would imply that somewhere there is an object in your code
which isn't getting instantiated properly. i reccomend you try to "localise"
the exception by using more than one try-catch block, use a seperate try
block for every operation where an error may occur (eg. when you reference a
shape which may not exist).

also using exception.ToString() gives a more detailed error string than
exception.Message and will usually give the exact line in your source code
which caused the problem thus helping you pinpoint the error.

hope this helps you find the erroneous portion of code.

Eunice said:
Hi, I am Eunice and just a begginer.
I coded some basic by C# and tried to add-on in Visio 2003. My purpose on
the program is that I try to get some shape properties such as text, ID,
from-shape ID and to-shape ID and then write the information to a text format
file. I added-on the file under the tool menu in Visio 2003. During compling
the code in visual studio. Net 2003, I don't have error. But whenever I click
the menu added-on under tool menu in visio 2003, I got the following error.

***************
An unhandled exception of type 'System.NullReferenceException' occurred
Additional information: Object reference not set to an instance of an object.
***********************

Could you help me out to solve this error? I will attach my simple code as
well.
The code has my all trials. So sorry to look mess up.
Thanks a lot. Have a nice day.

Eunice.

************** <VisioExEAddon.cs>*************

using System;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Collections;
using System.Resources;
using System.ComponentModel;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Visio;

namespace MatchMaker1 {

public class VisioEXEAddon {

public static Microsoft.Office.Interop.Visio.Application visioApplication;
static void Main(string[] args)
{

string message = "";
//int argIndex = 0;
AddonDialog addonDialog = null;
ResourceManager resourceMgr = null;
//ArrayList shapeValueArray = new ArrayList();

//Shape textinfo;
//Microsoft.Office.Interop.Visio.Application visioApplication;

resourceMgr = new
ResourceManager"MatchMaker1.ProjectStrings",
typeof(VisioEXEAddon).Assembly);

// Create a string to display argument details.
//message += resourceMgr.GetString("strArgumentCount") +
args.Length.ToString() + "\r\n\r\n";
//message += resourceMgr.GetString("strArgumentDetails") +
"\r\n";

//for (argIndex = 0; argIndex < args.Length; ++argIndex) {
// message += args[argIndex] + "\r\n";
//}

//shape1.Add(visioApplication.ActivePage.Shapes[1].ID.ToString());
//shape1.Add(visioApplication.ActivePage.Shapes[1].NameU);
//shape1.Add(visioApplication.ActivePage.Shapes[1].Text);
//shape1.Add(visioApplication.ActivePage.Shapes[1].Type.ToString());
//shape1.Add(visioApplication.ActivePage.Shapes[1].Master.ToString());


try
{

//textinfo =

StoreShapeInfo.GetShapeItem(visioApplication.ActivePage.Shapes, 1);
//message += "ShapeID : "+ textinfo.ID + "\r\n\r\n";
//message += "Text : "+ textinfo.Text + "\r\n\r\n";
//message += "NameU : "+ textinfo.NameU + "\r\n\r\n";
//message += "Type : "+ textinfo.Type + "\r\n\r\n";
//message += "Master : "+ textinfo.Master + "\r\n\r\n";

//message += "ShapeID :" + visioApplication.ActivePage.Shapes[1].ID +

"\r\n\r\n";
//message += "NameU :" + visioApplication.ActivePage.Shapes[1].NameU +

"\r\n\r\n";
//message += "Text :" + visioApplication.ActivePage.Shapes[1].Text +

"\r\n\r\n";
//message += "Master :" + visioApplication.ActivePage.Shapes[1].Master +

"\r\n\r\n";
//message += "Type :" + visioApplication.ActivePage.Shapes[1].Type +

"\r\n\r\n";



//for (int s = 0; s< shape1.Count ; s++)
//{
//string type= "v ";
//string blank= " ";
//pub.Text += type + NetronLight.Store.objectIDs + blank +

NetronLight.Store.texts+"\n";
//string vertex = type + NetronLight.Store.objectIDs + blank +

NetronLight.Store.texts;
//message += "Shape Values : " + shape1 + "\r\n\r\n";

//sw.WriteLine(vertex);

//}

System.Array shapeValueArray = Array.CreateInstance( typeof(string), 12);



shapeValueArray.SetValue(visioApplication.ActivePage.Shapes[1].ID.ToString(), 1);


shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionObject.ToString(), 2);


shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisRowIndices.visRowXFormOut.ToString(), 3);


shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisCellIndices.visXFormWidth.ToString(), 4);



shapeValueArray.SetValue(visioApplication.ActivePage.Shapes[2].ID.ToString(), 5);


shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionObject.ToString(), 6);


shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisRowIndices.visRowXFormOut.ToString(), 7);


shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisCellIndices.visXFormWidth.ToString(), 8);




shapeValueArray.SetValue(visioApplication.ActivePage.Shapes[3].ID.ToString(), 9);


shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionObject.ToString(),

10);


shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisRowIndices.visRowXFormOut.ToString(), 11);


shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisCellIndices.visXFormWidth.ToString(), 12);



//Return the formulas of the cells.
//Dim avarFormulaArray() As Variant
//ArrayList formulaArray = new ArrayList();


System.Array formulaArray = Array.CreateInstance( typeof(string), 12);
//System.Array formulaArray;

visioApplication.ActivePage.GetFormulasU(ref shapeValueArray, out

formulaArray);

//Debug.Print "Shape 1 width is "; avarFormulaArray(0)
//Debug.Print "Shape 2 height is "; avarFormulaArray(1)
//Debug.Print "Shape 3 angle is "; avarFormulaArray(2)

for (short index = 0; index < 12; index++)
{

//message += "Shape information :" + formulaArray.GetValue(index)

+ "\r\n\r\n";
message += "Shape information :" + formulaArray.GetValue(index) +

"\r\n\r\n";

}

// Display the argument details in a dialog box.
addonDialog = new AddonDialog();
addonDialog.Args.Text = message;
addonDialog.ShowDialog();

}
catch (Exception err)
{

StoreShapeInfo.DisplayException(visioApplication.AlertResponse,

err.Message);

}

}
}
}
 
E

Eunice

Thanks a lot, Shashi! It helped me a lot.
nullreference would imply that somewhere there is an object in your code
which isn't getting instantiated properly.

Because I am a just beginner, I don't understand in this point. Sorry about
that.
After following your recommendation, I got this more detail error.

********
System.NullReferenceException: Object reference not set to an instance of an
object. at MatchMaker1.VisioExEAddon.Main() in C:\~\visioexeaddon.cs: line 99
***********************
Here is line 99.
line 99
--->shapeValueArray.SetValue(visioApplication.ActivePage.Shapes[1].ID.ToString(), 1);

Shashi, could you give me some proper example codes in my case?

Without indexing shape ID(e.g.
visioApplication.ActivePage.Shapes[1].ID.ToString()),
is there any way to get shape information lists such as text and shape ID in
curretly opened visio diagram?

Here is my code again.

Thank you so much. Have a nice day.

Eunice.


************** <VisioExEAddon.cs>*************
public class VisioEXEAddon {

public static Microsoft.Office.Interop.Visio.Application visioApplication;

static void Main()
{
string message = "";
AddonDialog addonDialog = null;
ResourceManager resourceMgr = null;

try
{
System.Array shapeValueArray = Array.CreateInstance( typeof(string), 12);
shapeValueArray.SetValue(visioApplication.ActivePage.Shapes[1].ID.ToString(), 1); *****Line 99*****
shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionObject.ToString(), 2);
shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisRowIndices.visRowXFormOut.ToString(), 3);
shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisCellIndices.visXFormWidth.ToString(), 4);

//omitted repeated code above

System.Array formulaArray = Array.CreateInstance( typeof(string), 12);
visioApplication.ActivePage.GetFormulasU(ref shapeValueArray, out
formulaArray);

for (short index = 0; index < 12; index++)
{
message += "Shape information :" + formulaArray.GetValue(index) +
"\r\n\r\n";

}

addonDialog = new AddonDialog();
addonDialog.Args.Text = message;
addonDialog.ShowDialog();

}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
}
}
 
S

shashi behari

a simple example about the null reference thing:
when you create an object like this:

MyClass myObj;

you create a memory space for something of type MyClass which you can
reference with "myObj". this just allocates memory space though, it doesn't
create values to go into that space. if you tried to access "myObj" now it
would give you a nullreference
eg (assuming there is a GetData method which returns an int):
int i = myObj.GetData();
would return a nullreference exception.

the way you avoid this is by instantiating the object, or creating and
assigning data in a memory space:

myObj = new MyClass();

or (if anotherObj is of type MyClass)

myObj = anotherObj;

if you do not do this, myObj will equal null by default. which is why the
exception is called "null reference".

so in your case, there is an object reference in this line which has no
actual object data in the memory space that it references.

shapeValueArray.SetValue(visioApplication.ActivePage.Shapes[1].ID.ToString(), 1);

i think this might be your main problem here:
public static Microsoft.Office.Interop.Visio.Application visioApplication;

you've not instantiated the visio Application, either do this to start up a
new visio :
visioApplication = new Visio.ApplicationClass();

or this to get a reference to an existing one:

object visioObject = Marshal.GetActiveObject("Visio.Application");
this.visioApplication = visioObject as Visio.Application;

what you could do instead of using ....Shapes[1]... to get a specific shape
is to use a foreach loop as such :

foreach(Microsoft.Office.Interop.Visio.Shape shape in
visioApplication.ActivePage.Shapes)
{
if(shape.ID != null)
shapeValueArray.SetValue(shape.ID, shape.Index)
}

this is a sure way to get every shape on the page with as little chance for
error as possible.





Eunice said:
Thanks a lot, Shashi! It helped me a lot.
nullreference would imply that somewhere there is an object in your code
which isn't getting instantiated properly.

Because I am a just beginner, I don't understand in this point. Sorry about
that.
After following your recommendation, I got this more detail error.

********
System.NullReferenceException: Object reference not set to an instance of an
object. at MatchMaker1.VisioExEAddon.Main() in C:\~\visioexeaddon.cs: line 99
***********************
Here is line 99.
line 99
--->shapeValueArray.SetValue(visioApplication.ActivePage.Shapes[1].ID.ToString(), 1);

Shashi, could you give me some proper example codes in my case?

Without indexing shape ID(e.g.
visioApplication.ActivePage.Shapes[1].ID.ToString()),
is there any way to get shape information lists such as text and shape ID in
curretly opened visio diagram?

Here is my code again.

Thank you so much. Have a nice day.

Eunice.


************** <VisioExEAddon.cs>*************
public class VisioEXEAddon {

public static Microsoft.Office.Interop.Visio.Application visioApplication;

static void Main()
{
string message = "";
AddonDialog addonDialog = null;
ResourceManager resourceMgr = null;

try
{
System.Array shapeValueArray = Array.CreateInstance( typeof(string), 12);
shapeValueArray.SetValue(visioApplication.ActivePage.Shapes[1].ID.ToString(), 1); *****Line 99*****
shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionObject.ToString(), 2);
shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisRowIndices.visRowXFormOut.ToString(), 3);
shapeValueArray.SetValue(Microsoft.Office.Interop.Visio.VisCellIndices.visXFormWidth.ToString(), 4);

//omitted repeated code above

System.Array formulaArray = Array.CreateInstance( typeof(string), 12);
visioApplication.ActivePage.GetFormulasU(ref shapeValueArray, out
formulaArray);

for (short index = 0; index < 12; index++)
{
message += "Shape information :" + formulaArray.GetValue(index) +
"\r\n\r\n";

}

addonDialog = new AddonDialog();
addonDialog.Args.Text = message;
addonDialog.ShowDialog();

}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
}
}



shashi behari said:
nullreference would imply that somewhere there is an object in your code
which isn't getting instantiated properly. i reccomend you try to "localise"
the exception by using more than one try-catch block, use a seperate try
block for every operation where an error may occur (eg. when you reference a
shape which may not exist).

also using exception.ToString() gives a more detailed error string than
exception.Message and will usually give the exact line in your source code
which caused the problem thus helping you pinpoint the error.

hope this helps you find the erroneous portion of code.
 
E

Eunice

Shashi! Thanks a lot for your detail explanation.^^
It works very well. I solved the problem that had afflicted me from last week.
I so appreciate that.

Have a nice day.

Best regards,
Eunice.
 
Top