NullReferenceException

Split from necro'd unrelated topic

Page 1 of 1

6 Replies - 554 Views - Last Post: 03 November 2010 - 06:19 AM Rate Topic: -----

#1 Guest_oynx*


Reputation:

NullReferenceException

Posted 03 November 2010 - 02:42 AM

Hey, I have 2 classes, one called 'Controller' one called 'MainForm : Form' both under the same namespace.
In the controller class I have:
     public class Controller {
        public static string mainPath = @"D:\Temp\";
        public string inputPath = mainPath + @"1-input\";


I want to call the inputPath in the MainForm
public class MainForm : Form
    {
        private Controller controller = null; 
public MainForm(Controller controller)
        {
            this.controller = controller;
        }public void btnOpenFile_Click(object sender, System.EventArgs e)
        {
            this.fDialog = new OpenFileDialog();
            fDialog.InitialDirectory = controller.inputPath; 


How do I get this code to work, currently I get a NullReferenceException.

Is This A Good Question/Topic? 0

Replies To: NullReferenceException

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: NullReferenceException

Posted 03 November 2010 - 04:09 AM

You haven't even told us what object is being reported as null! Are you passing a valid Controller object to the MainForm contructor?
Was This Post Helpful? 0
  • +
  • -

#3 Guest_oynx*


Reputation:

Re: NullReferenceException

Posted 03 November 2010 - 05:11 AM

yes it's a valid Controller object as it worked in a method before I implemented the form. And it's this line that is null:
fDialog.InitialDirectory = controller.inputPath;

the controller.inputPath
Was This Post Helpful? 0

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: NullReferenceException

Posted 03 November 2010 - 05:17 AM

Time to learn how to debug. I don't think your assumption that your controller is non-null is true. Set a breakpoint on that line and verify that's the case. Then when you find out it's not true, step into the creation of the MainForm class to see why that's the case.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_oynx*


Reputation:

Re: NullReferenceException

Posted 03 November 2010 - 05:45 AM

namespace PSAR

public MainForm(Controller controller)


Controller is: class PSAR.Controller

this.controller = controller;

which is Controller MainForm.controller = Controller controller;

step into:
namespace PSAR {
    class Program {
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.Run(new MainForm());
            Controller controller = new Controller();
            Console.ReadKey();
        }
    }
}

Was This Post Helpful? 0

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: NullReferenceException

Posted 03 November 2010 - 06:02 AM

Proving your assumption wrong.

Your MainForm constructor, where you assign its controller member variable:
public MainForm(Controller controller)

how you're calling it:
Application.Run(new MainForm());

See?
Was This Post Helpful? 0
  • +
  • -

#7 Guest_oynx*


Reputation:

Re: NullReferenceException

Posted 03 November 2010 - 06:19 AM

Oh well don't I feel a fool. Sorry, all of this was unnecessary. I didn't need the 'Controller controller' variable. The variables from the Controller class weren't static!
Was This Post Helpful? 0

Page 1 of 1