How to Modify a Getter and Setter, to Handle a Null Reference Exception

Getter returning NULL c#

This is auto-properties introduced in C# 3.0 and later

Change the property to:

private string provinceCode { get; set; }

Instead of a separate method:

public string ProvinceCode
{
get; set;
}

Null Reference Exception C# get set

You need to initialize/instantiate your list.

public List<double> NewData
{
get;
set;
}

public InfoFile(String fileName, String groupName)
{
// initialize NewData to a new instance of List<double>
NewData = new List<double>();

this.group = groupName;
test = File.ReadAllLines(fileName);
FileInfo label = new FileInfo(fileName);
this.name = Path.GetFileName(fileName);
isDrawn = false;
for (int t = 2; t < test.Length; t++)
{
NewData.Add(double.Parse(test[t].Substring(6, 4)));

}

}

Docs:

In C# 3.0 and later, auto-implemented properties make
property-declaration more concise when no additional logic is required
in the property accessors. They also enable client code to create
objects. When you declare a property as shown in the following
example, the compiler creates a private, anonymous backing field that
can only be accessed through the property's get and set accessors.

http://msdn.microsoft.com/en-us/library/bb384054.aspx


Depending on your scenario, as stated by @EricLippert in his comment to your OP, a setter is rarely used when exposing a List/Collection. It's more common to "Lazy Load" said List/Collection:

public List<double> _newData;
public List<double> NewData
{
get
{
if(_newData == null)
_newData = new List<double>();

return _newData;
}
}

Manipulate the setter to avoid null

You need to declare a backing field, which I’ll call _code here:

private string _code = "";
public string Code
{
get
{
return _code;
}
set
{
if (value == null)
_code = "";
else
_code = value;
}
}

I’ve also renamed the property to Code because it is customary in C# to capitalise everything that’s public.

Note that in your own code, you wrote default(string), but this is the same as null.

Instead of setting _code to "", it is common practice to throw an exception:

private string _code = "";
public string Code
{
get
{
return _code;
}
set
{
if (value == null)
throw new ArgumentNullException("value");
_code = value;
}
}

Null Pointer exception in getter function

If you are creating an object from pojo once, you don't have to create another one to get so remove the Pojo pojo = new Pojo(); and just put :

String id=pojo.getId();

Your code should be like :

JSONArray arr1 = new JSONArray(strServerResponse);
JSONObject jsonObj1 = arr.getJSONObject(0);
pojo = new Pojo();
empid = jsonObj1.optString("empid");
pojo.setId(empid);
String id = pojo.getId();

Then with the SAME OBJECT you'll have your id.

Null pointer exception on getter setter in java

You never initialized sharedData, so its value is null, calling a method on it got your program to crash.

I think you're trying to use Singleton Pattern. Try the below:

private static SharedData instance = new SharedData();   \\ Initialize here

private SharedData() { // Make it private....
// randomizeServers();
}

// data to be shared
private double src_latitude = -1;
private double src_longitude = -1;
private double end_latitude = -1;
private double end_longitude = -1;

//Getter-Setters
public static SharedData getInstance() {
return instance;
}

C# variable that should never be null throws null reference exception

Your UserCalls property can be null in a multithreaded context.

Given:

public static List<UserCall> UserCalls
{
get
{
if (userCalls == null)
{
userCalls = new List<UserCall>();
}

return userCalls;
}

set
{
userCalls = value;
}
}

Consider the following sequence with two threads, [A] and [B] where userCalls is currently non-null.

[A] Accesses `UserCalls` getter. 
[A] Checks if `userCalls` is null. It is not, so it will skip the assignment.
[B] Accesses `UserCalls` setter, about to set it to `null`.
[B] Sets `userCalls` to null.
[A] Returns `userCalls`, which is now null => BANG!

You can protect against this by using a lock:

public static List<UserCall> UserCalls
{
get
{
lock (_locker)
{
if (userCalls == null)
{
userCalls = new List<UserCall>();
}

return userCalls;
}
}

set
{
lock (_locker)
{
userCalls = value;
}
}
}

static object _locker = new object();

However, note that there is nothing to prevent something from setting one of the elements of UserCalls to null. If something does that, you will also get a NullReferenceException in calls like UserCalls.FirstOrDefault(x => x.UserID == userID);, since x would be null and using x.UserID will give you the null reference exception.



Related Topics



Leave a reply



Submit