0

I have a class which allows me to store a time as follows.

    class LockTime
{

    public int Start { get; set; }

    public int End {get; set; }

    public LockTime (int start, int end)
    {
        Start = start;
        End = end;

    }

}

In another class I declare a list of this object.

private List<LockTime> listOfTimes;

I have a method in this 2nd class to then add to this list.

LockTime theTime = new LockTime((pickerTimeStart.Value.Hour + pickerTimeStart.Value.Minute), (pickerTimeEnd.Value.Hour + pickerTimeEnd.Value.Minute));

listOfTimes.Add(theTime);

When it trys to add to the list it throws the NullPointerException. In the debugger it shows 2 values in theTime variable. I don't quite understand why it's saying NullPointer.

Also at the start where I've declared listOfTimes it is underlined blue saying that the field is never assigned to anything and will always have a null value.

This small little issue is driving me nuts, am I just completely missing something. Please help!

1
  • 2
    Have you initialized listOfItems ? listOfTimes = new List<LockTime>(); ? Commented Apr 26, 2014 at 19:12

5 Answers 5

6

You need to initialize the field, otherwise its default value is null because List<T> is a reference type.

Use private List<LockTime> listOfTimes = new List<LockTime>();

Sign up to request clarification or add additional context in comments.

3 Comments

Sad thing is, I had tried = new List<LockTime> but I was missing the braces. I've wasted almost 2-3 hours pulling my hair out. Thanks for such a quick response.
@Jagga If you forgot the parentheses you would get an error like "A new expression requires (), [], or {} after type". What did you not understand about that? (Not a criticism of you, I'm just curious about what you thought it meant. When you know a language its hard to see how others misunderstand it.)
I kept doing ..=new List<LockTime>(0,0); and stuff. Then I started to change the accessor from private to public and the whole lot. I basically did a load of unnessecary changes simply because I didn't know you can say or perhaps remember I need the braces. For some reason my brain kept going, constructor requires 2 ints. I dunno.
4

Seems you didn't allocate the list object .

maybe change

private List<LockTime> listOfTimes;

to:

private List<LockTime> listOfTimes = new List<LockTime>();

Comments

2

You declared the list, but didn't initialize it, so it currently points to null.

private List listOfTimes = new List();

Comments

2

You declare but do not initialize the list. So you are trying to call Add on what is essentially null.

Comments

2

You have not initialized the list. The place where you are seeing the warning do something like this ,

   private List<LockTime> listOfTimes = new List <LockTime>()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.