none
Object reference not set to an instance of an object.

    Question

  • hi experts,

    i'm having a problem (again) with my radiobuttonlist the values are in my sql database and when i click on 1 of them, they need to go into my db but I allways get this error: Object reference not set to an instance of an object.
    i ran debug and the fault is here  can somebody help me
    btw i'm making a survey




    Code Snippet

    int Answer = Convert.ToInt16(rblquestion3.SelectedItem.Value);
    answerAdapter.answerIngeven(3, userID, null, 3);


    ps: sorry for bad english
    Sunday, April 27, 2008 1:03 AM

Answers

  • You have a couple problems with this code:

     

    1. The reason that SelectedItem is null is because you don't have a default item in rblVraag3 set with Selected to True. This means that if no items are selected then rblVraag3.SelectedItem will be null.  Here's one way to fix that:

     

    Code Snippet

    <asp:RadioButtonList ID="rblVraag3" runat="server">

        <asp:ListItem Selected="True" Value="1">One</< FONT>asp:ListItem>

        <asp:ListItem Value="2">Two</< FONT>asp:ListItem>

        <asp:ListItem Value="3">Three</< FONT>asp:ListItem>

    </< FONT>asp:RadioButtonList>

     

    2. If you don't want to set a default selected item in your RadioButtonList, then you can use the following code to ensure that Antwoord defaults to 1:

     

    Code Snippet

    int Antwoord = 1;

     

    if (rblVraag3.SelectedItem == null)

    {

        // tell the user they must select an item

    }

    else

    {

        Antwoord = int.Parse(rblVraag3.SelectedItem.Value);

    }

     

    Joe

    Sunday, April 27, 2008 6:59 PM
  • All the second code snippet does is show you how to check for a null value.  Since your RadioButtonList didn't have a default value where Selected was true and the user did not select an item rblVraag3.SelectedItem was null.  If it isn't null, then you can read what the value is.

     

    If you have other RadioButtonList controls, then you should create a new variable for them and set that variable to its default value.  Antwoord is a variable and can be set to anything you want, whether its 1 or 6. I set it to 1 because I though you needed to figure out how to get it to default to 1 if you weren't able to read from rblVraag3.

     

    Joe

    Sunday, April 27, 2008 10:42 PM

All replies

  • Hi,

     

    It looks like answerAdapter is null.  Make sure that answerAdapter gets instantiated prior to using it. Also, it is often useful to check for null before using an object, especially if you are supposed to receive an object reference from other code.

     

    Code Snippet

    if (answerAdapter != null)

    {

        answerAdapter.answerIngeven(3, userID, null, 3);

    }

     

    or

     

    Code Snippet

    if (answerAdapter == null)

    {

        answerAdapter = new AnswerAdapter();

    }

     

    answerAdapter.answerIngeven(3, userID, null, 3);

     

    Of course, this might not fit your situation, so you'll have to find a scenario that makes sense to you.

     

    Joe

    Sunday, April 27, 2008 1:56 AM
  • ok joe i think you are right about the 0 value

    i just ran debug and the value of "Antwoord" (i marked it in red) is 0 and that has to be 1 or something.

    Code Snippet

    AntwoordenTableAdapter AntwoordAdapter = new AntwoordenTableAdapter();
    int Antwoord = Convert.ToInt16(rblVraag3.SelectedItem.Value);
    AntwoordAdapter.AntwoordIngeven(3, GebruikerID, null, Antwoord);


    does somebody now how it can be automaticly be 1,, i think it has to do something with that  "rblVraag3.SelectedItem.Value" cause when i go with my mouse over selecteditem  it gives null value.

    can somebody help me  by giving the code or something im to newbie to insert it myself =(

    thx for your effort

    Sunday, April 27, 2008 9:26 AM
  • someone who knows ?? =(
    Sunday, April 27, 2008 12:14 PM
  • You have a couple problems with this code:

     

    1. The reason that SelectedItem is null is because you don't have a default item in rblVraag3 set with Selected to True. This means that if no items are selected then rblVraag3.SelectedItem will be null.  Here's one way to fix that:

     

    Code Snippet

    <asp:RadioButtonList ID="rblVraag3" runat="server">

        <asp:ListItem Selected="True" Value="1">One</< FONT>asp:ListItem>

        <asp:ListItem Value="2">Two</< FONT>asp:ListItem>

        <asp:ListItem Value="3">Three</< FONT>asp:ListItem>

    </< FONT>asp:RadioButtonList>

     

    2. If you don't want to set a default selected item in your RadioButtonList, then you can use the following code to ensure that Antwoord defaults to 1:

     

    Code Snippet

    int Antwoord = 1;

     

    if (rblVraag3.SelectedItem == null)

    {

        // tell the user they must select an item

    }

    else

    {

        Antwoord = int.Parse(rblVraag3.SelectedItem.Value);

    }

     

    Joe

    Sunday, April 27, 2008 6:59 PM
  • i understand the first code snippet but it dont get the second

    how can i make a variable of antwoord cause in question 3 it has to be 1 or 2 but i have other questions where it has to be 6 or 7.

    in the second code snippet you give antwoord the value 1 but how can i make it variable.

    can you make a code snippet of that plz

    kind regards
    Sunday, April 27, 2008 10:21 PM
  • All the second code snippet does is show you how to check for a null value.  Since your RadioButtonList didn't have a default value where Selected was true and the user did not select an item rblVraag3.SelectedItem was null.  If it isn't null, then you can read what the value is.

     

    If you have other RadioButtonList controls, then you should create a new variable for them and set that variable to its default value.  Antwoord is a variable and can be set to anything you want, whether its 1 or 6. I set it to 1 because I though you needed to figure out how to get it to default to 1 if you weren't able to read from rblVraag3.

     

    Joe

    Sunday, April 27, 2008 10:42 PM
  • I have the same problem but with this code. Why?

                    while ((CurrentWord = FileReaderObject.ReadLine()) != null)
                    {
                        while (CurrentWord != "\n")
                        {
                            CurrentCharacter = (char)FileReaderObject.Read();
                            //if (CurrentCharacter == '\n')
                            //{
                            //    ((ArrayList)TChromosomes[TChromosomes.Count - 1]).Add(CurrentWord2);
                            //    TChromosomes.Add(new ArrayList());
                            //    TMask.Add(new ArrayList());
                            //    CurrentWord2 = "";
                            //}
                            //else
                            if (CurrentCharacter == ',')
                            {
                                TChromosomes.Add(CurrentWord2);
                                TMask.Add((int)(new Random().Next(0, 2)));
                                CurrentWord2 = "";
                            }
                            else
                            {
                                CurrentWord2 += CurrentCharacter;
                            }
                        }
                    }

    Help me ! :<

    Wednesday, July 24, 2013 10:49 AM