PDA

View Full Version : your all programmers right? for the love of god i need some help with this code!!!



dangleberries
April 18th, 2011, 15:23
The part with "#" above and below just wont work!? it just keeps saying:

"nullreference exception was handled
Object variable or With block variable not set."

someone please help this is my coursework for computing for wednesday:(

Also this is written with visual basic express.net 2010 btw.


Public Class Form1
Dim fname, sname, fletter_name, fletter_sname, random_let As String
Dim random_num As Integer
Dim refcode As Object
Dim counts As Integer
Dim minimum As Integer
Dim position As Integer
Dim item_num As Integer
Dim list(4) As Object
Dim counter As Integer




Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

fname = InputBox("please enter first name")
sname = InputBox("please enter second name")
TextBox1.Text = fname
TextBox2.Text = sname
Call extracting_letters(fletter_name, fletter_sname)

Call random_generator(random_num, random_let)
TextBox3.Text = fletter_name & fletter_sname & random_num & random_let
Call queries(list, item_num, minimum)
End Sub


Private Sub extracting_letters(ByRef fname As String, ByRef sname As String)
'picks out first letter from both first and second name.
fletter_name = TextBox1.Text.Substring(0, 1)
fletter_sname = TextBox2.Text.Substring(0, 1)

End Sub
Private Sub random_generator(ByRef random_num As Integer, ByRef random_let As String)

'generate random number.
Dim number As Integer
Randomize()
random_num = number
number = Int(Rnd() * 10) + 1
'generate random letter.
random_let = Chr(Asc("a") + Int(26 * Rnd()))

End Sub
Private Sub queries(ByRef list, ByRef item_num, ByRef minimum)
Dim choice As Integer
Do
MsgBox("welcome to the graphics card slection screen")
choice = InputBox("Pease select an option to find:" & vbNewLine & "1 The fastest clock speed" & vbNewLine & "2 The most powerful processor" & vbNewLine & "3 The lowest storage for the highest cost" & vbNewLine & "4 Or end and display")
If choice = 1 Then
Call graphics_speeds(item_num, list(4), minimum)
End If
Loop Until choice = 4

End Sub

Private Sub graphics_speeds(ByRef item_num, ByRef list(), ByRef minimum)

Call setup(minimum, item_num, list) 'sets up list.
Call get_minimum(minimum) ' gets users input for number to search.
Call Find_minimum(minimum, item_num, list) ' counts the number of times users target number appears in the list.
End Sub
Private Sub setup(ByRef minimum, ByRef item_num, ByRef list)
'sets all values to zero.
minimum = 0
item_num = 0

lstspeeds.Items.Clear() 'clears list.

lstspeeds.Items.Add(1986)
lstspeeds.Items.Add(550)
lstspeeds.Items.Add(870)
lstspeeds.Items.Add(790)
lstspeeds.Items.Add(1600) 'fill list with array.
###############################################
Do
list(item_num) = Val(lstspeeds.Items(item_num))
###############################################
item_num = item_num + 1
Loop Until item_num = lstspeeds.Items.Count 'what do you say to make the program racognise how many items it has to go through?

End Sub

Private Sub get_minimum(ByRef minimum)
minimum = InputBox("Enter the number you would like to count") 'asking for number to be counted.
End Sub
Private Sub Find_minimum(ByRef minimum, ByRef item_num, ByRef list())
Dim position As Integer
Dim counts As Integer
counts = 0 'sets number of counts to 0

For position = 0 To item_num - 1 'sets sets position of counter to 0.
If list(position) > minimum Then
counts = counts + 1 'if the counter meets a number in the list that matches target the counts increase by 1.
End If
Next
MsgBox("There were " & (counts) & " graphics cards which have a faster clock speed")

End Sub
End class

Mothrayas
April 18th, 2011, 16:16
your all programmers right?

To answer that question, no.

I wonder where you got that idea from.



As for the problem, I'm sorry but I can't help you, because I don't know BASIC.

dangleberries
April 18th, 2011, 16:25
To answer that question, no.

I wonder where you got that idea from.

.

well surly theres some programmers on your team making wfto??

and basically im just asking round different forums.

verhoevenv
April 18th, 2011, 16:33
Your variable 'list' is null. In the subroutine 'queries' it still holds a list (of 5 nulls), you pass list(4) to 'graphics_speed' so the variable 'list' there contains a null (it's also probably not a list anymore), which you pass to 'setup' and then try to access.

Learn to use the debugger. It can tell you the values of your variables at runtime. Very useful.

dangleberries
April 18th, 2011, 17:48
Thanks, ill have another attempt at fixing it taking both of your sets of advice into account.

---------- Post added at 17:48 ---------- Previous post was at 17:42 ----------


Your variable 'list' is null. In the subroutine 'queries' it still holds a list (of 5 nulls), you pass list(4) to 'graphics_speed' so the variable 'list' there contains a null (it's also probably not a list anymore), which you pass to 'setup' and then try to access.

Learn to use the debugger. It can tell you the values of your variables at runtime. Very useful.

i love you. works perfectly cheers i didnt notice that!

Ogre
April 18th, 2011, 20:47
i love you. works perfectly cheers i didnt notice that!

Sometimes you can't see the error until someone elses sees it :) Good for you!

dangleberries
April 19th, 2011, 17:09
Sometimes you can't see the error until someone elses sees it :) Good for you!

very true, and very happy now i can hand in my course work!