Original Post
Hi, I am trying to write code that will produce a generic set of values for each Hex of a hex map. The only problem is, when I try to run the program, I get this error message: Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.at Hex.Set_x_coordinate(Hex* , UInt16 x). It references line 444 of the source code, but I can't tell why it is giving me this error. (Captain obvious here aren't I?) Any help would be appreciate, below is the source code (sorry, I have no clue how to do one of those handy boxes for it). Thanks in advance, Kenneth // Universe.cpp : Defines the entry point for the application. // #include #include #include #include class Hex { public: Hex(); ~Hex(){}; void Set_hex_name(char name[50]){ strcpy(hex_name,name); }; char Fetch_hex_name(){ return hex_name[50]; }; void Set_x_coordinate(unsigned short int x){ x_coordinate = x; }; unsigned short int Fetch_x_coordinate(){ return x_coordinate; }; void Set_y_coordinate(unsigned short int y){ y_coordinate = y; }; unsigned short int Fetch_y_coordinate(){ return y_coordinate; }; void Set_z_coordinate(unsigned short int z){ z_coordinate = z; }; unsigned short int Fetch_z_coordinate(){ return z_coordinate; }; void Set_hex_type(unsigned short int type){ hex_type = type; }; unsigned short int Fetch_hex_type(){ return hex_type; }; void Set_population_amount(unsigned short int pop_amount[2]){ population_amount[0] = pop_amount[0]; population_amount[1] = pop_amount[1]; }; unsigned short int Fetch_population_amount(unsigned short int choice){ return population_amount[choice]; }; // choice can either be 0 or 1, 0 returns population amount, 1 returns population type void Set_birth_rate(unsigned short int brate){ birth_rate = brate; }; unsigned short int Fetch_birth_rate(){ return birth_rate; }; void Set_death_rate(unsigned short int drate){ death_rate = drate; }; unsigned short int Fetch_death_rate(){ return death_rate; }; void Set_wages(unsigned int wage_amount){ wages = wage_amount; }; unsigned short int Fetch_wages(){ return wages; }; void Set_work_amount(unsigned int work){ work_amount = work; }; unsigned short int Fetch_work_amount(){ return work_amount; }; void Set_tax_amount(unsigned int tax){ tax_amount = tax; }; unsigned short int Fetch_tax_amount(){ return tax_amount; }; void Set_entertainment_amount(unsigned int entertainment){ entertainment_amount = entertainment; }; unsigned short int Fetch_entertainment_amount(){ return entertainment_amount; }; void Set_total_funds_avaliable(){ total_funds_avaliable = wages + tax_amount + entertainment_amount; }; unsigned short int Fetch_total_funds_avaliable(){ return total_funds_avaliable; }; //look below for these functions void Set_for_sale(unsigned short int choice, unsigned short int sale[2][2][2][2]); unsigned short int Determine_for_sale_first_empty_spot(); unsigned short int Fetch_for_sale(unsigned short int choice, unsigned short int number); void Set_wanted(unsigned short int choice, unsigned short int buy[2][2][2][2]); unsigned short int Determine_wanted_first_empty_spot(); unsigned short int Fetch_wanted(unsigned short int choice, unsigned short int number); void Set_recruit(unsigned short int choice, unsigned short int rec[2][2][2]); unsigned short int Determine_recruit_first_empty_spot(); unsigned short int Fetch_recruit(unsigned short int choice, unsigned short int number); void Set_movement(unsigned short int choice, unsigned short int move[2][2]); unsigned short int Fetch_movement(unsigned short int choice, unsigned short int number); private: char hex_name[50]; // name of the hex unsigned short int x_coordinate; // x_coordinate of hex unsigned short int y_coordinate; // y_coordinate of hex unsigned short int z_coordinate; // z_coordinate of hex unsigned short int hex_type; // type of hex unsigned short int population_amount[2]; // 0 = population of the hex, 1 = type unsigned short int birth_rate; // birth rate of the hex unsigned short int death_rate; // death rate of the hex unsigned int wages; // wages of the hex unsigned int work_amount; // amount of work money avaliable in the hex unsigned int tax_amount; // amount of tax money avaliable in the hex unsigned int entertainment_amount; // amount of entertainment money avaliable in the hex unsigned int total_funds_avaliable; // sum of previous three amounts unsigned short int for_sale[50][50][50][50]; // items sold in the hex, from left to right, each [50] corresponds to item #, amount, price, and seller (0 for the hex, otherwise the unit number) unsigned short int wanted[50][50][50][50]; // same as for for_sell, but with items wanted unsigned short int recruit[20][20][20]; // creatures open for recruit in the hex, goes type, amount, price unsigned short int movement[6][6]; // where in the 6 adjoining hexes can we move? 1 = can move to, 0 = none; next [] is the days to move between the two }; Hex::Hex() { strcpy ("Void",hex_name); x_coordinate = 0; y_coordinate = 0; z_coordinate = 0; hex_type = 12; population_amount[0] = 0; population_amount[0] = 17; birth_rate = 0; death_rate = 0; wages = 0; work_amount = 0; tax_amount = 0; entertainment_amount = 0; total_funds_avaliable = work_amount + tax_amount + entertainment_amount; for(int counter = 0; counter < 50; counter++) { for_sale[counter][0][0][0] = 0; for_sale[0][counter][0][0] = 0; for_sale[0][0][counter][0] = 0; for_sale[0][0][0][counter] = 0; } for(int counter = 0; counter < 50; counter++) { wanted[counter][0][0][0] = 0; wanted[0][counter][0][0] = 0; wanted[0][0][counter][0] = 0; wanted[0][0][0][counter] = 0; } for(int counter = 0; counter < 20; counter++) { recruit[counter][0][0] = 0; recruit[0][counter][0] = 0; recruit[0][0][counter] = 0; } for(int counter = 0; counter < 6; counter++) { movement[counter][0] = 1; movement[0][counter] = 1; } } void Hex::Set_for_sale(unsigned short int choice,unsigned short sale[2][2][2][2]) { /* sale properties sale[0][1][1][1] -> item number sale[1][0][1][1] -> amount sale[1][1][0][1] -> price sale[1][1][1][0] -> seller */ for_sale[choice][0][0][0] = sale[0][1][1][1]; for_sale[0][choice][0][0] = sale[1][0][1][1]; for_sale[0][0][choice][0] = sale[1][1][0][1]; for_sale[0][0][0][choice] = sale[1][1][1][0]; } unsigned short int Hex::Determine_for_sale_first_empty_spot() { //variable list unsigned short int a = 0; //return position of the first empty spot //if there is no empty spot, return 50 while(for_sale[a][0][0][0] != 0) { a++; if(a == 50) { break; } } return a; } unsigned short int Hex::Fetch_for_sale(unsigned short int choice, unsigned short int number) { /* choice signifies which one you want returned. i.e. 1 = item # 2 = amount 3 = price 4 = seller number signifies which of the 50 that you want the information about */ if (choice == 1) { return for_sale[number][0][0][0]; } if(choice == 2) { return for_sale[0][number][0][0]; } if(choice == 3) { return for_sale[0][0][number][0]; } if(choice == 4) { return for_sale[0][0][0][number]; } } void Hex::Set_wanted(unsigned short int choice,unsigned short buy[2][2][2][2]) { /* wanted properties buy[0][1][1][1] -> item number buy[1][0][1][1] -> amount buy[1][1][0][1] -> price buy[1][1][1][0] -> seller */ wanted[choice][0][0][0] = buy[0][1][1][1]; wanted[0][choice][0][0] = buy[1][0][1][1]; wanted[0][0][choice][0] = buy[1][1][0][1]; wanted[0][0][0][choice] = buy[1][1][1][0]; } unsigned short int Hex::Determine_wanted_first_empty_spot() { //variable list unsigned short int a = 0; //return position of the first empty spot //if there is no empty spot, return 50 while(wanted[a][0][0][0] != 0) { a++; if(a == 50) { break; } } return a; } unsigned short int Hex::Fetch_wanted(unsigned short int choice, unsigned short int number) { /* choice signifies which one you want returned. i.e. 1 = item # 2 = amount 3 = price 4 = seller number signifies which of the 50 that you want the information about */ if (choice == 1) { return wanted[number][0][0][0]; } if(choice == 2) { return wanted[0][number][0][0]; } if(choice == 3) { return wanted[0][0][number][0]; } if(choice == 4) { return wanted[0][0][0][number]; } } void Hex::Set_recruit(unsigned short int choice,unsigned short rec[2][2][2]) { /* rec properties rec[0][1][1] -> type rec[1][0][1] -> amount rec[1][1][0] -> price */ recruit[choice][0][0] = rec[0][1][1]; recruit[0][choice][0] = rec[1][0][1]; recruit[0][0][choice] = rec[1][1][0]; } unsigned short int Hex::Determine_recruit_first_empty_spot() { //variable list unsigned short int a = 0; // holds number of array that contains a 0 unsigned short int b = 0; // set to 1 when we find something that holds a 0 //return position of the first empty spot //if there is no empty spot, return 20 while(recruit[a][0][0] != 0) { a++; if(a == 20) { break; } } return a; } unsigned short int Hex::Fetch_recruit(unsigned short int choice, unsigned short int number) { /* choice signifies which one you want returned. i.e. 1 = type 2 = amount 3 = price number signifies which of the 20 that you want the information about */ if (choice == 1) { return recruit[number][0][0]; } if(choice == 2) { return recruit[0][number][0]; } if(choice == 3) { return recruit[0][0][number]; } } void Hex::Set_movement(unsigned short int choice,unsigned short move[2][2]) { /* move properties move[0][1] -> can we move to it? move[1][0] -> time to move between */ movement[choice][0] = move[0][1]; movement[0][choice] = move[1][0]; } unsigned short int Hex::Fetch_movement(unsigned short int choice, unsigned short int number) { /* choice signifies which one you want returned. i.e. 1 = can we move? 2 = time to move between number signifies which of the 6 that you want the information about */ if (choice == 1) { return movement[number][0]; } if(choice == 2) { return movement[0][number]; } } void create_generic_map() { //variable list unsigned short int max_x_coordinate; // holds max x value unsigned short int max_y_coordinate; // holds max y value unsigned short int max_z_coordinate; // holds max z value char pathway[255]; // pathway for the generic map file char map_name[255]; // name of the map unsigned short int x = 0; // arbitrary x counter unsigned short int y = 0; // arbitrary y counter unsigned short int z = 0; // arbitrary z counter int pointer_counter; // arbitrary pointer counter cout << "You have selected to generate a generic map." <> max_x_coordinate; cout << "Please enter the maximum number for the y axis." <> max_y_coordinate; cout << "Please enter the maximum number for the z axis." <> max_z_coordinate; cout << "Next, we need the pathway for the generic map to be stored." <> pathway; cout << "Next, please enter the name of the map." <> map_name; //I'm not sure the number 50 is needed and/or if it may cause errors strncat(pathway,map_name,50); strncat(pathway,".",1); strncat(pathway,"txt",3); cout << "The constructed pathway name is: "; cout << pathway << endl; ofstream fout(pathway); int size = (max_x_coordinate*max_y_coordinate*max_z_coordinate); cout << "The total hex size of the map is: "; cout << size << endl; Hex *pWorld; pWorld = (Hex *)calloc(size, 25320158 ); if(pWorld != NULL) { cout << "Not enough memory!!!!!!" <> menu_choice; if (menu_choice == 1) { create_generic_map(); } if (menu_choice == 2) { //edit_map(); } return; }