Click here to Skip to main content
15,919,931 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am currently developing a bio-metric application and everything seems to work well until it gets to the code snippet below. The variable fs generates an error "Object reference not set to an instance of an object"

Try
Using fs As FileStream = IO.File.Open(lblPath.Text + "/" + txtFoundationNo.Text + ".fpt", IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
Template.Serialize(fs)
End Using
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Enrollment Error Alert!")
End Try

What I have tried:

I have tried different means of declaring the variable
Some of the declarations tried are as stated listed below:

1.
Dim fs As New IO.FileStream(lblPath.Text + "/" + txtFoundationNo.Text + ".fpt", IO.FileMode.OpenOrCreate, IO.FileAccess.Write)

2.
Dim fs As New IO.FileStream(lblPath.Text + "/" + txtFoundationNo.Text + ".fpt", IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
Using fs
Template.Serialize(fs)
End Using

I still end up getting the same error message.
Posted
Updated 8-Jun-17 5:39am
Comments
FranzBe 6-Jun-17 8:00am    
Try to compose the fileName with path before calling New FileStream and check if the contents of the variable points to your file.

Dim fileNameWithExtention As String = txtFoundationNo.Text + ".fpt"
Dim fileNameWithPath As String = System.IO.Path.Combine(lblPath.Text, fileNameWithExtention)

Dim fs As New IO.FileStream(fileNameWithPath, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
SamFad 6-Jun-17 8:19am    
Thanks for your reply.
I still got the same error message
Richard MacCutchan 6-Jun-17 8:27am    
You should use your debugger to step through the code and find out exactly which reference is not getting set.
SamFad 6-Jun-17 8:40am    
Thank you sir,
I have tried using the debugger severally but seems I couldn't place my finger on where exactly the error is.

Before the error occur using the debugger to inspect the values of the variable fs, I get the following values:

fs|{System.IO.FileStream}
*CanRead == False
*CanSeek == True
*CanTimeout == False
*CanWrite == True
*DefaultBufferSize == 4096
*ERROR_BROKEN_PIPE == 109
*ERROR_NO_DATA == 232
*GENERIC_READ == -214748648
*Handle == 1448
*IsAsync == False
*Length == 0
*Position == 0
Is there any value in the above that can serve as a pointer as to the possible cause of the error?
Richard MacCutchan 6-Jun-17 8:59am    
Which line of code did the error occur on? From looking at the above values it appears that the FileStream object is valid, so you must look further.

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Thank you all.

I have solved the problem code. The code block below shows how I was able to resolve the problem.

1. Dim fileNameWithExtention As String = txtFoundationNo.Text + ".fpt"
2. Dim fileNameWithPath As String = lblPath.Text & "\" & fileNameWithExtention
3. fileNameWithPath = Strings.Replace(fileNameWithPath, "/", "\")
4.
5. Using fs As IO.FileStream = IO.File.Open(fileNameWithPath, IO.FileMode.Create, IO.FileAccess.Write)
6. Template.Serialize(fs)
7. End Using

OriginalGriff, your solution was a bam! it got me thinking. FranzBe clue on was also useful.
Line 5 was however where the solution lies. All the earlier declarations in my previous code actually created the files with a zero(0) byte. So by the time it gets to line 6, variable fs will throw exception error.
What I eventually did was to include the filename (i.e.fileNameWithPath) in declaration at line 5 and it worked like magic.
Thank you all once again for your useful contributions.
 
Share this answer
 
Comments
Member 14551914 13-Aug-19 1:44am    
protected void login_click_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Registrationconnectionstring"].ConnectionString);
con.Open();
string checkuser = "select count(*) from logintab where username=' " + TextBoxUsername.Text + "'";
SqlCommand com = new SqlCommand(checkuser, con);
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
con.Close();
if (temp == 1)
{
con.Open();
string checkPasswordQuery = "select password from logintab where username=' " + TextBoxUsername.Text + "'";
SqlCommand passcom = new SqlCommand(checkPasswordQuery, con);
string password = passcom.ExecuteScalar().ToString().Replace(" ", "");
if (password == TextBoxPassword.Text)
{
Session["new"] = TextBoxPassword.Text;
Response.Write("Successfully");
}
else
{
Response.Write("Incorrect password");
}
}
else
{
Response.Write("User name incorrect");
}
}
}


I used this code for getting values from db but its showing error.
Error: object reference not set an instance object

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900