This project is read-only.

Showing Null Reference Exception

May 3, 2015 at 11:09 PM
Edited May 3, 2015 at 11:28 PM
I have using the following code in my project



its a graph ploting project
using System.Windows.Forms;
using Microsoft.Win32;
using RDotNet;
using RDotNet.Devices;
using RDotNet.Internals;
using RDotNet.Dynamic;
using System.IO;
using WindowsFormsApplication8;
using RDotNet.NativeLibrary;
using MySql.Data.MySqlClient;
using System.Diagnostics;


namespace WindowsFormApllication8
{
    public partial class Form2 : Form
    {   
        private readonly RGraphAppHook cbt;
        public REngine engine;
        public DataFrame dataset;
        
       public static void SetupPath(string Rversion = "R-3.1.3")
        {
            var oldPath = System.Environment.GetEnvironmentVariable("PATH");
            var rPath = System.Environment.Is64BitProcess ?
                                   string.Format(@"C:\Program Files\R\R-3.1.3\bin\i386", Rversion) :
                                   string.Format(@"C:\Program Files\R\R-3.1.3\bin\x64", Rversion);


            if (!Directory.Exists(rPath))
                throw new DirectoryNotFoundException(
                  string.Format(" R.dll not found in : {0}", rPath));
            var newPath = string.Format("{0}{1}{2}", rPath,
                                         System.IO.Path.PathSeparator, oldPath);
            System.Environment.SetEnvironmentVariable("PATH", newPath);
        }
        public Form2()
        {  
            InitializeComponent();
          
             SetupPath();
            REngine engine = REngine.CreateInstance("RDotNet");
            engine = REngine.GetInstanceFromID("RDotNet");
            //cbt = new RGraphAppHook { GraphControl = RPanel1 };
             pictureBox1.Visible = false;
            
              
             
        }
  private void portVizerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            engine = REngine.GetInstanceFromID("RDotNet");
            engine.EagerEvaluate("library(ggplot2)");
            engine.EagerEvaluate("library(playwith)");
            engine.EagerEvaluate("dataset<-read.csv(file.choose(), header=TRUE, sep = ',' )");
            engine.EagerEvaluate("data1<-table(dataset$Protocol)");
            engine.EagerEvaluate("data2<-as.data.frame(data1)");
            engine.EagerEvaluate("playwith(plot(data2))");

        }

        private void inSameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            cbt.Install();
            engine = REngine.GetInstanceFromID("RDotNet");
            engine.EagerEvaluate("library(ggplot2)");
            engine.EagerEvaluate("primary<-read.csv(file.choose())");
            engine.EagerEvaluate("attach(primary)");
            engine.EagerEvaluate("var<-c(\"Time\",\"Source\")");
            engine.EagerEvaluate("secondary<-primary[var]");
            engine.EagerEvaluate("code1<-secondary[!duplicated (secondary [,c(\"Source\")]),]");
            engine.EagerEvaluate("code<-as.integer(code1[,c(\"Time\")])");
            engine.EagerEvaluate("code2<-table(code)");
            engine.EagerEvaluate("code3<-as.data.frame(code2)");
            engine.EagerEvaluate("attach(code3)");
            engine.EagerEvaluate("plot(code3$Source,ch=1,col=5)");

