exception when using public static variables in public static class
After I made a public static class has some public static variables when I tried to use one of static variables VS had an exception The type initializer for 'EM_Image.staticvariables' threw an exception.
Why? and how I can solve it?
public static class StaticVariables
{
public static string image_source = "ahmed";
public static Bitmap b = new Bitmap(image_source);
public static int K_numcolors = 0;
public static int M_leastbits = 0;
public static BitmapImage bi = null;
public static Color[,] RGB_num = new Color[b.Width, b.Height]; // orginal colors
public static Color[,] new_RGB_byte = new Color[b.Width, b.Height]; // colors after compression 1
public static string[,,] RGB_Bits = new string[b.Width, b.Height, 3]; // original images
public static string[,,] new1_RGB_Bits = new string[b.Width, b.Height, 3]; // after compression 1
}
private void bt_B开发者_如何学运维rowse_Click(object sender, System.Windows.RoutedEventArgs e)
{
browse.ShowDialog();
direction_text.Text = browse.FileName;
staticvariables.image_source = browse.FileName;
ImageSource imageSource = new BitmapImage(new Uri(browse.FileName));
pic_origin.Source = imageSource;
}
Edit:
public static string image_source="ahmed" ;
public static Bitmap b=new Bitmap(image_source);
Looks like your default image_source
is creating a null
Bitmap - so the exception is thrown when the other static properties are initialized and try to access Bitmap b
- which is null:
public static Color[,] RGB_num = new Color[b.Width, b.Height];//orginal colors
Your current design doesn't really suit your needs - it looks like you need a singleton instance instead of a collection of static properties. Having said that, you can just initialize all variables with null
(all those Color
variables i.e.) and once you have valid input i.e. image_source
) you have to update/initialize them all.
The code that initializes your class (in the initializers for the fields or in a static constructor) threw an exception.
You can see the actual exception in the InnerException
property, or by telling the debugger to break whenever an exception is thrown in Debug, Exceptions.
I solved it by changing my static variables with assignments:
public static string image_source = "ahmed" ;
public static Bitmap b=new Bitmap(image_source);
Using properties:
public static string image_source { get; set; }
public static Bitmap b=new Bitmap { get; set; }
I did have to initialize the values at run time, but didn't see that as a problem.
Why
Whenever first time we use a static class, that class gets initialized. So when you set the static field
staticvariables.image_source
before the field is set, your class "staticvariables" gets initialized, while initializing it sets "image_source" to "ahmed".
In the next line the constructor of Bitmap class will throw, "ArgumentException" and that is why your "staticvariables" class will stop code execution that point itself, and @BrokenGlass the value of Bitmap b will not be null for other statements. The exception will stop the code execution.
During class initialization if any exception occurs, the "TypeInitializationException" will be created and the actual exception will be set as InnerException property, which in this case will be ArgumentException: "Parameter is not valid".
How to solve
By looking at your bt_Browse_Click, it seems you want user to select the image file. and may be at that time only you need to set other fields of the static class.
So below implementation of your "staticvariables" should give you an idea... Please note I have changed the class name to Pascal case.
public static class StaticVariables
{
public static string _image_source = "ahmed";
public static string image_source
{
get => _image_source;
set
{
if (!File.Exists(value))
{
throw new FileNotFoundException();
}
_image_source = value;
SetImageData();
}
}
public static Bitmap b = null;
public static int K_numcolors = 0;
public static int M_leastbits = 0;
public static BitmapImage bi = null;
public static Color[,] RGB_num = null;//orginal colors
public static Color[,] new_RGB_byte = null;// colors after compression 1
public static string[, ,] RGB_Bits = null;//original images
public static string[, ,] new1_RGB_Bits = null;//after compression 1
private static void SetImageData()
{
b = new Bitmap(_image_source);
RGB_num = new Color[b.Width, b.Height];//orginal colors
new_RGB_byte = new Color[b.Width, b.Height];// colors after compression 1
RGB_Bits = new string[b.Width, b.Height, 3];//original images
new1_RGB_Bits = new string[b.Width, b.Height, 3];//after compression 1
}
}
Regarding whether or not to use Singleton pattern, Jon Skeet has already given answer, about the difference between Singleton pattern implementation and static class. However for now you should go with simple things.
Hope it helps.
精彩评论