Link to home
Start Free TrialLog in
Avatar of ethnarch
ethnarch

asked on

Problem with my application after changing some project settings

I stupidly changed the properties in one of my programs now it seems i have really messed things up.

What i did was right click on the on the directory under Solution in the solution explorer panel.
Then i changed both the assembly name and the root namespace.

I then realized this messed things up and i first went through the code changing the project name in lines like
    Friend WithEvents Loc1DS1 As WindowsApplication4.loc1DS
    Friend WithEvents Loc2DS1 As WindowsApplication4.loc2DS
    Friend WithEvents Loc3DS1 As WindowsApplication4.loc3DS

When this did not make the code work i went back and changed everything back to it's original settings including everything i changed in the code and the assembly name and root namespace. What the heck did i do, i can't figure it out.

now i keep getting this error

An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication4.exe

Additional information: Object reference not set to an instance of an object.
Avatar of ethnarch
ethnarch

ASKER

oh i get the error when i debug it by hitting f5
Hi ethnarch,
I will try to assist you if you could clarify a couple of points for me:

Where in your code does this exception occur (what line)?

How many projects in your solution?
Are they all in the same namespace?

Regards,
David
it doesn't occur on a line it occurs the second that i hit f5
i had left everything in it's default up until i did the above I was just trying to change it, not knowing it would be my undoing i only changed the project properties, and there is only one project in the solution
to add one more thing it is a single form application, it's a very simple program that just looks up barcodes numbers in a database and shows it to the user.
Hi again ethnarch,

So, you hit F5, and you get an exception message box straight away?

Do you have a Sub Main() in this project?
In your project properties, what is the Stratup object set to?

Does the exception message indicate a line where the exception is occurring?

Aer you running it in Debug or Release mode?

David
---I hit f5 and maybe about a few nano seconds go by, hehe or about a second so it runs some code i am assuming

---there is no sub main in my project

---the startup object is set to form 1

---no it does not indicate a line

---I was running it in release mode so then i tried changing it back to debug and It gave me different error message

An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication4.exe
Additional information: Object reference not set to an instance of an object.

, where it pointed out this line of code
DataSet11.Clear()

---Which happens to be inside this Function
 Sub setData(ByVal OkToFilter As Boolean)
        DataSet11.Clear()
        DataAdapColl(locNo).Fill(DataSet11)
        Dim dv1 = New DataView(DataSet11.Tables("MenuItem"))
        DataGrid1.DataSource = dv1

        If OkToFilter = True Then
            dv1.Rowfilter = ComboBox2.Text & " LIKE '%" & TextBox1.Text & "%'"
        End If

    End Sub

---So i tried to fix that error by changing it to this

    Sub setData(ByVal OkToFilter As Boolean)
        DataSet11 = New DataSet1() '<-----------------My attempt at fixing the problem
        DataSet11.Clear()
        DataAdapColl(locNo).Fill(DataSet11)
        Dim dv1 = New DataView(DataSet11.Tables("MenuItem"))
        DataGrid1.DataSource = dv1

        If OkToFilter = True Then
            dv1.Rowfilter = ComboBox2.Text & " LIKE '%" & TextBox1.Text & "%'"
        End If

    End Sub

---Then it gave me this error
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in microsoft.visualbasic.dll
Additional information: Collection index must be in the range 1 to the size of the collection.

--on this line of code

DataAdapColl(locNo).Fill(DataSet11)

---which is in the function listed above and here is where the DataAdapColl and locNo originates and is setup

Public Class Form1
    Inherits System.Windows.Forms.Form

    Dim srchColName As String
    Dim locNo As Integer = 2
    Dim DataAdapColl As New Collection()
    Dim dv1 As New DataView()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ComboBox1.Items.Add("Warehouse")
        ComboBox1.Items.Add("Gotham 1 (loc 1)")
        ComboBox1.Items.Add("Gotham 2 (loc 2)")
        ComboBox1.Text = "Gotham 1 (loc 1)"

        ComboBox2.Items.Add("PLU")
        ComboBox2.Items.Add("Description")
        DataAdapColl.Add(SqlDataAdapter2)
        DataAdapColl.Add(SqlDataAdapter3)
        DataAdapColl.Add(SqlDataAdapter4)
        setData(False)

---so now you should have all the code up until the setdata function i first mentioned above is called
Hi again ethnarch,

now we are getting somewhere.
The fact that you had your project set to Release mode meant that you weren't able to see the exact exception, and is also the reason why it didn't give a line where the error occurred.

I agree with the step you took to correct the initial exception (the dataset was = Nothing).

Now it seems that the variable "locNo" has a value outside the range of the DataAdaptColl collection.
The value must be anywhere from 1 to the number of items in the collection (collections are 1-based, unlike arrays).

