Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to resolve null refernce exception
when calling a method tu = obj.chkActiveTimer(ownerID); throwing exception.It works for some time and then throws exception
private void tmr_Tick(object sender, EventArgs e)
       {
           try
           {

               tmr.Stop();
               DataTable dt = new DataTable();

               if (ownerID != null && ownerID != 0)
               {
                   tu = obj.chkActiveTimer(ownerID);
                   dt = tu.TimerUtilityDt;
               }
               tmr.Start();



               if (dt.Rows.Count > 0)
               {

                   if (flag == 0 && !_timerRunning)
                   {
                       if (dt.Rows[0]["ON_TIMER"].ToString() == "Y")
                       {

                           _startTime = DateTime.Now;
                           _totalElapsedTime = _currentElapsedTime;
                           _currentElapsedTime = TimeSpan.Zero;
                           if (linkUT.Text == "Project Task")
                           {
                               changeUTType();
                           }

                           flag = flag + 1;
                           comboBox1.Text = obj.getProName(dt.Rows[0]["project_ref_id"].ToString());
                           //   comboBox1_SelectedIndexChanged(this, EventArgs.Empty);
                           cbTaskList.ValueMember = "REF_ID";
                           cbTaskList.DisplayMember = "SCOPE_DETAILS";
                           //cbTaskList.DataSource = dt;
                           cbTaskList.SelectedValue = dt.Rows[0]["ref_id"].ToString();
                           comboBox1.Enabled = false;
                           cbTaskList.Enabled = false;
                           linkUT.Enabled = false;


                           _timer.Start();

                           btnTimer.Text = "STOP";

                           this.btnTimer.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
                           this.btnTimer.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
                           btnTimer.BackgroundImage = ETimerUtility.Properties.Resources.stop_btn;
                           _currentElapsedTimeDisplay.Visible = true;

                       }
                   }
               }

               else
               {
                   flag = 0;
                   linkUT.Enabled = true;

                   comboBox1.Enabled = true;
                   cbTaskList.Enabled = true;
                   _timer.Stop();
                   _totalElapsedTime = TimeSpan.Zero;
                   _currentElapsedTime = TimeSpan.Zero;
                   string taskID = cbTaskList.SelectedValue.ToString();
                   if (taskID != "0" && taskID != "-1" && taskID != "-2" && projectID != "0" && projectID != "" && projectID != null)
                   {
                       lblAllocHrs.Visible = lblUsedHrs.Visible = true;

                       tu = obj.CalculateHrs(projectID, taskID, ownerID);
                       dt = tu.TimerUtilityDt;
                       if (dt.Rows.Count > 0)
                       {
                           lblAllocHrs.Text = lblUsedHrs.Text = "";
                           lblAllocHrs.Text = "Allocated Hrs" + " : " + dt.Rows[0]["allocatedhrs"].ToString();
                           lblUsedHrs.Text = "Used Hrs" + " : " + dt.Rows[0]["usedhrs"].ToString();
                       }
                   }
                   btnTimer.BackgroundImage = ETimerUtility.Properties.Resources.start_btn;
                   btnTimer.Text = "START";
                   this.btnTimer.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
                   this.btnTimer.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
                   _currentElapsedTimeDisplay.Text = "00";
                   _lblCurrentElapsedTimeMin.Text = "00";
                   _lblCurrentDisplayTimeSec.Text = "00";
               }
               dt.Dispose();

           }


           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }


What I have tried:

How to resolve null refernce exception 
when calling a method  tu = obj.chkActiveTimer(ownerID); throwing exception.It works for some time and then throws exception 
Posted
Updated 24-Feb-17 23:14pm
Comments
Richard MacCutchan 25-Feb-17 5:00am    
You need to use your debugger to find out why obj is null. It is not always a good idea to use non local references inside event handlers.
Graeme_Grant 25-Feb-17 5:06am    
I was about to write almost the same thing! :)
Michael_Davies 25-Feb-17 5:07am    
What is null, tu or obj, use the debugger to see which element is null and work from there, the difficulty in trying to help you is that both are global variables declared, instantiated and manipulated elsewhere in your code that we cannot see.

The debugger is your friend, once you know which variable is null you can set a watch to see when it occurs.

1 solution

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
Member 10202625 25-Feb-17 5:45am    
Sir, I have tried to find out .I am calling service method on timer tick every second .First of all it runs perfectly but after some time it throws error
OriginalGriff 25-Feb-17 6:04am    
We can't do it for you - you need to use the debugger to find out what is null, and then look for why. We do not have your data to work from, so we can't duplicate your problem! And if you can't duplicate a problem, you can't fix it...
Ralf Meier 25-Feb-17 8:44am    
The question, which guides you to the goal, was asked by Michael Davis : "What is null, tu or obj" - you don't answered that - and I suppose you also don't looked for that.
In your code you only look if you have an Owner-ID.
So ... perhaps you try to find out what is the matter with those both object and why one of it is Null ...
OriginalGriff 25-Feb-17 8:49am    
And to do that, he needs to use the debugger ... :laugh:
Ralf Meier 25-Feb-17 8:58am    
Of course ... no word against anything you wrote ... I only tried to point the OP into THE direction ... ;-)

And ... I can't understand why nowadays people no more make use of the debugger ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900