i'm working on a school project and I got this error: NullReferenceException was unhandled by user code
Code:
private MazeGen mazeGen;
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
object[] args = e.Argument as object[];
int value = (int)args[0];
bool solving = (bool)args[1];
if (!solving)
{
this.mazeGen.generateMaze(this.box.Width / value,
(this.box.Height - value) / value);
}
else
{
this.mazeGen.solve();
this.hasSolution = true;
}
this.box.Invalidate();
}
So the error occurs in this part: "this.mazeGen.generateMaze(this.box.Width / value,
(this.box.Height - value) / value);"
The object mazeGen is nog an object. I already checked everything and the class mazeGen is also public so I really don't understand why the program can't find mazeGen. Also I checked if value, width and height had a value and they did.
Maybe it's a stupid solution but I really have no idea what it could be.
Simply, you are not initializing the mazeGen, so it's null.
private MazeGen mazeGen = new MazeGen();
Related
So I'm basically trying to create a search button.
This search is using REGEX.
I think I have it correct but it's not working, Can someone tell me how / where i've gone wrong, Not coded in AGES...
public void SearchFunction(string searchtext)
{
SupporterId();
ReferenceNumber();
ConsignmentNumber();
}
private static void SupporterId()
{
const string sId= "";
var supporterId = Regex.IsMatch(sId, #"^[A-F,S,R][0-9]{3,6}$", RegexOptions.IgnoreCase);
}
private static void ReferenceNumber()
{
const string refNumber = "";
var referenceNumber = Regex.IsMatch(refNumber, #"^[ABN158][0-9]{6,17}$", RegexOptions.IgnoreCase);
}
private static void ConsignmentNumber()
{
const string conNumber = "";
var consignmentNumber = Regex.IsMatch(conNumber, #"&[0-9]{14}$", RegexOptions.IgnoreCase);
}
}
}
Those are my Regex, And this is my code behind..
protected void CheckStateClick(object sender, EventArgs e)
{
ConsignmentSearch();
}
private void ConsignmentSearch()
{
var searchclass = new RegexMethods();
searchclass.SearchFunction(txtReferenceNumber.Text);
}
Can anyone tell me where I have gone wrong and HOW I can fix it, Please don't tell me oh your missing this an then don't tell me how to fix it.
IF you can tell me how / what needs adding to be fixed example: add this line of code here .... < >
Please and thank you.
__
THIS IS THE ERROR
Test 'M:DeliveryInputSystem.Default.AddBox_Click(System.Object,System.EventArgs)' failed: Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
Default.aspx.cs(113,0): at DeliveryInputSystem.Default.AddBox_Click(Object sender, EventArgs e)
I may be wrong, but it looks like you are only checking empty strings...
How about checking your searchtext like this:
public void SearchFunction(string searchtext)
{
SupporterId(searchtext);
ReferenceNumber(searchtext);
ConsignmentNumber(searchtext);
}
private static void SupporterId(string sId)
{
var supporterId = Regex.IsMatch(sId, #"^[A-F,S,R][0-9]{3,6}$", RegexOptions.IgnoreCase);
}
private static void ReferenceNumber(string refNumber)
{
var referenceNumber = Regex.IsMatch(refNumber, #"^[ABN158][0-9]{6,17}$", RegexOptions.IgnoreCase);
}
private static void ConsignmentNumber(string conNumber)
{
var consignmentNumber = Regex.IsMatch(conNumber, #"&[0-9]{14}$", RegexOptions.IgnoreCase);
}
However, if I understand your code correctly, your searchtext variable only contains the txtReferenceNumber.Text text, so you should only run the ReferenceNumber(string searchtext) method on it.
You provided error text:
__ THIS IS THE ERROR Test'
M:DeliveryInputSystem.Default.AddBox_Click(System.Object,System.EventArgs)' failed:
Object reference not set to an instance of an object.
System.NullReferenceException:
Object reference not set to an instance of an object.
Default.aspx.cs(113,0): at DeliveryInputSystem.Default.AddBox_Click(Object sender, EventArgs e)
There is written cause of error: NullReferenceException and where does it occur Default.aspx.cs(113,0). You need to analyze what is there, in line 113 in Default.aspx.cs and why may it cause NullReferenceException.
If you don't know where to start, start with documentation. According to MSDN documentation for NullReferenceException class:
A NullReferenceException exception is thrown when you try to access a member on a type whose value is null.
You have often also an example there:
1: List<String> names;
2: if (sth) names = new List<String>();
3: names.Add("Major Major Major")
If sth is false then no instance is assigned to names and exception will be thrown.
I'm using the IntegerUpDown control from WPFExtendedToolkit
However, I'm unable to assign the event my function so that when the value is changed it will call my function. I'm pretty new to both c# and wpf so help is greatly appreciated. I've been trying to get it to work as show in a similar example here.
private IntegerUpDown m_argumentUpDown;
public IntArgumentOption(ArgumentOptionDescription argumentDesc) : base(argumentDesc)
{
m_argumentUpDown = new IntegerUpDown
{
Watermark = argumentDesc.m_watermark,
Increment = (int)argumentDesc.m_interval,
Minimum = (int)argumentDesc.m_minimum,
Maximum = (int)argumentDesc.m_maximum,
FormatString = "G",
SelectAllOnGotFocus = true,
MinWidth = 50,
FontSize = 10,
Margin = new System.Windows.Thickness(5, 0, 0, 0),
};
// COMPILER ERROR:
m_argumentUpDown.ValueChanged += new RoutedPropertyChangedEventHandler<int>(ArgumentChanged);
}
void ArgumentChanged(object sender, RoutedPropertyChangedEventArgs<int> e)
{
}
Doing this results in the compiler error:
error CS0029: Cannot implicitly convert type 'System.Windows.RoutedPropertyChangedEventHandler< int >' to 'System.Windows.RoutedPropertyChangedEventHandler< object >'
Here in the UpDownBase class (Xceed.wpfToolkit.dll) the method signature for ValueChanged is:
public event RoutedPropertyChangedEventHandler<object> ValueChanged;
hence in your code you have to declare a event handler where the generic is of type "Object" instead of int. Because of the mismatch in type the compiler is unable to implicitly convert to Int from object.
So change code as below
m_argumentUpDown.ValueChanged += new RoutedPropertyChangedEventHandler<object>(ArgumentChanged);
}
void ArgumentChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
//type caste e.newValue and e.OldValue
}
The following will work, I tested that. But I dont know if this is considered work around or creator of IntegerUpDown control meant it to be used this way.
m_argumentUpDown.ValueChanged += new RoutedPropertyChangedEventHandler<object>(ArgumentChanged);
//or you can change above line to following for brevity. ReSharper always suggesting me to do this
//m_argumentUpDown.ValueChanged += ArgumentChanged;
void ArgumentChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
//you need to cast New and Old value to int since both are of type object now
int newVal = (int)e.NewValue;
int oldVal = (int)e.OldValue;
}
I need to add selected items to this collection..
I select my item on a page and then appbar appears and i want on the appbar button tapped event to add item to collection that will be displayed on the other page..
private void Button_Tapped_1(object sender, TappedRoutedEventArgs e)
{
AllActors m = new AllActors();
ActorsObject objkt = itemGridView.SelectedItem;
m.allActors.Add(objkt);
}
this doesnt work... here are my classes:
public class AllActors : LivingDataCommon
{
public AllActors()
: base(String.Empty, String.Empty)
{
}
public AllActors(String ID, String title)
: base(ID, title)
{ }
private ObservableCollection<ActorsObject> _AllActors = new ObservableCollection<ActorsObject>();
public ObservableCollection<ActorsObject> allActors
{
get { return this._AllActors; }
}
}
Problem one: compiler error "Cannot implicitly convert type"
ActorsObject objkt = itemGridView.SelectedItem;
The GridView's SelectedItem property returns an object. You're trying to assign that to a variable of type ActorsObject, and the compiler can't assume that this is okay. You have to tell it...
ActorsObject objkt = (ActorsObject)itemGridView.SelectedItem;
The compiler's error message specifically asked "are you missing a cast?" and pointed to this line. That's useful information - it's just told you what was wrong and suggested how to fix it. Always read compiler errors and think about what they're telling you, don't just reduce it to "it doesn't work".
Problem two: "it doesn't do what I want"
private void Button_Tapped_1(object sender, TappedRoutedEventArgs e)
{
AllActors m = new AllActors();
ActorsObject objkt = itemGridView.SelectedItem;
m.allActors.Add(objkt);
}
Read this carefully and think about what it's doing. On the first line of the function, you're creating a new AllActors object and assign it to 'm'. On the third line, you make a change to that object. But then your function ends, and you've thrown that object away!
It's more likely that to achieve what you want you need to create the AllActors object as a field on your window class. Then you can keep the object around, bind to it, and any changes to it will be kept. Something like this, although you will also need some UI to display this data.
private AllActors _m = new AllActors();
private void Button_Tapped_1(object sender, TappedRoutedEventArgs e)
{
ActorsObject objkt = (ActorsObject)itemGridView.SelectedItem;
_m.allActors.Add(objkt);
}
You need to change
ActorsObject objkt = itemGridView.SelectedItem;
to
ActorsObject objkt = (ActorsObject)itemGridView.SelectedItem;
you are missing a cast there.
Try this:
ActorsObject objkt = (ActorsObject ) itemGridView.SelectedItem;
m.allActors.Add(objkt);
or
ActorsObject objkt = itemGridView.SelectedItem as ActorsObject;
m.allActors.Add(objkt);
You have to cast SelectedItem, since it is object not ActorsObject
I have this method and I have no idea what object sender is sending
void xTreve(object sender, Microsoft.SilverlightMediaFramework.Core.CustomEventArgs<Microsoft.SilverlightMediaFramework.Core.Media.PlaylistItem> e)
{
}
how do I check to see what object sender contains
I'm going to take it that your question is geared toward determining what sort of type sender is so you can work with it.
With that in mind, you would first need to get the type of sender, then you can cast it appropriately so you can operate on it.
For example, you can do the following in your method:
if (sender is TypeA)
{
var iAmA = (TypeA)sender;
// do something A-ish with sender
}
else if (sender is TypeB)
{
var iAmB = (TypeB)sender;
// do something B-ish with sender
}
else
{
// do something else
}
Alternatively, the following does the same as the preceding:
Type type = sender.GetType();
if (type == typeof(TypeA))
{
var iAmA = (TypeA)sender;
// do something A-ish with sender
}
else if (type == typeof(TypeB))
{
var iAmB = (TypeB)sender;
// do something B-ish with sender
}
else
{
// do something else
}
If you need to tell which instance sender is it depends if sender has some kind of property to identify it. Consider the following code:
public void randtest()
{
var rand = new Random();
var obj1 = new object();
var obj2 = new object();
if (rand.Next() % 2 == 1)
{
method(obj1);
}
else
{
method(obj2);
}
}
public void method(object thing)
{
//here i have no way to tell if thing is obj1 or obj2;
}
If the object is always going to be of the same type, but you just aren't sure what that type will be, then set a breakpoint inside the function and use the visual studio quickwatch window to inspect it. You will be able to see the control name and other properties of the sender object, as well as it's type. Once you know the type you know what to cast sender as in the code if you need to manipulate it.
Here's the C# code directly from the website (http://jobijoy.blogspot.com/2007/10/time-picker-user-control.html) that everyone refers to when someone asks about TimePicker for WPF, although I moved it around a little bit to be more organized. (Please note, if you're trying to run this code to work with it: you must change the XAML code on this site from KeyDown to PreviewKeyDown on the 3 Grids where Hours, Minutes, and Seconds displays live, and change the TextBlocks with each Grid to TextBoxes)
public partial class TimeControl : UserControl
{
public TimeControl()
{
InitializeComponent();
}
public TimeSpan Value
{
get { return (TimeSpan)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(TimeSpan), typeof(TimeControl),
new UIPropertyMetadata(DateTime.Now.TimeOfDay, new PropertyChangedCallback(OnValueChanged)));
public int Hours
{
get { return (int)GetValue(HoursProperty); }
set { SetValue(HoursProperty, value); }
}
public static readonly DependencyProperty HoursProperty =
DependencyProperty.Register("Hours", typeof(int), typeof(TimeControl),
new UIPropertyMetadata(0, new PropertyChangedCallback(OnTimeChanged)));
public int Minutes
{
get { return (int)GetValue(MinutesProperty); }
set { SetValue(MinutesProperty, value); }
}
public static readonly DependencyProperty MinutesProperty =
DependencyProperty.Register("Minutes", typeof(int), typeof(TimeControl),
new UIPropertyMetadata(0, new PropertyChangedCallback(OnTimeChanged)));
public int Seconds
{
get { return (int)GetValue(SecondsProperty); }
set { SetValue(SecondsProperty, value); }
}
public static readonly DependencyProperty SecondsProperty =
DependencyProperty.Register("Seconds", typeof(int), typeof(TimeControl),
new UIPropertyMetadata(0, new PropertyChangedCallback(OnTimeChanged)));
private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
TimeControl control = obj as TimeControl;
control.Hours = ((TimeSpan)e.NewValue).Hours;
control.Minutes = ((TimeSpan)e.NewValue).Minutes;
control.Seconds = ((TimeSpan)e.NewValue).Seconds;
}
private static void OnTimeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
TimeControl control = obj as TimeControl;
control.Value = new TimeSpan(control.Hours, control.Minutes, control.Seconds);
}
private void Down(object sender, KeyEventArgs args)
{
switch (((Grid)sender).Name)
{
case "sec":
if (args.Key == Key.Up)
this.Seconds++;
if (args.Key == Key.Down)
this.Seconds--;
break;
case "min":
if (args.Key == Key.Up)
this.Minutes++;
if (args.Key == Key.Down)
this.Minutes--;
break;
case "hour":
if (args.Key == Key.Up)
this.Hours++;
if (args.Key == Key.Down)
this.Hours--;
break;
}
}
}
I'm not very good with Dependency or Binding yet, I'm just learning it, that's why I can't figure it out. But here's the problem: When the Minutes or Seconds are taken beyond 59/-59 there is an infinite loop. I'll explain the flow of it (at least I'm learning that much here!):
Let's say the TimeControl object is at 0:59:00 and we press the up key while focused on the minute TextBox. So, as we follow the logic, it goes to the PreviewKeyDown event, and the switch statement takes us to this.Minutes++ which gets Minutes and sees 59, so sets minutes to 60.
This triggers OnTimeChanged for Minutes, which gets Hours (0) Minutes (60) Seconds (0) and sets Value to that. Since Value is a TimeSpan, it interprets this as 1:00:00, which is great.
So, once that is set, it tiggers OnValueChanged, which sets Hours to 1, and this immediately calls back to OnTimeChanged for Hours. At this point it gets Hours (1) Minutes (60) Seconds (0) and sets Value to that (which is interpreted as 2:00:00).
Now we have an infinite loop until Hours becomes too large and throws an exception. This is a little over my head to understand how to fix it. What would be the 'proper' fix? I know it could be fixed with if statements in the switch statement, or even the OnTimeChanged/OnValueChanged methods, but I'm sure there's a better way to do it with the dependencies.
Simple Fix: change it so it resets the minutes first, then update the hour.
// Disclaimer: Haven't read the code yet so i might be wrong
No need to set properties if they aren't different, try something like this:
private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
TimeControl control = obj as TimeControl;
var ts = (TimeSpan)e.NewValue;
if(ts.Hours != control.Hours) control.Hours = ts.Hours;
if(ts.Minutes != control.Minutes) control.Minutes = ts.Minutes;
if(ts.Seconds != control.Seconds) control.Seconds = ts.Seconds;
}
Normally I'd put this logic in the setters, something you see common with data access layers...but I think your dependency calls would still happen there, so best to do it in this event handler in your code.