none
Null Reference on Object table reference RRS feed

  • Question

  • Morning

    Hope all are well.

    As all can probably see I'm a bit confused on where to initialize the object, we've create a class that hold the values so it can be shared with front end and back end applications. I think I did initialize it in the beginning of the class that will populate the records. we got multiple Datatable that needs to be pulled into memory and want to group it in a DataSet but below error got a null.reference on the datatable, but to what I can see it's initialize at the top of the class.. know I'm missing something.

        ///Call from a Form   

    private void btnNewHuNr_Click(object sender, EventArgs e) { NewHuNrSymbols newHuSymbols; var testNuHu = new NewHuNrTriggeredHandlers(); newHuSymbols = testNuHu.HuNrSequenceController(); while (newHuSymbols.sequencer != 0) { newHuSymbols = testNuHu.HuNrSequenceController(); } }

      public class NewHuNrSymbols
        {
            public int sequencer;
            public DataSet tableRecords;
    
            public NewHuNrSymbols()
            {
                sequencer = 1;
                var tableRecords = new DataSet();
            }
        }
    namespace WCS_Shared.SubHandlers.Logic
    {
        /// <summary>
        /// This will handle all sub triggers and decisions based around a new HUNr in the tb_Instruction_WCS_PLC Sql database
        /// </summary>
        public class NewHuNrTriggeredHandlers
        {
    
            string HuNr = ""; //Stores HuNr found in case 1
            DataTable MatchingHuNr;  //Stores the Records matching the HuNr in the Wave Table
            NewHuNrSymbols huSymbols = new NewHuNrSymbols();
            /// Check if there is a new HU Nr
            /// if there is a New Hu Nr case 1
            ///  case 1 = Check if there is new HuNr in tb_Instruction_PLC_WCS table
            ///  case 2 = If there is new records Copy all HuNr Matching Numbers into Temp Datatable
            ///  case 3 = Create a DataTable with loop to select all order Nr that match the above HuNr. (Get all order Numbers)
            ///  case 3 = Copy all records matching ORder number with data to ProdPigeonStatus
            ///  case 4 = Check which Order number require a pigeon hole
            ///  case 5 = Check for available pigeon Locations
            ///  case 6 = Allocated each of the ORder numbers a Pigeon locations
            ///  case 7 = Set Status for specific HuNr that require picking to think Status 2....
    
            public NewHuNrSymbols HuNrSequenceController()
            {
    
                Console.WriteLine("Sequenceer currently processing " + huSymbols.sequencer);                 
    
                    switch (huSymbols.sequencer)
                    {
                        case 1: //Check if there is a new HuNr
    
                            HuNr = MonitorForNewHuNrIn_tbInstructionTable();
    
                            if (HuNr.Length > 0)
                            {
                            huSymbols.sequencer = 2; //New Hu Nr Recorded
                                 
                            }                        
                            else
                            {
                            huSymbols.sequencer = 0; // No New Hu Nr found in table
                                 
                            }
                            return huSymbols;
                            break;
    
                        case 2:  //Copy all HuNr to new Table
    
                            MatchingHuNr = SelectAllRecordsFrom_tb_WaveDataByHuNr(HuNr);                        
                            huSymbols.tableRecords.Tables.Add(MatchingHuNr);
    
                            if (MatchingHuNr.Rows.Count > 0)
                            {
                            huSymbols.sequencer = 3; //There is records in the Wave Table that matches the HuNr
                                 
                            }
                            else
                            {
                            huSymbols.sequencer = 0; //There is no records in Wave Table matching the HuNr in WaveData                             
                            }
                            return huSymbols;
                            break;                            
                }
                return huSymbols;
            }        
            /// <summary>
            /// Monitor the tb_Instruction_WCS_PLC table if new HUnr has been populated.
            /// </summary>
            /// <returns></returns>
            private string MonitorForNewHuNrIn_tbInstructionTable()
            {
                var newPlcInstruction = new tb_Instruction_WCS_PLCManager();
                string newRecord = "";
                string message = "";
    
                newRecord = newPlcInstruction.ReadInstructionTableHuCode();
    
                if (HuNr.Length > 0) //New Record has been inserted by PLC with Status 0...
                {
                    message = ("New HuNr: " + HuNr + " recorded in tb_Instruction_WCS_PLC table processing now");
                    LogFiles.EventLogHandler.WriteEvent("3 ; " + this + " ; " + message);
                    Console.WriteLine(message);
                }
                else
                {
                    message = ("No new HuNr Nr Recorded in tb_Instruction_WCS_PLC table");
                    LogFiles.EventLogHandler.WriteEvent("3 ; " + this + " ; " + message);
                    Console.WriteLine(message);
                }
                return newRecord;
            }
            /// <summary>
            /// Select All records matching the HuNr
            /// </summary>
            /// <param name="huNr"></param>
            /// <returns></returns>
            private DataTable SelectAllRecordsFrom_tb_WaveDataByHuNr(string huNr)
            {
                DataTable waveData;
                string message = "";
                var waveDb = new tb_WaveDataManager();
    
                waveData = waveDb.GetDbWaveDataByHUnr(huNr);
    
                if (waveData.Rows.Count > 0) //New Record has been inserted by PLC with Status 0...
                {
                    message = ("Total of " + waveData.Rows.Count + " found in tb_WaveData");
                    LogFiles.EventLogHandler.WriteEvent("3 ; " + this + " ; " + message);
                    Console.WriteLine(message);
                }
                else
                {
                    message = ("No Record matching " + HuNr + " founded, check Wave data");
                    LogFiles.EventLogHandler.WriteEvent("3 ; " + this + " ; " + message);
                    Console.WriteLine(message);
                }
                return waveData;
            }
            
        }
    }





    labjac

    Friday, July 3, 2020 5:29 AM

All replies

  • Hi labjac,

    Thank you for posting here.

    Please pay attention to this class.

        public class NewHuNrSymbols
        {
            public int sequencer;
            public DataSet tableRecords;
    
            public NewHuNrSymbols()
            {
                sequencer = 1;
                var tableRecords = new DataSet();
            }
        }

    You created a new local variable in the constructor, and the tableRecords property of the class has not been assigned.

    You can delete that "var" to solve this problem.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, July 3, 2020 7:29 AM
  • Hi,

    Has your issue been resolved?

    If so, please click on the "Mark as answer" option of the reply that solved your question, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 8, 2020 9:44 AM
  • hi

     public NewHuNrSymbols()
            {
                sequencer = 1;
                var tableRecords = new DataSet();
    	    this.tableRecords =  tableRecords;	
            }

    Thanks and Regards

    Friday, July 10, 2020 3:39 PM