program that asks trivia questions and pulls data from an access database. I'm getting an error that says Object variable not set. System.NullReferenceException HResult=0x80004003 Message=Object variable or With block variable not set. Source=Microsoft.VisualBasic StackTrace: at Microsoft.VisualBasic.CompilerServices.Symbols.Container..ctor(Object Instance) at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateSet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean OptimisticSet, Boolean RValueBase, CallType CallType) at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateSetComplex(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames,
I'm running a visual basic program that asks trivia questions and pulls data from an access
System.NullReferenceException
HResult=0x80004003
Message=Object variable or With block variable not set.
Source=Microsoft.VisualBasic
StackTrace:
at Microsoft.VisualBasic.CompilerServices.Symbols.Container..ctor(Object Instance)
at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateSet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean OptimisticSet, Boolean RValueBase, CallType CallType)
at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateSetComplex(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean OptimisticSet, Boolean RValueBase)
at Trivia_Project.MainForm.MainForm_Load(Object sender, EventArgs e) in C:\Users\mikek\Documents\Visual Studio 2019\Visual-Basic\Trivia Solution-ModifyThis\Trivia Project\Main Form.vb:line 30
This exception was originally thrown at this call stack:
[External Code]
Trivia_Project.MainForm.MainForm_Load(Object, System.EventArgs) in Main Form.vb
Here's the code:
' Project name: Trivia Project
' Project purpose: Displays trivia questions and
' answers and the number of incorrect
' answers made by the user
' Created/revised by: <Mike Kerklin> on <4/29/21>
Option Explicit On
Option Infer Off
Public Class MainForm
Public Shared DtTable As New DataTable
Private Shared ReadOnly SelectedQuest As Integer
Private Shared ReadOnly TotalScore As Integer
Private Shared ReadOnly DtSelected As New DataTable
Private Shared ReadOnly QuesNo As Object
Private numIncorrect() As Integer = {-1, -1, -1, -1, -1, -1, -1, -1}
Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click
Me.Close()
End Sub
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'TriviaDataSet.tblGame' table. You can move, or remove it, as needed.
Me.TblGameTableAdapter.Fill(Me.TriviaDataSet.tblGame)
QuesNo.Text = (TblGameBindingSource.Position + 1).ToString
End Sub
Private Sub NewButton_Click(sender As Object, e As EventArgs) Handles newButton.Click
' starts a new game
TblGameBindingSource.MoveFirst()
questionNumLabel.Text = "Question 1:"
newButton.Enabled = False
submitButton.Enabled = True
End Sub
Private Sub SubmitButton_Click(sender As Object, e As EventArgs) Handles submitButton.Click
' determines whether the user's answer is correct
' and the number of incorrect answers
Dim ptrPosition As Integer
Dim userAnswer As String
' store record pointer's position
ptrPosition = TblGameBindingSource.Position
' determine selected radio button
Select Case True
Case aRadioButton.Checked
userAnswer = aRadioButton.Text.Substring(1, 1)
Case bRadioButton.Checked
userAnswer = bRadioButton.Text.Substring(1, 1)
Case cRadioButton.Checked
userAnswer = cRadioButton.Text.Substring(1, 1)
Case Else
userAnswer = dRadioButton.Text.Substring(1, 1)
End Select
' if necessary, update the number of incorrect answers
If userAnswer <>
TriviaDataSet.tblGame(ptrPosition).CorrectAnswer Then
numIncorrect(ptrPosition) = 0
Else
numIncorrect(ptrPosition) = 1
End If
MessageBox.Show("Answer Submitted!")
End Sub
Private Sub NextButton_Click(sender As Object, e As EventArgs) Handles nextButton.Click
TblGameBindingSource.MoveNext()
End Sub
Private Sub JumpToButton_Click(sender As Object, e As EventArgs) Handles jumpToButton.Click
Try
Dim qno As Integer = CInt(InputBox("Enter Question Number to jump to: ", "Trivia Game"))
TblGameBindingSource.Position = qno - 1
Catch ex As Exception
MessageBox.Show(ex.Message, "Trivia Game")
End Try
QuesNo.Text = (TblGameBindingSource.Position + 1).ToString
End Sub
Private Sub NumberIncorrect_Click(sender As Object, e As EventArgs) Handles numberIncorrect.Click
Dim incorrect As Integer = 0
Dim attempted As Integer = 0
Dim i As Integer
For i = 0 To 8
If numIncorrect(i) = 0 Then
incorrect += 1
End If
If numIncorrect(i) <> -1 Then
attempted += 1
End If
Next
MessageBox.Show("Incorrect Answers: " & incorrect.ToString & Environment.NewLine & "Attempted Questions: " & attempted.ToString(), "Trivia Game", MessageBoxButtons.OK, MessageBoxIcon.Information)
QuesNo.Text = (TblGameBindingSource.Position + 1).ToString
End Sub
End Class

Step by step
Solved in 2 steps









