0

I looked on another question similar to this but couldn't quite understand what they did to solve the problem.

I am simply passing a value into a public static int:

namespace ModNote
{

public partial class homeScreen : Form
{

    public homeScreen()
    {
        InitializeComponent();
    }

    private void gamemodButton_Click(object sender, EventArgs e)
    {
        backgroundProgram.moduleNumber = 1;
        this.Hide();
        moduleScreen showForm = new moduleScreen();
        showForm.Show();         
    }

and this is where this variable is initialized

namespace ModNote
{
#region // Setting up Variables
public class backgroundProgram
{
    public static int moduleNumber;     
}
#endregion

}

and here a picture of the error: http://puu.sh/opETJ/fb8152d164.png

Thankyou.

edit: initializing the string array causes this error, any problems with this array being initialized? (moduleArray)

namespace ModNote
{
#region // Setting up Variables
public class backgroundProgram
{
    public static int moduleNumber;
    public static string[] noteArray;
    public static string[] moduleArray = new string[7] 
    { File.ReadAllText(@"ModulesFile\CGP1005M.txt"),
        File.ReadAllText(@"ModulesFile\CMP1005M.txt"),
        File.ReadAllText(@"ModulesFile\CMP1123M.txt"),
        File.ReadAllText(@"ModulesFile\CMP1124M.txt"),
        File.ReadAllText(@"ModulesFile\CMP1125M.txt"),
        File.ReadAllText(@"ModulesFile\CMP1127M.txt"),
        File.ReadAllText(@"ModulesFile\CMP1129M.txt")
    };

}
#endregion

}

4
  • That exception usually indicates an exception in a static initializer or constructor. Since you show neither I assume the actual problem lies somewhere else. Commented Apr 20, 2016 at 20:01
  • I have just realised it does not work when i initialize my array Commented Apr 20, 2016 at 20:05
  • If you can't figure it out you can edit this question and vote to reopen it) or create a new one. Commented Apr 20, 2016 at 20:07
  • i have edited the question but do not know how to reopen it as it says i cannot vote on my own post Commented Apr 20, 2016 at 20:07

1 Answer 1

0

If the exception is throw here:

public static string[] moduleArray = new string[7] 
{ File.ReadAllText(@"ModulesFile\CGP1005M.txt"),
    File.ReadAllText(@"ModulesFile\CMP1005M.txt"),
    File.ReadAllText(@"ModulesFile\CMP1123M.txt"),
    File.ReadAllText(@"ModulesFile\CMP1124M.txt"),
    File.ReadAllText(@"ModulesFile\CMP1125M.txt"),
    File.ReadAllText(@"ModulesFile\CMP1127M.txt"),
    File.ReadAllText(@"ModulesFile\CMP1129M.txt")
};

Then one of those lines is throwing an exception. There are all sorts of reasons for an exception to be thrown when reading from a file - security, not found, in use, etc.

I would suggest moving that logic to the static constructor so you can debug it to find the immediate problem, then add better error handling.

Another option is to not read all of that data in the static constructor and instead create an Initialize method or something. Exceptions in static constructors are difficult to handle in general.

Sign up to request clarification or add additional context in comments.

2 Comments

I looked around at using Initialize and dont quite understand it. How would I go about creating a string array, that reads in 7 text files to each location in the array? Ive been stuck on this for an hour now trying to figure it out.
There's no magical Initialize method; I'm just saying create a static method that initializes the data instead of doing it in the constructor. You'd call that method from your main program on startup. Syntactically there's nothing wrong with what you have, but something is causeing a runtime exception. You'd need to debug or add error handling to know exactly what the problem is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.