- Home /
"NullReferenceExecption: Object reference not set to an instance of an object" error when using array of own made class objects.
So I want to do an interface in which the user could create his profiles, which would be saved in text file. Profile consists of name and keybinds. Everything seems to work if I use only a single class Profile object, but there should be ability to add more than one profile, so I want to have array of class Profile objects and I get the following error: "NullReferenceException: Object reference not set to an instance of an object Profiles.Update () (at Assets/Profiles.js:138)"
#pragma strict
import System.IO;
static var name1 : String = "Enter your name";
static var keybind : KeyCode;
static var keybind1 : KeyCode = KeyCode.W;
static var keybind2 : KeyCode = KeyCode.S;
static var keybind3 : KeyCode = KeyCode.A;
static var keybind4 : KeyCode = KeyCode.D;
static var keybind5 : KeyCode = KeyCode.U;
static var keybind6 : KeyCode = KeyCode.O;
static var n = 0; // ammount of profiles
var filePath = "C:/Users/Kompas/Desktop/gg.txt";
class Profile
{
private var name : String;
private var move_up : KeyCode = KeyCode.W;
private var move_down : KeyCode = KeyCode.S;
private var move_left : KeyCode = KeyCode.A;
private var move_right : KeyCode = KeyCode.D;
private var rotate_left : KeyCode = KeyCode.U;
private var rotate_right : KeyCode = KeyCode.O;
// Konstruktorius
function Profile()
{}
// Name
function GetName()
{
return name;
}
function SetName(name1 : String)
{
name = name1;
}
// Move up
function GetMoveUp()
{
return move_up;
}
function SetMoveUp(move_up1 : KeyCode)
{
move_up = move_up1;
}
// Move down
function GetMoveDown()
{
return move_down;
}
function SetMoveDown(move_down1 : KeyCode)
{
move_down = move_down1;
}
// Move left
function GetMoveLeft()
{
return move_left;
}
function SetMoveLeft(move_left1 : KeyCode)
{
move_left = move_left1;
}
// Move right
function GetMoveRight ()
{
return move_right;
}
function SetMoveRight(move_right1 : KeyCode)
{
move_right = move_right1;
}
// Rotate left
function GetRotateLeft()
{
return rotate_left;
}
function SetRotateLeft(rotate_left1 : KeyCode)
{
rotate_left = rotate_left1;
}
// Rotate right
function GetRotateRight()
{
return rotate_right;
}
function SetRotateRight(rotate_right1 : KeyCode)
{
rotate_right = rotate_right1;
}
}
static var profile1 : Profile[] = new Profile[10];
function Start () {
}
function Update () {
switch(GameManager.change_key)
{
case 1:
keybind1 = keybind;
break;
case 2:
keybind2 = keybind;
break;
case 3:
keybind3 = keybind;
break;
case 4:
keybind4 = keybind;
break;
case 5:
keybind5 = keybind;
break;
case 6:
keybind6 = keybind;
break;
}
if (GameManager.save_profile)
{
profile1[0].SetMoveUp(keybind1);
profile1[0].SetName(name1);
var sw : StreamWriter = new StreamWriter (filePath);
sw.WriteLine(profile1[0].GetMoveUp() + "" + profile1[1].GetName());
sw.Flush();
sw.Close();
GameManager.save_profile = false;
n++;
// if (profile1.move_up != "None");
// GameManager.change_key = 0;
}
}
Answer by robertbu · Mar 23, 2014 at 08:44 PM
You create the array, but that array is just an array of null pointers. You also have to create an instance of the class Profile for any entry in the array you want to use. Given your structure, I'm not sure where the best place to insert it is. You could insert between line 127 and 128:
if (profile1[0] == null)
provile1[0] = new Profile();
Damm, I thought that solution will be something simple, but didn't even imagine it would be so simple. Huge thanks, pal.