.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

How to avoid null pointer exception when you use invoke member in c#?

Posted By:      Posted Date: October 10, 2010    Points: 0   Category :.NET Framework
Hi ,

I am working on one of the projects where I have to use the Reflection. I have a file which has values of datatype double,string, date and time as well as empty data (null). When I import the data using invoke method, it returns the null value as one of the data in the file is empty and throws "null reference exception".
value =object.GetType().InvokeMember(string, BindingFlags.GetProperty, null, object, null);
catch {
        if (value == null) {                    
           value = null;
I catch the exception and forcefully make it null than crashing my project. When I do this, the performance of the import is very slow. The data fetch is very slow as it goes like 419  times into the catch. How to avoid this the slowness of the fetch.?  try and catch will take a lot of time in fetch as I am  catching the exception and making it null. But if I do not catch it then it will crash in the middle. I need some input on this issue. Please help me out with this.... The data fetch is perfect but it has become slow due to the try and catch..  

View Complete Post

More Related Resource Links

Page.FindControl Causing Null Reference Exception


Hi guys,

I developed a page in VWD 2008 and it works perfectly locally, but when I deployed it on the server every Page.FindControl() call is causing the NullReference Exception "Object reference not set to an instance of an object. " Can anyone help me out? Code is below


Mike Marshall

            For i As Integer = 1 To 10
                If (CType(Page.FindControl("last_" & i), TextBox).Text.Length > 0) Or (CType(Page.FindControl("first_" & i), TextBox).Text.Length > 0) Or (CType(Page.FindControl("dob_" & i), TextBox).Text.Length > 0) Then
                    Dim person_entry(7) As String
                    person_entry(0) = info_list.Count - 1
                    person_entry(1) = CType(Page.FindControl("last_" & i), TextBox).Text
                    person_entry(2) = CType(Page.FindControl("first_" & i), TextBox).Text
                    person_entry(3) = CType(Page.FindControl("dob_" & i), TextBox).Text
                    person_entry(4) = CType(Page.FindControl("appear_" & i), TextBox).Text
                    person_entry(5) = CType(Page.FindControl("mental_" & i), TextBox).Text
                    person_entry(6) = CType(Page.FindControl("behave_" & i), TextB

FileNotFound exception trying to open a site - Farm.local is Null - x64

  Hello I've created a simple asp.net application to open a site and display the title of the corresponding web. But i'm getting FileNotFoundException while trying to open the site. The same code works perfectly when i run it in a console app. My spec Windows Server 2008 R2 x64 SharePoint 2007 x64 Visual Studio 2005 My target for the asp.net app is set to 'Any CPU'. As far as permissions is considered i've checked that the current identity using which VS2005 hosts the asp.net app has full rights. In fact i've used the same identity for app pools in IIS. As an asp.net web application As a console application Any ideas? Regards NLV Visit SharePoint User group - India

Null reference exception

Hi.. I'm trying to retreive values from the textboxes in the footer row of the grid view. My code is : -         con.Open();        SqlCommand com = new SqlCommand("select count(*) from prod", con);        int c = (Int32)com.ExecuteScalar();                if (c == 0)        {            DataTable dt = new DataTable();            DataColumn c1 = new DataColumn("ID");            dt.Columns.Add(c1);            DataColumn c2 = new DataColumn("DESCRIPTION");            dt.Columns.Add(c2);            DataColumn c3 = new DataColumn("QUANTITY");            dt.Columns.Add(c3);            DataColumn c4 = new DataColumn("DATE");          &nbs

Null Reference Exception was not handled by user Object Reference not set to instance of an object

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Security.Principal;using System.Data;using System.Data.SqlClient;using System.Windows;using System.Collections; public partial class Default2 : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {     }    private bool tableCopied = false;    private DataTable originalDataTable;     protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)    {         if (e.Row.RowType == DataControlRowType.DataRow)        {            //string sValue = e.Row.Cells[3].Text;             if (!tableCopied)            {                originalDataTable = ((System.Data.DataRowView)e.Row.DataItem).Row.Table.Copy();                ViewState["originalDataTable"] = originalDataTable;      &nbs

Accessing a published web service gives null ref exception. Won't occur in test though?

I have an Asp.net web service that I created using Visual Studio 2005. I can acess the web service in my testenvironment, call methods and return data as expected. If this web service is published to the outside world (Outside of the company firewall), it will return a null reference exception. Does anyone have any idea why this might be happening? The same code is being used,against thesame database. The only difference is that the published website is giving back the null reference exception. If I type in the URL for the web service "https://secure.theSiteGeist.com/wsIndigo.asmx" a list of methods forthe web service is displayed in my browser. Yet if I try and call any methods on the web service I getback the null reference exception. Note that the URL I posted above is not the real name of my web service, but isjust used for reference sake.

CLR Profiler for the .NET Framework 2.0 : A bug (with possible fix!) - It throws exception - null ob

CLR Profiler for the .NET Framework 2.0   I tied to profile a service on my machine. But when I try to see the heap data (during reading of the log file) it throws exception that the object reference is not set (null object).   I debugged the CLR profiler and found that in class ReadNewLog, there is method "ReadFile" internal void ReadFile(long startFileOffset, long endFileOffset, ReadLogResult readLogResult, int requestedIndex) ...     if (matched > 0 && c != -1)     {         /* use some other stack trace as a reference */         int otherStackTraceId = ReadInt();         otherStackTraceId = stacktraceTable.MapTypeSizeStacktraceId(otherStackTraceId);         int[] stacktrace = stacktraceTable.IndexToStacktrace(otherStackTraceId);         if (matched > stacktrace.Length - hadTypeId)             matched = stacktrace.Length - hadTypeId;         for(int i = 0; i < matched; i++)         {             int funcId = stacktrace[i + hadTypeId];             Debug.Assert

NULL value exception when trying to save to stream


Im trying to resize an image. I have the original file and then try to resize it and save it to a new image.
However, I get an exception because the value of stream cannot be null. This makes sense, but I dont know how to prevent it:

Value cannot be null.
Parameter name: stream

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: stream

Source Error:

Line 395:
Line 396:        Dim toStream As System.IO.Stream
Line 397:        thumbnailBitmap.Save(toStream, origimage.RawFormat)
Line 398:        thumbnailGraph.Dispose()
Line 399:        thumbnailBitmap.Dispose()

Dim origimage As Image=System.Drawing.Image.FromStream(FU.PostedFile.InputStream)
        Dim thumbnailBitmap = New Bitmap(newWidth, newHeight)
        Dim t

Best way to avoid error message due to null vs integer incompatibility


I receive the error message below, I think because the database field in question is null (I realize I probably should not allow null values, but I can't even display the page to allow the user to enter a value---and I would prefer not to enter a default value in SQL).

How do I avoid this error message? If the column is null (and it is supposed to be an integer), do I need to change the value to something like "0" in the code behind---or is there any easier way to do this right in aspx page (see source of error message below)?  Either way, I'm not sure how to do either (I can't get any such lines of code to work here). Thank you.


Object cannot be cast from DBNull to other types.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Object cannot be cast from DBNull to other types.

Source Error:

Line 50:  <td><ItemTemplate>
Line 51:    <asp:Label ID="PriceLabel" run

Need to display Blank Member as a NULL in one of the column Level


 Hi ,

  1. select NON EMPTY {[Measures].[Balance], [Measures].[Peso Equiv]} ON COLUMNS,
    from [depositlib_22]
    where [REPORT DATE].[All Report Date]
    |                                                   | Balance     | Peso Equiv     |
    | All Particulars |        |                  |                | 8,286,706,398.76 | 373,066,873,125.05 |
    |         | RESIDENTS   |                  |                | 8,279,166,150.24 | 373,059,101,772.19 |
    |         |        | GOVERNMENT            |                |      68.38 |       68.38 |
    |         |        |                  | NATIONAL

The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value t


Hi All,

        int maxAge = db.tbl_zaposlenis.Where(p => p.IdSifra == 22)
                         .Max(p => p.IdSifra);

        zaposleni.IdSifra = maxAge;

How to display the largest number in the "IdSifra?


Remove member wiht contains null value


HI all 


i have data like this

Feb March April May

Bike 20 10 20 30

shoes 10 Null 10 20

Jacket 50 100 Null 50


i want to remove product containt null values in any period

if range time from Feb until may, data show


Feb March April May

Bike 20 10 20 30



if range time Feb until March, data shoe



Script Component: Null value Exception



When I ran the 2008 SSIS package I am getting error at Script component (Script Component: RunTime Error  The column has NULL Value).

at Microsoft.SqlServer.Dts.Pipeline.PipelineBuffer.CheckStatusAndNull(Int32 columnIndex)

at Microsoft.SqlServer.Dts.Pipeline.PipelineBuffer.GetInt32(Int32 columnIndex)

at ScriptComponent_14ee8da369a64bce8aff81b1e175b6b0.vbproj.Input0Buffer.get_RedemptionItem()

at ScriptComponent_14ee8da369a64bce8aff81b1e175b6b0.vbproj.ScriptMain.Input0_ProcessInputRow(Input0Buffer Row)

at ScriptComponent_14ee8da369a64bce8aff81b1e175b6b0.vbproj.UserComponent.Input0_ProcessInput(Input0Buffer Buffer)

at ScriptComponent_14ee8da369a64bce8aff81b1e175b6b0.vbproj.UserComponent.ProcessInput(Int32 InputID, PipelineBuffer Buffer)

at Microsoft.SqlServer.Dts.Pipeline.ScriptComponentHost.ProcessInput(Int32 inputID, PipelineBuffer buffer)

Please Suggest the best way to handle the NULL values in Script component (like is there any setting which can allow NULL values in Script component OUTput Buffer).

Thanks in advance



SSAS 2008 - Date Member Property gets converted from NULL to (Blank)


Hello everyone,

I have this cube with a dimension of investment deals.  The "Deal" member has a attribute named "Latest Acquisition Date" where this date can be null for some deals.  Once the cube is processed, the null dates always get converted to "(blank)" either in BIDS cube browser or in Excel.  This happens no matter what I do to the NullProcessing property of this date attribute.

Is there any way to have SSAS display [NULL] (as in nothing at all) instead of "(blank)" ?

Model with List as member: Member alwasys NULL after submit



I have following model class:

public class WebModel
    public List<ArticleModel> Articles { get; set; }

public class ArticleModel
    public int ID { get; set; }       

    public double ValueParam { get; set; }

Then I have the controller with the two actions:

public ActionResult Index()
      WebModel model = new WebModel();
      ModelConverter.ConvertToModel(model, controller);    
      return View(model);

public ActionResult Index(WebModel model, string saveButton)
      ModelValidation.ValidateWebModel(model, ModelState);
      if (ModelState.IsValid)
                return RedirectToAction("Create", "Article");

How do i solve the Genuine Channel Operation Exception : cannot connect to remote host "gtcp://null:

I am trying to register my application which uses remoting. When i pass in the needed parameters i get this exception. Can anyone explain me what GTCP is ? How do i register my application with this protocol ?
Shoeb Sayyed

Null Reference Exception while trying to initialise an Integer array


Hi ,

I am getting System.NullReferenceException:Object reference not set to an instance of an object

while executing the below code..!!

Dim arr() As Integer

Dim g,j As Integer



While g > 0
                arr(j) =0
                g = g - 1
                j = j + 1
End While

Could anybody help me out..!!!

Thanks in advance

Top .NET Performance Problems and how to avoid them

Every time I work with one of our .NET customers to help them with managing their application performance I come across the same problems as seen with other clients before: lots of ADO.NET queries, many hidden exceptions in core or 3rd party .NET libraries, slow 3rd party components, inefficient custom code
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend