TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Kaung Htet Kyaw
NA
66
6.3k
Can someone else fix my problem? Please
Jul 31 2020 9:17 AM
My dearest friends,
Please I am facing the System.NullReferenceException in Program.cs. It says that "EventManager.NoArgumentInvoker.get returned null" how should I suppose to do? Please, can someone can else fix my exception. Any help will be very very appreciated. Please fix my error.
This is the code of Program.cs:
using
System;
using
System.Collections.Generic;
using
System.Diagnostics;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
UnityEngine;
namespace
ProgrammingAssignment3
{
/// <summary>
/// Programming Assignment 3
/// </summary>
class
Program
{
// test case to run
static
int
testCaseNumber;
// debug log messages
static
List<
string
> logMessages =
new
List<
string
>();
/// <summary>
/// Tests the Programming Assignment 3 classes
/// </summary>
/// <param name="args">command-line args</param>
static
void
Main(
string
[] args)
{
// listen for Unity debug log events
UnityEngine.Debug.LogEvent.AddListener(HandleLogEvent);
// create Invoker and Listener
Invoker invoker =
new
Invoker();
Listener listener =
new
Listener();
// Awake is always called before Start
invoker.Awake();
// loop while there's more input
string
input = Console.ReadLine();
while
(input[0] !=
'q'
)
{
// extract test case number from string
GetInputValueFromString(input);
// execute selected test case
switch
(testCaseNumber)
{
case
1:
// invoker added to event manager first for no argument event
InitializeTestCase(invoker, listener);
EventManager.NoArgumentInvoker.InvokeNoArgumentEvent();
if
(logMessages[0] ==
"MessageEvent"
&&
logMessages.Count == 1)
{
Console.WriteLine(
"Passed"
);
}
else
{
Console.WriteLine(
"FAILED"
);
}
break
;
case
2:
logMessages.Clear();
EventManager.ClearInvokersAndListeners();
// listener added to event manager first for no argument event
listener.Start();
invoker.Start();
EventManager.NoArgumentInvoker.InvokeNoArgumentEvent();
if
(logMessages[0] ==
"MessageEvent"
&&
logMessages.Count == 1)
{
Console.WriteLine(
"Passed"
);
}
else
{
Console.WriteLine(
"FAILED"
);
}
break
;
case
3:
// invoker added to event manager first for one argument event
InitializeTestCase(invoker, listener);
EventManager.IntArgumentInvoker.InvokeOneArgumentEvent(-1);
if
(logMessages[0] ==
"CountMessageEvent: -1"
&&
logMessages.Count == 1)
{
Console.WriteLine(
"Passed"
);
}
else
{
Console.WriteLine(
"FAILED"
);
}
break
;
case
4:
logMessages.Clear();
EventManager.ClearInvokersAndListeners();
// listener added to event manager first for one argument event
listener.Start();
invoker.Start();
EventManager.IntArgumentInvoker.InvokeOneArgumentEvent(-1);
if
(logMessages[0] ==
"CountMessageEvent: -1"
&&
logMessages.Count == 1)
{
Console.WriteLine(
"Passed"
);
}
else
{
Console.WriteLine(
"FAILED"
);
}
break
;
case
5:
InitializeTestCase(invoker, listener);
// invoke no argument event directly through invoker
invoker.InvokeNoArgumentEvent();
if
(logMessages[0] ==
"MessageEvent"
&&
logMessages.Count == 1)
{
Console.WriteLine(
"Passed"
);
}
else
{
Console.WriteLine(
"FAILED"
);
}
break
;
case
6:
InitializeTestCase(invoker, listener);
// invoke one argument event directly through invoker
invoker.InvokeOneArgumentEvent(-1);
if
(logMessages[0] ==
"CountMessageEvent: -1"
&&
logMessages.Count == 1)
{
Console.WriteLine(
"Passed"
);
}
else
{
Console.WriteLine(
"FAILED"
);
}
break
;
case
7:
InitializeTestCase(invoker, listener);
// invoke multiple no argument events directly through invoker
invoker.InvokeNoArgumentEvent();
invoker.InvokeNoArgumentEvent();
invoker.InvokeNoArgumentEvent();
if
(logMessages[0] ==
"MessageEvent"
&&
logMessages[1] ==
"MessageEvent"
&&
logMessages[2] ==
"MessageEvent"
&&
logMessages.Count == 3)
{
Console.WriteLine(
"Passed"
);
}
else
{
Console.WriteLine(
"FAILED"
);
}
break
;
case
8:
InitializeTestCase(invoker, listener);
// invoke multiple one argument events directly through invoker
invoker.InvokeOneArgumentEvent(1);
invoker.InvokeOneArgumentEvent(2);
invoker.InvokeOneArgumentEvent(3);
if
(logMessages[0] ==
"CountMessageEvent: 1"
&&
logMessages[1] ==
"CountMessageEvent: 2"
&&
logMessages[2] ==
"CountMessageEvent: 3"
&&
logMessages.Count == 3)
{
Console.WriteLine(
"Passed"
);
}
else
{
Console.WriteLine(
"FAILED"
);
}
break
;
case
9:
InitializeTestCase(invoker, listener);
// invoke combination of events directly through invoker
invoker.InvokeOneArgumentEvent(1);
invoker.InvokeNoArgumentEvent();
invoker.InvokeOneArgumentEvent(3);
if
(logMessages[0] ==
"CountMessageEvent: 1"
&&
logMessages[1] ==
"MessageEvent"
&&
logMessages[2] ==
"CountMessageEvent: 3"
&&
logMessages.Count == 3)
{
Console.WriteLine(
"Passed"
);
}
else
{
Console.WriteLine(
"FAILED"
);
}
break
;
case
10:
InitializeTestCase(invoker, listener);
// invoke multiple one argument events directly through invoker
invoker.InvokeNoArgumentEvent();
invoker.InvokeOneArgumentEvent(2);
invoker.InvokeNoArgumentEvent();
if
(logMessages[0] ==
"MessageEvent"
&&
logMessages[1] ==
"CountMessageEvent: 2"
&&
logMessages[2] ==
"MessageEvent"
&&
logMessages.Count == 3)
{
Console.WriteLine(
"Passed"
);
}
else
{
Console.WriteLine(
"FAILED"
);
}
break
;
}
input = Console.ReadLine();
}
}
/// <summary>
/// Extracts the test case number from the given input string
/// </summary>
/// <param name="input">input string</param>
static
void
GetInputValueFromString(
string
input)
{
testCaseNumber =
int
.Parse(input);
}
/// <summary>
/// Handles the log event from the Debug mock
/// </summary>
/// <param name="message">message to log</param>
static
void
HandleLogEvent(
string
message)
{
logMessages.Add(message);
}
/// <summary>
/// Initializes test case
/// </summary>
static
void
InitializeTestCase(Invoker invoker, Listener listener)
{
logMessages.Clear();
EventManager.ClearInvokersAndListeners();
invoker.Start();
listener.Start();
}
}
}
This is the CountMessageEvent.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
UnityEngine.Events;
namespace
ProgrammingAssignment3
{
public
class
CountMessageEvent : UnityEvent <
int
>
{
}
}
This is the listener.cs
using
System.Collections;
using
System.Collections.Generic;
using
UnityEngine;
/// <summary>
/// An event listener
/// </summary>
public
class
Listener : MonoBehaviour
{
/// <summary>
/// Use this for initialization
/// </summary>
public
void
Start()
{
// add your code here
Invoker invoker =
new
Invoker();
invoker.AddNoArgumentListener(HandleMessageEvent);
invoker.AddOneArgumentListener(HandleCountMessageEvent);
}
/// <summary>
/// Handles the no argument event
/// </summary>
void
HandleMessageEvent()
{
print(
"MessageEvent"
);
}
/// <summary>
/// Handles the one argument event
/// </summary>
/// <param name="number">number</number>
void
HandleCountMessageEvent(
int
number)
{
print(
"CountMessageEvent: "
+ number);
}
}
This is the Invoker.cs
using
ProgrammingAssignment3;
using
System.Collections;
using
System.Collections.Generic;
using
UnityEngine;
using
UnityEngine.Events;
/// <summary>
/// An event invoker
/// </summary>
public
class
Invoker : MonoBehaviour
{
// add your fields for your message event support here
Timer MessageTimer;
MessageEvent messageEvent;
// add your fields for your count message event support here
int
count;
Timer CountMessageTimer;
CountMessageEvent countMessageEvent =
new
CountMessageEvent();
/// <summary>
/// Awake is called before Start
/// </summary>
public
void
Awake()
{
// add your code here
messageEvent =
new
MessageEvent();
}
/// <summary>
/// Use this for initialization
/// </summary>
public
void
Start()
{
// add your code here
MessageTimer = gameObject.AddComponent<Timer>();
MessageTimer.Duration = 1;
MessageTimer.Run();
}
/// <summary>
/// Update is called once per frame
/// </summary>
void
Update()
{
// add your code here
if
(MessageTimer.Finished)
{
messageEvent.Invoke();
MessageTimer.Run();
}
}
/// <summary>
/// Adds the given listener to the no argument event
/// </summary>
/// <param name="listener">listener</param>
public
void
AddNoArgumentListener(UnityAction listener)
{
// add your code here to add the given listener for
// the message event
//MessageEvent messageEvent = new MessageEvent();
messageEvent =
new
MessageEvent();
messageEvent.AddListener(listener);
}
/// <summary>
/// Adds the given listener to the one argument event
/// </summary>
/// <param name="listener">listener</param>
public
void
AddOneArgumentListener(UnityAction<
int
> listener)
{
// add your code here to add the given listener for
// the count message event
countMessageEvent.AddListener(listener);
}
/// <summary>
/// Removes the given listener to the no argument event
/// </summary>
/// <param name="listener">listener</param>
public
void
RemoveNoArgumentListener(UnityAction listener)
{
// add your code here to remove the given listener from the
// message event
messageEvent.RemoveListener(listener);
}
/// <summary>
/// Removes the given listener to the one argument event
/// </summary>
/// <param name="listener">listener</param>
public
void
RemoveOneArgumentListener(UnityAction<
int
> listener)
{
// add your code here to remove the given listener from the
// count message event
countMessageEvent.RemoveListener(listener);
}
/// <summary>
/// Invokes the no argument event
///
/// NOTE: We need this public method for the autograder to work
/// </summary>
public
void
InvokeNoArgumentEvent()
{
// add your code here to invoke the message event
messageEvent.Invoke();
}
/// <summary>
/// Invokes the one argument event
///
/// NOTE: We need this public method for the autograder to work
/// </summary>
/// <param name="argument">argument to use for the Invoke</param>
public
void
InvokeOneArgumentEvent(
int
argument)
{
// add your code here to invoke the count message event
// with the given argument
countMessageEvent.Invoke(argument);
}
}
This is the EventManager.cs
using
System.Collections;
using
System.Collections.Generic;
using
UnityEngine;
using
UnityEngine.Events;
/// <summary>
/// The event manager
/// </summary>
public
static
class
EventManager
{
// DON'T change the fields, properties, or ClearInvokersAndListeners
// method; if you do, you'll break the autograder!
// All your work in this class is in the last 4 methods
#region Fields
// no argument event support
static
Invoker noArgumentInvoker;
static
UnityAction noArgumentListener;
// int argument event support
static
Invoker intArgumentInvoker;
static
UnityAction<
int
> intArgumentListener;
#endregion
#region Properties (for autograder use only)
/// <summary>
/// Gets the no argument invoker
/// </summary>
public
static
Invoker NoArgumentInvoker
{
get
{
return
noArgumentInvoker; }
}
/// <summary>
/// Gets the no argument listener
/// </summary>
public
static
UnityAction NoArgumentListener
{
get
{
return
noArgumentListener; }
}
/// <summary>
/// Gets the int argument invoker
/// </summary>
public
static
Invoker IntArgumentInvoker
{
get
{
return
intArgumentInvoker; }
}
/// <summary>
/// Gets the int argument listener
/// </summary>
public
static
UnityAction<
int
> IntArgumentListener
{
get
{
return
intArgumentListener; }
}
#endregion
#region Public methods
/// <summary>
/// Clears all the invokers and listeners from the event manager.
///
/// Note: We need this functionality so the autograder can start with
/// a "clean" event manager at the start of each test case
/// </summary>
public
static
void
ClearInvokersAndListeners()
{
// no argument invoker and listener
if
(noArgumentInvoker !=
null
)
{
if
(noArgumentListener !=
null
)
{
noArgumentInvoker.RemoveNoArgumentListener(noArgumentListener);
noArgumentListener =
null
;
}
noArgumentInvoker =
null
;
}
// int argument invoker and listener
if
(intArgumentInvoker !=
null
)
{
if
(intArgumentListener !=
null
)
{
intArgumentInvoker.RemoveOneArgumentListener(intArgumentListener);
intArgumentListener =
null
;
}
intArgumentInvoker =
null
;
}
}
/// <summary>
/// Adds the invoker for the no argument event
/// </summary>
/// <param name="invoker">invoker</param>
public
static
void
AddNoArgumentInvoker(Invoker invoker)
{
// add your code here
noArgumentInvoker = invoker;
}
/// <summary>
/// Adds the listener for the no argument event
/// </summary>
/// <param name="listener">listener</param>
public
static
void
AddNoArgumentListener(UnityAction listener)
{
// add your code here
listener= noArgumentListener;
}
/// <summary>
/// Adds the invoker for the int argument event
/// </summary>
/// <param name="invoker">invoker</param>
public
static
void
AddIntArgumentInvoker(Invoker invoker)
{
// add your code here
invoker= intArgumentInvoker;
}
/// <summary>
/// Adds the listener for the int argument event
/// </summary>
/// <param name="listener">listener</param>
public
static
void
AddIntArgumentListener(UnityAction<
int
> listener)
{
// add your code here
intArgumentListener = listener;
}
#endregion
}
This is the code of MessageEvent.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
UnityEngine.Events;
namespace
ProgrammingAssignment3
{
public
class
MessageEvent : UnityEvent
{
}
}
After implementing that, I faced this exception...
Please fix my error hurry. My work will be dued in Aug 3. Please help and fix my error and I will appreciate for your fix...
I also uploaded the zip file to see my full code...
Attachment:
3_Windows_Autograder_Programming_Assignment_3_Materials.zip
Reply
Answers (
6
)
Objects in Blender
What should I add what data in the Waypoint ?