Ошибка System.NullReferenceException: "Ссылка на объект не указывает на экземпляр объекта."
Написал простенькую игру на C#, но при выполнении возникает эта ошибка, пожалуйста помогите решить. Ошибка возникает в самом конце кода ( enemies[i].Left)
//----------------------------Main Abrs---------------------
//int BLs = Bullets speed
//int BGs = BackGround Speed
//int PRs = Player Speed
//timer Lmot = Left Motion Timer
//timer Rmot = Right Motion Timer
//timer Umot = Up Motion Timer
//timer Dmot = Down Motion Timer
//PRb = Player Box
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WMPLib;
namespace WindowsFormsApp1
{
public partial class Fprm1 : Form
{
//Params
int PRs = 3;
int BGs = 2;
int BLs = 80;
int ENs = 3;
//adds
PictureBox[] bullets;
PictureBox[] enemies;
PictureBox[] cloud;
Random rnd;
WindowsMediaPlayer GameSong;
WindowsMediaPlayer Shoot;
public Fprm1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
for (int i = 0; i < cloud.Length; i++)
{
cloud[i].Left += BGs;
if (cloud[i].Left >= 1280)
{
cloud[i].Left = cloud[i].Height;
}
}
for (int i = cloud.Length; i < cloud.Length; i++)
{
cloud[i].Left += BGs - 10;
if (cloud[i].Left >= 1280)
{
cloud[i].Left = cloud[i].Left;
}
}
}
//Base Form1
private void Form1_Load(object sender, EventArgs e)
{//Bullets alg
bullets = new PictureBox[1];
for (int i = 0; i < bullets.Length; i++)
{
bullets[i] = new PictureBox();
bullets[i].BorderStyle = BorderStyle.None;
bullets[i].Size = new Size(20, 5);
bullets[i].BackColor = Color.White;
this.Controls.Add(bullets[i]);
}
//clouds alg
cloud = new PictureBox[20];
rnd = new Random();
for (int i = 0; i < cloud.Length; i++)
{
cloud[i] = new PictureBox();
cloud[i].BorderStyle = BorderStyle.None;
cloud[i].Location = new Point(rnd.Next(-1000, 1800), rnd.Next(50, 280));
if (i % 2 == 1)
{
cloud[i].Size = new Size(rnd.Next(100, 255), rnd.Next(30, 70));
cloud[i].BackColor = Color.FromArgb(rnd.Next(50, 120), 255, 160, 150);
}
else
{
cloud[i].Size = new Size(150, 25);
cloud[i].BackColor = Color.FromArgb(rnd.Next(50, 125), 255, 205, 205);
}
this.Controls.Add(cloud[i]);
}
//Sounds alg
Shoot = new WindowsMediaPlayer();
Shoot.URL = "sounds\\shoot.mp3";
Shoot.settings.volume = 100;
GameSong = new WindowsMediaPlayer();
GameSong.URL = "sounds\\GameSong.mp3";
GameSong.settings.setMode("loop", true);
GameSong.settings.volume = 5 ;
GameSong.controls.play();
}
//Moving Person
private void Lmo_Tick(object sender, EventArgs e)
{if (PRb.Left > 10){PRb.Left -= PRs;}}
private void Rmot_Tick(object sender, EventArgs e)
{if (PRb.Right > 10){PRb.Left += PRs;}}
private void Umot_Tick(object sender, EventArgs e)
{if (PRb.Top > 300) { PRb.Top -= PRs; }}
private void Dmot_Tick(object sender, EventArgs e)
{if (PRb.Top <600){PRb.Top += PRs;}}
//Keys
private void Fprm1_KeyDown(object sender, KeyEventArgs e)
{PRb.Image = Properties.Resources.TankGO;
if(e.KeyCode == Keys.Left){Lmot.Start();}
if(e.KeyCode == Keys.Right){Rmot.Start();}
if(e.KeyCode == Keys.Up){Umot.Start();}
if(e.KeyCode == Keys.Down){Dmot.Start();}
if(e.KeyCode == Keys.Space){
for (int i = 0; i < bullets.Length; i++){if (bullets[i].Left > 1280){bullets[i].Location = new Point(PRb.Location.X + 100 + i * 50, PRb.Location.Y + 50);}}
Shoot.controls.play();}
//Enemies
for (int i = 0; i < enemies.Length; i++)
{
Image EzEN = Image.FromFile("assets\\EzEnemies.png");
enemies = new PictureBox[4];
int ENsz = rnd.Next(50, 100);
enemies[i] = new PictureBox();
enemies[i].Size = new Size(ENsz, ENsz);
enemies[i].SizeMode = PictureBoxSizeMode.Zoom;
enemies[i].BackColor = Color.Transparent;
enemies[i].Image = EzEN;
enemies[i].Location = new Point((i + 1) * rnd.Next(90, 160) + 1080, rnd.Next(450, 600));
this.Controls.Add(enemies[i]);
}
}
private void Fprm1_KeyUp(object sender, KeyEventArgs e)
{
PRb.Image = Properties.Resources.TankST;
Lmot.Stop();
Rmot.Stop();
Umot.Stop();
Dmot.Stop();
}
private void Bmot_Tick(object sender, EventArgs e)
{
for (int i = 0; i < bullets.Length; i++)
{
bullets[i].Left += BLs;
}
}
private void Emot_Tick(object sender, EventArgs e)
{
MvEN(enemies, ENs);
}
private void MvEN(PictureBox[] enemies, int speed)
{
enemies = new PictureBox[4];
for (int i = 1; i < enemies.Length; i++)
{
enemies = new PictureBox[4];
enemies[i].Left -= ENs + (int)(Math.Sin(enemies[i].Left * Math.PI / 180) + Math.Cos(enemies[i].Left * Math.PI / 180));
}
}
}
}