Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

null reference exception handle #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ sysinfo.txt
*.apk
*.unitypackage
build_windows.zip
.idea
/Assets/Plugins/Editor/JetBrains
/Assets/Plugins/Editor/JetBrains.meta
5 changes: 4 additions & 1 deletion Assets/Scripts/BulletScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ void Start()
void OnEnable ()
{
distance = 0.0f;
Pool.Instance.ActivateObject("rifleSoundEffect").SetActive(true);
//before active the activateObject, check the null reference situation
GameObject activateObject = Pool.Instance.ActivateObject("rifleSoundEffect");
if(activateObject != null)
activateObject.SetActive(true);
}

// Update is called once per frame
Expand Down
8 changes: 7 additions & 1 deletion Assets/Scripts/CannonScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ void Start ()
{
enemyTags = Enemies.Select(e => e.tag).ToList();
var enemy = EnemyManagerScript.Instance.GetEnemyInRange(transform.position, float.PositiveInfinity, enemyTags);
//the enemy might be null,if it is null ,return here
if(enemy == null)
return;
var angle = MathHelpers.Angle(enemy.transform.position - transform.position, transform.up);
transform.eulerAngles = new Vector3(0, 0, angle);

bulletPlaceholder = transform.Find("Rocket").gameObject;
//Rocket might be not found ,null reference check
Transform rocket = transform.Find("Rocket");
if(rocket != null)
bulletPlaceholder = rocket.gameObject;
}

void FixedUpdate ()
Expand Down
6 changes: 4 additions & 2 deletions Assets/Scripts/CoinScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ void OnMouseOver()
{
GameManager.Instance.CoinCollected(gameObject);
Pool.Instance.DeactivateObject(gameObject);

Pool.Instance.ActivateObject("coinSoundEffect").SetActive(true);
//null reference check
GameObject coinSoundEffect = Pool.Instance.ActivateObject("coinSoundEffect");
if (coinSoundEffect != null)
coinSoundEffect.SetActive(true);
}
}
7 changes: 5 additions & 2 deletions Assets/Scripts/HealthDrawerScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ public class HealthDrawerScript : MonoBehaviour

public void Draw(int lives)
{
//there is a index out of range exception ,so check it here
for(int i = 0; i < lives; i++)
{
Hearts[i].sprite = FullHeart;
if(Hearts != null && Hearts.Length > i)
Hearts[i].sprite = FullHeart;
}

for(int i = lives; i < Hearts.Length; i++)
{
Hearts[i].sprite = EmptyHeart;
if(Hearts != null && Hearts.Length > i)
Hearts[i].sprite = EmptyHeart;
}
}

Expand Down
36 changes: 25 additions & 11 deletions Assets/Scripts/PathFollower.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@ void OnEnable ()
{
switch(Type)
{
case PathType.Ground: path = GameObject.Find("GroundPath").GetComponent<PathCreator>().path; break;
case PathType.Air: path = GameObject.Find("AirPath").GetComponent<PathCreator>().path; break;
//the GroundPath and AirPath might be not found ,and cause a null reference exception
//so ,before use it ,check the null reference situation first
case PathType.Ground:
GameObject groundPath = GameObject.Find("GroundPath");
if(groundPath != null)
path = groundPath.GetComponent<PathCreator>().path;
break;
case PathType.Air:
GameObject airPath = GameObject.Find("AirPath");
if(airPath != null)
path = airPath.GetComponent<PathCreator>().path;
break;
}

segmentIndex = 0;
Expand Down Expand Up @@ -58,17 +68,21 @@ void FixedUpdate ()

private void RecomputeSegment()
{
var segment = path.GetPointsInSegment(segmentIndex);
//the path might be null reference ,so ensure it is not,and go to calculations
if (path != null)
{
var segment = path.GetPointsInSegment(segmentIndex);

A = segment[0] + PositionOffset;
B = segment[1] + PositionOffset;
C = segment[2] + PositionOffset;
D = segment[3] + PositionOffset;
A = segment[0] + PositionOffset;
B = segment[1] + PositionOffset;
C = segment[2] + PositionOffset;
D = segment[3] + PositionOffset;

v1 = -3 * A + 9 * B - 9 * C + 3 * D;
v2 = 6 * A - 12 * B + 6 * C;
v3 = -3 * A + 3 * B;
v1 = -3 * A + 9 * B - 9 * C + 3 * D;
v2 = 6 * A - 12 * B + 6 * C;
v3 = -3 * A + 3 * B;

t = 0;
t = 0;
}
}
}
5 changes: 4 additions & 1 deletion Assets/Scripts/Pool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ public void RegisterObject(GameObject prototype)

public GameObject ActivateObject(string tag)
{
//when run the game ,there are so many exceptions,so ,I remove the KeyNotFoundException
//and return null instead to avoid exception
//and i will handle the null situation at the caller outside this method
if (!pooledObjects.ContainsKey(tag))
throw new KeyNotFoundException();
return null;

var singlePool = pool[tag];

Expand Down
9 changes: 6 additions & 3 deletions Assets/Scripts/RocketScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public class RocketScript : FlyingShotScript
// Use this for initialization
void OnEnable ()
{
Pool.Instance.ActivateObject("missileSoundEffect").SetActive(true);

//ensure missileSoundEffect is not null reference
GameObject missileSoundEffect = Pool.Instance.ActivateObject("missileSoundEffect");
if(missileSoundEffect != null)
missileSoundEffect.SetActive(true);
shadow = transform.Find("Shadow");
shadow.position = transform.position;
acceleration = InitialAcceleration;
Expand All @@ -42,8 +44,9 @@ void FixedUpdate ()
{
Target = EnemyManagerScript.Instance.GetClosestEnemyInRange(transform.position, float.PositiveInfinity, EnemyTags);
if (Target == null) BlowUp();
//if the Target is null ,the direction which calculated by target will also be null,so return here
return;
}

var direction = Target.transform.position - transform.position;
var angle = MathHelpers.Angle(direction, transform.up) * Time.deltaTime * Inertia * Mathf.Pow(velocity.sqrMagnitude, 0.5f);

Expand Down
8 changes: 6 additions & 2 deletions Assets/Scripts/StartButtonScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ public class StartButtonScript : MonoBehaviour

private void Start()
{
LoadingScreen.SetActive(false);
//the lodingScreen is unsigned ,add null reference check here
if(LoadingScreen != null)
LoadingScreen.SetActive(false);
}

public void StartGame()
{
LoadingScreen.SetActive(true);
//the lodingScreen is unsigned ,add null reference check here
if(LoadingScreen != null)
LoadingScreen.SetActive(true);
GameManager.Lives = GameManager.MaxLives;
SceneManager.LoadScene("Level_01");
}
Expand Down