none
Object refernce is not set to an instance of the object error: Not constitant RRS feed

  • Question

  • Hello All:

    I am running below thread for every 10 seconds. For every alternative run, I'm getting the error  message "Object reference is not set to an instance of the object". It does not give error all the time. It runs fine one time, next time it throws the exception:

    Please take a look at the code below and let me know what is the problem:

    word[0] = 0;

               

    intCh1Cnt = 0; //May need later


               

    intCh2Cnt = 0; // May need later


               

    intCh3Cnt = 0; //may need later


               

    intCh4Cnt = 0; //may need later.


               

    intcount = 0;

               

    boolCh1Read, Ch2Read, Ch3Read, Ch4Read;

               

    //Buffer to hold response data


               

    byte[] data = newbyte[0];

               

    //Buffer to write data to modbus


               

    byte[] wrData = newbyte[2];

               

    // MessageBox.Show("Starting Thread");


                Master MBmaster;

               

    try


                {

                   

    //Create a new instance of modbus and connect


                   

    //TODO: Action on connection fail:


                   

    //TODO: Sanity checking and actions


                    MBmaster =

    newMaster(IPAddr.Text, 502);

                   

    try


                    {

                        data =

    newbyte[0];

                        Ch1Read =

    true;

                        MBmaster.ReadHoldingRegister(3, 2, 11, 2,

    refdata);

                       

    if(data.Length !=0)

                        {

                            Ch1Cnt = SetText(data, 1);

                        }

                    }

    //End Try Read1 function


                   

    catch(SystemException error)

                    {

                        MessageBox.Show(

    "Ch1 Read Error"+ error.Message);

                    }

    //End Catch for Ch1 Read Data


                   

    //Read Channel2 Data


                   

    try


                    {

                        data =

    newbyte[0];

                        Ch2Read =

    true;

                        MBmaster.ReadHoldingRegister(3, 2, 13, 2,

    refdata);

                       

    if(data.Length != 0)

                        {

                            Ch2Cnt = SetText(data, 2);

                        }

                    }

    //End try Read2 function


                   

    catch(SystemException error)

                    {

                        MessageBox.Show(

    "Ch2 Read Error"+ error.Message);

                    }

    //End Catch for Ch2 Read Data


                   

    //Read Channel3 Data


                   

    try


                    {

                        data =

    newbyte[0];

                        Ch3Read =

    true;

                        MBmaster.ReadHoldingRegister(3, 2, 30, 2,

    refdata);

                       

    if(data.Length != 0)

                        {

                            Ch3Cnt = SetText(data, 3);

                        }

                    }

    //End Try Read3 function


                   

    catch(SystemException error)

                    {

                        MessageBox.Show(

    "Ch3 Read Error"+ error.Message);

                    }

    //End Catch for Ch3 Read Data


                   

    //Read Channel4 data


                   

    try


                    {

                        data =

    newbyte[0];

                        Ch4Read =

    true;

                        MBmaster.ReadHoldingRegister(3, 2, 32, 2,

    refdata);

                       

    if(data.Length != 0)

                        {

                            Ch4Cnt = SetText(data, 4);

                        }

                    }

    //End Try Read4 function


                   

    catch(SystemException error)

                    {

                        MessageBox.Show(

    "Ch4 Read Error"+ error.Message);

                    }

    //


                    

    //Write to Modbus if Start button is clicked


                    

    //If Start Button is clicked


                    

    //Send start command to mon=bus


                    button_start.Click += (s, e) => {

                        wrData[0] = 1;

                        wrData[1] = 1;

                        MBmaster.WriteSingleRegister(6, 2, 101, wrData);

                    };

                   

    //If Stop button is clcked


                   

    //send stop command to modbus


                    button_stop.Click += (s, e) => {

                        wrData[0] = 0;

                        wrData[1] = 0;

                        MBmaster.WriteSingleRegister(6, 2, 101, wrData);

                    };

                   

    //TODO: Disconnect???


                }

    //End Try connect


               

    catch(SystemException error)

                {

                   

    //TODO: Process Error messasge


                    MessageBox.Show(

    "Connection Error"+error.Message);

                }  

               

               

    //Write to Database


               

    //TBD: TYpe checking


                WriteToDatabase(Ch1Cnt, Ch2Cnt, Ch3Cnt, Ch4Cnt);

            }

    //End of Thread

    Thursday, April 5, 2018 6:57 PM

All replies

  • Hello,

     First of all, when you insert code to a post please use Insert Code Block during

    the post edit.  This will help greatly with readability and not give a mess like your

    post is in.

     

     Second, the error message you have will tell you exactly what object has the

    problem. Normally, "object not set to instance" error means you did not use the

    "new" in the object declaration before actually trying to use that object.

     

     Finally, your code has "word[0] = 0;" statement without the "new" keyword.

    If object "word" is not an array then omit "[0]" asit is pointless. If object "word"

    is an array then rewrite the statement to include the "new" keyword.

     

     Hope this helps :)

    Thursday, April 5, 2018 7:18 PM
  • Thank you, for suggesting how to insert the code. I am new to the forum and I was not aware of that feature. But, I cannot see "insert code block" option?? I know my post was messy :(. Thank you for pointing out the word[0]=0. But the issue is not with that. I deleted it and still getting the error. I am tracing my code.It's the problem with "data" variable. But' it's assigned to object using new. The code executes without any error at one run and next run it's raising the error.
    Thursday, April 5, 2018 7:25 PM
  • Here is the code block, second button on the right.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, April 5, 2018 7:59 PM
    Moderator
  • Hello,

     Without knowing the class Master structure will limit anyone to help you.

    However, I have a few questions about the below code statement;

    byte[] data = new byte[0]

     Why are there 5 statements? According to scope, you only need to have

    a single code statement because the others inside each Try block are

    useless.

     

     I don't know about anyone else but your project is missing vital details

    that limits my ability to assist you in a solution.

     

     Sorry :/

    Thursday, April 5, 2018 8:13 PM