Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8068703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:46:34+00:00 2026-06-05T12:46:34+00:00

I have a null object reference exception in executing this code. I have a

  • 0

I have a null object reference exception in executing this code. I have a file html control and my Razor code looks like this:

@using (Html.BeginForm("AddFiles", "AdministerFiles", FormMethod.Post))
 {
    <p><label for="filename">Filename : </label><input type="text" name="filename" /></p>
    <p><label for="description">Description : </label><input type="text" name="description" /></p>
    <p><input type="file" name="file" id="file" /> </p>
    <p><input type="hidden" value="@Model[2]" name="catid"/></p>

    <input type="submit" value="Upload Now!" />
    <input type="reset" value="Reset" />
 }    

My AdministerFilesController is this:

[HttpPost]
    public ActionResult AddFiles(HttpPostedFileBase file, int catid, string description)
    {
        // Verify that the user selected a file {
        if (file != null && file.ContentLength > 0)
        {
            // extract only the filename
            var filename = Path.GetFileName(file.FileName);
            // store the file inside ~/App_Data/Uploads folder
            var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"), filename);
            file.SaveAs(path);
        }
        RefDataLinks_mst fileDetails = new RefDataLinks_mst()
        {
            LastModified = new DateTime(2012,1,1),
            CategoryID = catid,
            DataFileName = Path.GetFileName(file.FileName),
            DataTitle = "SAMPLETITLE",
            DataID = ((new Random()).Next(100, 1000)),
            DataFilePath = "Sample/Path",
            Description = description,
            UpdatedBy = "Arjel",
            FileSize = file.ContentLength
        };
        bool b = ServiceReferenceHelper.AddFile(fileDetails);
        // Redirect back to the index action to show the form once again
        return Redirect("AddFiles?catid=" + catid); //both works
        //return RedirectToAction("ViewDataFiles", new { catid = catid }); 
    }

The data from catid and description are received but the file references to a null object. What could be the problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:46:35+00:00Added an answer on June 5, 2026 at 12:46 pm

    Uploads actually require you to specify the enctype attribute on the form. Here’s an example of how to do that:

    @using (Html.BeginForm("AddFiles", "AdministerFiles", FormMethod.Post, 
        new { enctype = "multipart/form-data" }))
    { 
       ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to have an 'UnassignedDepartment' object instead of letting employees have a null
i have this JSON object: {error:null, result:[{id:1234567890, count:1, recipients: [u3848, u8958, u7477474 ], dateCreated:2012-06-13T09:13:45.989Z
I have a solution to check NULL values extracted from object, However i feel
I have a class that occasionally gets passed null for File objects. During normal
I have used this code in my AS3 document class to remove all objects
i have this code: Texture2D *cachedTexture; if(cachedTexture = [cachedTextures objectForKey:aName]) { return cachedTexture; }
I have some inline code in an aspx file that executes: <dd><%= encode(Listing.Address.Line1) %>
I'm trying to reference another object I've defined in a Spring config file from
I have a basic doubt, when I was analyzing object and reference. int a;
I have a form within my Asp.Net MVC application which returns a Null reference

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.