            cbt.Uninstall();
        }

        private void iToolStripMenuItem_Click(object sender, EventArgs e)
        {
            engine = REngine.GetInstanceFromID("RDotNet");
            engine.EagerEvaluate("library(ggplot2)");
            engine.EagerEvaluate("primary<-read.csv(file.choose())");
            engine.EagerEvaluate("attach(primary)");
            engine.EagerEvaluate("var<-c(\"Time\",\"Source\")");
            engine.EagerEvaluate("secondary<-primary[var]");
            engine.EagerEvaluate("code1<-secondary[!duplicated (secondary [,c(\"Source\")]),]");
            engine.EagerEvaluate("code<-as.integer(code1[,c(\"Time\")])");
            engine.EagerEvaluate("code2<-table(code)");
            engine.EagerEvaluate("code3<-as.data.frame(code2)");
            engine.EagerEvaluate("attach(code3)");
            engine.EagerEvaluate("plot(code3)");
        }
  private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void rGraphicsWindowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            engine = REngine.GetInstanceFromID("RDotNet");
            engine.EagerEvaluate("library(ggplot2)");
            //engine.EagerEvaluate("library(Rgraphviz)");
            engine.EagerEvaluate("dataset<-read.table(file.choose(), header=TRUE, sep = ',')");
            engine.EagerEvaluate("matrix1<-dataset[!duplicated(dataset[,c(\"Source\",\"Destination\")]),]");
            engine.EagerEvaluate("attach(matrix1)");
            engine.EagerEvaluate("matrix1$val<- 1");
            engine.EagerEvaluate("adjMat <- reshape2::dcast(matrix1, Source ~ Destination, fun.aggregate= NULL,value.var = \"val\", fill=0)");
            engine.EagerEvaluate("adjMat<-adjMat[,-c(nrow(adjMat)+1:ncol(adjMat))]");
            engine.EagerEvaluate("adjmat<-as.matrix(adjMat)");
            engine.EagerEvaluate("am.graph<-new(\"graphAM\", adjMat=adjmat, edgemode=\"directed\");");
            engine.EagerEvaluate("plot(am.graph, attrs = list(node = list(fillcolor = \"2\"),edge = list(arrowsize=0.5)))");
        }

        private void gtk2WindowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            engine = REngine.GetInstanceFromID("RDotNet");
            engine.EagerEvaluate("library(Rgraphviz)");
            engine.EagerEvaluate("library(playwith)");
            engine.EagerEvaluate("dataset<-read.table(file.choose(), header=TRUE, sep = ',')");
            engine.EagerEvaluate("matrix1<-dataset[!duplicated(dataset[,c(\"Source\",\"Destination\")]),]");
            engine.EagerEvaluate("attach(matrix1)");
            engine.EagerEvaluate("matrix1$val<- 1");
            engine.EagerEvaluate("adjMat <- reshape2::dcast(matrix1, Source ~ Destination, fun.aggregate= NULL,value.var = \"val\", fill=0)");
            engine.EagerEvaluate("adjMat<-adjMat[,-c(nrow(adjMat)+1:ncol(adjMat))]");
            engine.EagerEvaluate("adjmat<-as.matrix(adjMat)");
            engine.EagerEvaluate("am.graph<-new(\"graphAM\", adjMat=adjmat, edgemode=\"directed\");");
            engine.EagerEvaluate("playwith(plot(am.graph, attrs = list(node = list(fillcolor = \"2\"),edge = list(arrowsize=0.5))))");

        }
 private void cmdToolStripMenuItem_Click(object sender, EventArgs e)
        {

            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = "/C tshark -i 2 -p -a filesize=100 -w \"C:\\Users\\nilkhil rajendran\\OneDrive\\WindowsFormsApplication8\\WindowsFormsApplication8\\bin\\Debug\\test\\in\\a.pcap" ;
            startInfo.Arguments = "/C sigma.exe -dbh localhost -dbmy -in \"./test/in \"  -out \"./test/out\" -dbu  root -dbp root  -ts \"C:\\Program Files\\Wireshark\\tshark.exe\" -pre Basic";
            process.StartInfo = startInfo;
            process.Start();
           }
 private void logTableToolStripMenuItem_Click(object sender, EventArgs e)
        {
            engine = REngine.GetInstanceFromID("RDotNet");
            engine.EagerEvaluate("library(RMySQL)");
            engine.EagerEvaluate("mydb = dbConnect(MySQL(), user='root',password='root', dbname='sigma', host='127.0.0.1')");
            engine.EagerEvaluate("rs=dbSendQuery(mydb, 'SELECT * FROM ip')");
            engine.EagerEvaluate("data=fetch(rs)");
            engine.EagerEvaluate("dataset1=as.data.frame(data)");
            dataset = engine.EagerEvaluate("dataset1").AsDataFrame();
            Form3 sform = new Form3(dataset);
            sform.Show();

        }
 private void simpleGraphToolStripMenuItem_Click(object sender, EventArgs e)
        {
            engine = REngine.GetInstanceFromID("RDotNet");
            engine.EagerEvaluate("source(\"Rgraphviz.R\")");__**Second time null reference error pointed here**__
            
           
        }

        private void simpleGraphclientToolStripMenuItem_Click(object sender, EventArgs e)
        {
            engine = REngine.GetInstanceFromID("RDotNet");
            engine.EagerEvaluate("source(\"RgraphvizClient.R\")"); // __**first time null reference exception error pointed here**__ 
        }
only one of this code will work properly and after that the other code shows null reference exception
ie if simpleGraphclient plots a graph then simplegraph shows null reference exception and vice versa