0

I am trying to create a system that allows the user to add exam questions to file for later retrieval. I have declared a record with the following fields:

Structure recQuestion
    Public fldQuestion As String
    Public fldAnswer As String
    Public fldPicture As PictureBox
    Public fldExamName As String
    Public fldExamNumber As Integer
    Public fldMark As Integer
End Structure

Public oneExamQuestionRecord As recQuestion

I then have a form which allows the user to add the details to the various textboxes; the question, the answer and so on and also includes a facility to allow the user to select and display an image in the picturebox.

To add the details to file I have created a button (btnAdd) with the following code:

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

    Dim testFileName As String = CurDir() & "/testFile.dat"

    With oneExamQuestionRecord
        .fldAnswer = txtAnswer.Text
        .fldExamName = txtExamTitle.Text
        .fldExamNumber = CInt(txtExamNumber.Text)
        .fldMark = CInt(txtMark.Text)
        .fldPicture.Image = PictureBox1.Image
        .fldQuestion = txtQuestion.Text
    End With

I get the below error message:

 "Object reference not set to an instance of an object"

when the below line is being executed:

.fldPicture.Image = PictureBox1.Image 

Any suggestions?

phoenix
  • 3,069
  • 3
  • 22
  • 29
  • That wont work. a) `fldPicture` is defined as a PictureBox not an image. that is also why you get an NRE - that picture box is nothing. b) The image will have to be converted to a Base64String, but if this is destined for some random access flat file, the length will vary which rules that out. Just save the path to the image, but I'd use a modern storage means like serializing a `List(of T)` using a class (not structure) or a database – Ňɏssa Pøngjǣrdenlarp Feb 14 '16 at 21:01
  • Thanks Plutonix for your extremely quick response. I was hoping for something simple but I will need to go back to the drawing board. I'll need to find out more about 'serializing a List(of T) using a class'. – Mr A McCanny Feb 14 '16 at 21:08
  • A database is not hard at all. [This answer covers serializing a List](http://stackoverflow.com/a/33529974/1070452). It takes 2-3 lines to save a list of N items. See also [Five Minute starter Guide to Lists and Classes](http://stackoverflow.com/a/34164458/1070452) – Ňɏssa Pøngjǣrdenlarp Feb 14 '16 at 21:10

0 Answers0