Can you set a breaKpoint on the line where the exception is occurring, and when te debugger stops there, hover over the variable (locNo) to see its value.
Is it greater than 0 (zero)?
Is it less than DataAdaptColl.Count?

Regards,
David
---locNo is set to 2, i have that set as the default in the beginning of the Form Class before any code is run like this

Public Class Form1
    Inherits System.Windows.Forms.Form

    Dim srchColName As String
    Dim locNo As Integer = 2
    Dim DataAdapColl As New Collection()
    Dim dv1 As New DataView()

--- It's strange that it's out of range unless this code is incorrect
        DataAdapColl.Add(SqlDataAdapter2)
        DataAdapColl.Add(SqlDataAdapter3)
        DataAdapColl.Add(SqlDataAdapter4)

--- there should be a total of 3 items in the collection i tried changing the value of LocNo to 1 but i still get the same error.
Ok so i put a msg box with DataAdapColl.Count in it and it reads 0.  Somehow my add commands are not working on form load.

and just to add one more point, even when i put the msg box right after DataAdapColl.Add() it still reads 0
Ok,
Are the objects "SqlDataAdapter1", "SqlDataAdapter2", "SqlDataAdapter3" all set to instances of objets before you add them to the collection? ie. They are NOT NOTHING?
This code is in the windows form generated code section i think this is what you mean
        Me.SqlDataAdapter2 = New System.Data.SqlClient.SqlDataAdapter()
        Me.SqlDataAdapter3 = New System.Data.SqlClient.SqlDataAdapter()
        Me.SqlDataAdapter4 = New System.Data.SqlClient.SqlDataAdapter()

The form load function exists after the windows form generated code. I don't know if this even matters since it's not called till the form loads but i just figured i would let you know anyway.
Some more information that might be useful.

I tried putting a breakpoint on every line of my form code to see what would happen and it's almost as if it skips over lines of code. The last time it stops is on this line of code --ComboBox1.Text = "Gotham 1 (loc 1)"

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ComboBox1.Items.Add("Warehouse")
        ComboBox1.Items.Add("loc 1")
        ComboBox1.Items.Add("loc 2")
        ComboBox1.Text = "loc 1" '<-----------From this point on it skips and doesn't stop at the breakpoints

        ComboBox2.Items.Add("PLU")
        ComboBox2.Items.Add("Description")
        DataAdapColl.Add(SqlDataAdapter2)
        DataAdapColl.Add(SqlDataAdapter3)
        DataAdapColl.Add(SqlDataAdapter4)
        MsgBox(DataAdapColl.Count)
        'ComboBox2.Items.Add("Description")
        srchColName = "PLU"
        setData(False)
        'ComboBox2.Items.Add("693 8th Avenue")
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of doobdave
doobdave

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I did go through it line by line hence the previous statement
                         Qoute "I tried putting a breakpoint on every line of my form code to see what would happen and
                                   it's almost as if it skips over lines of code. The last time it stops is on this line of code --
                                   ComboBox1.Text = "Gotham 1 (loc 1)"

Maybe i am misunderstanding you?
Yep, the method I suggested is different from putting a breakpoint on every line.
Using your method, if an exception occurs, any breakpoints after that will not be hit (as I suspect is happening).
Are you pressing the 'Play' button or hitting F5? They both continue execution until the next breakpoint, except ni the circumstance I just mentioned.

If you keep pressing F8 or F11, it will 'step-thruogh' the code liine by line, so you will see exactly which line is causing problems.

Did you implement the Try..Catch?
i just tried the catch, it only gives me the error on the collection being out of range so i am taking a guess it has to do with the sqlAdapters i am going to try remaking them when i get time and start from scratch maybe some settings i can't see got changed when i changed the properties of my project.
Ok i solved it by just recoding that part of my project. I got rid of the collection and instead had the DataAdapter refill the Dataset everytime just changing the SQL Query instead of having different DataAdapters for each set of data, which is the right way to do it anyway.

Thankyou for the help though i'll give you the points for the effort.
i do have another question however though, do you know a source online or any books that explain Try catch statements they are really useful and i really don't know much about them.
Hi again,
yep, I think it's really important to understand Try..Catch blocks.

It is formally known as "Structured Exception Handling", as opposed to the old VB6 unstructured (On Error...).

I normally have one in every sub and function.

Here's some links to get you started:

http://www.c-sharpcorner.com/vbnet/vbarticles/VBASPExHSSK.asp

http://www.vbdotnetheaven.com/Code/Jun2003/2009.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconintrotoexceptionhandling.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconerrorhandlingusingtrycatchfinally.asp

Definition:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vastmTryCatchFinally.asp

HTH

David