Jump to content
  • Advertisement
Sign in to follow this  
OglaHai

Problem with object reference

This topic is 4847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 <iostream> #include <fstream.h> #include <string> #include <memory.h> 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." <<endl; cout << "Therefore, we need some basic information to do so." <<endl; cout << "Please enter the maximum number for the x axis." <<endl; cin >> max_x_coordinate; cout << "Please enter the maximum number for the y axis." <<endl; cin >> max_y_coordinate; cout << "Please enter the maximum number for the z axis." <<endl; cout << "When considering how many planes (i.e. z = # of planes)" <<endl; cout << "to have, take into consideration the fact that x * y * z * size of hex class" <<endl; cout << "is limited by the amount of memory of your computer." <<endl; cin >> max_z_coordinate; cout << "Next, we need the pathway for the generic map to be stored." <<endl; cout << "Please enter in a format like C:\\Universe\\Maps\\Abaddon\\." <<endl; cout << "Please also note that the pathway is limited to 255 characters total and must NOT contain numbers." <<endl; cin >> pathway; cout << "Next, please enter the name of the map." <<endl; cin >> 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!!!!!!" <<endl; } cout << "pWorld pointer assigned to array of Hexes." <<endl; for(int counter = 0; counter < ((max_x_coordinate)*(max_y_coordinate)*(max_z_coordinate)); counter++) { //need to assign the correct coordinates to all the hexes //do so here //start at 1,1,1 and move through, x's first, until the max x is reached //then increase y by 1 and reset x and repeat //once max y is reached, increase z by one and repeat all over again cout << "Setting x coordinate..." <<endl; pWorld[counter].Set_x_coordinate(x+1); cout << "Setting y coordinate..." <<endl; pWorld[counter].Set_y_coordinate(y+1); cout << "Setting z coordinate..." <<endl; pWorld[counter].Set_z_coordinate(z+1); cout << "Incrementing x coordinate..." <<endl; x++; if(((y+1) == max_y_coordinate) && ((x+1) == max_x_coordinate)) { cout << "Going to next plane in map..." <<endl; x = 0; y = 0; z++; } if((x+1) == max_x_coordinate) { cout << "Going to next column in map..." <<endl; x = 0; y++; } //need to verify this works correctly! } cout << "Correct coordinates assigned to all entries of the Map array..." <<endl; cout << "Outputting map name to file..." <<endl; fout << map_name; cout << "Outputting max x coordinate to file..." <<endl; fout << max_x_coordinate; cout << "Outputting max y coordinate to file..." <<endl; fout << max_y_coordinate; cout << "Outputting max z coordinate to file..." <<endl; fout << max_z_coordinate; cout << "Calculating point counter..." <<endl; pointer_counter = ((max_x_coordinate * max_y_coordinate * max_z_coordinate) - 1); cout << "The point counter is: " << pointer_counter <<endl; //how do I return the pointer to the 0th entry of the array??? //have to pWorld-- ((max x * max y * max z) - 3)) times to return to zeroth entry //switched to -1, due to fact that array is as big as max size - 1 since it starts at 0 //cout << "Returning pWorld pointer to zeroth entry..." <<endl; //while(pointer_counter != 0) //{ // pWorld--; //} //the above should return pWorld to pointing to the 0th entry of the array //needs to be verified though cout << "Outputting all information to file sequentially..." <<endl; for(int counter = 0; counter < ((max_x_coordinate)*(max_y_coordinate)*(max_z_coordinate)); counter++) { //cycle through array and output the information to the file cout << "Outputting name..." <<endl; fout << pWorld[counter].Fetch_hex_name(); cout << "Outputting x coordinate..." <<endl; fout << pWorld[counter].Fetch_x_coordinate(); cout << "Outputting y coordinate..." <<endl; fout << pWorld[counter].Fetch_y_coordinate(); cout << "Outputting z coordinate..." <<endl; fout << pWorld[counter].Fetch_z_coordinate(); cout << "Outputting hex type..." <<endl; fout << pWorld[counter].Fetch_hex_type(); unsigned short int choice1 = 0; cout << "Outputting popuation amount..." <<endl; fout << pWorld[counter].Fetch_population_amount(choice1); choice1 = 1; fout << pWorld[counter].Fetch_population_amount(choice1); cout << "Outputting birth rate..." <<endl; fout << pWorld[counter].Fetch_birth_rate(); cout << "Outputting death rate..." <<endl; fout << pWorld[counter].Fetch_death_rate(); cout << "Outputting wages..." <<endl; fout << pWorld[counter].Fetch_wages(); cout << "Outputting work amount..." <<endl; fout << pWorld[counter].Fetch_work_amount(); cout << "Outputting tax amount..." <<endl; fout << pWorld[counter].Fetch_tax_amount(); cout << "Outputting entertainment amount..." <<endl; fout << pWorld[counter].Fetch_entertainment_amount(); cout << "Outputting total funds avaliable..." <<endl; fout << pWorld[counter].Fetch_total_funds_avaliable(); cout << "Outputting for sale..." <<endl; for(unsigned short int counter1 = 0; counter1 < 50; counter1++) { unsigned short int choice2 = 1; pWorld[counter].Fetch_for_sale(choice2,counter1); choice2 = 2; pWorld[counter].Fetch_for_sale(choice2,counter1); choice2 = 3; pWorld[counter].Fetch_for_sale(choice2,counter1); choice2 = 4; pWorld[counter].Fetch_for_sale(choice2,counter1); } cout << " Outputting wanted..." <<endl; for(unsigned short int counter2 = 0; counter2 < 50; counter2++) { unsigned short int choice3 = 1; pWorld[counter].Fetch_wanted(choice3,counter2); choice3 = 2; pWorld[counter].Fetch_wanted(choice3,counter2); choice3 = 3; pWorld[counter].Fetch_wanted(choice3,counter2); choice3 = 4; pWorld[counter].Fetch_wanted(choice3,counter2); } cout << "Outputting recruits..." <<endl; for(unsigned short int counter3 = 0; counter3 < 50; counter3++) { unsigned short int choice4 = 1; pWorld[counter].Fetch_recruit(choice4,counter3); choice4 = 2; pWorld[counter].Fetch_recruit(choice4,counter3); choice4 = 3; pWorld[counter].Fetch_recruit(choice4,counter3); } cout << "Outputting movement..." <<endl; for(unsigned short int counter4 = 0; counter4 < 50; counter4++) { unsigned short int choice5 = 1; pWorld[counter].Fetch_movement(choice5,counter4); choice5 = 2; pWorld[counter].Fetch_movement(choice5,counter4); } //cout << "Incrementing pWorld pointer..." <<endl; //pWorld++; } //cout << "Deleting Map array..." <<endl; //delete [] Map; cout << "Freeing memory allocated to map..." <<endl; free( pWorld ); fout.close(); cout << "Generic Map Completed." <<endl; return; } void main() { //variable list unsigned short int menu_choice; // stores the choice for the menu //first, output the menu cout << "Universe PBEM Wargame Moderator Software" <<endl; cout << "Version .1 Alpha" <<endl; cout << "1) Create Generic Map" <<endl; cout << "2) Edit Map" <<endl; cin >> menu_choice; if (menu_choice == 1) { create_generic_map(); } if (menu_choice == 2) { //edit_map(); } return; }

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Just going out on a limb here it looks like this line is the problem:
pWorld[counter].Set_x_coordinate(x+1);

Given the kind of error you have, this means that pWorld[counter] is null.

I am not all that familiar with calloc but the line pWorld = (Hex *)calloc(size, 25320158 ); probably just allocates the space for your array rather than creating valid references in it.

So I think you need to add a line to create a new object at the start of your loop so it becomes:

for(int counter = 0; counter <

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Looks like my reply got mangled. The start of the loop becomes:


for(int counter = 0; counter < ((max_x_coordinate)*(max_y_coordinate)*(max_z_coordinate)); counter++)
{
//need to assign the correct coordinates to all the hexes
//do so here
//start at 1,1,1 and move through, x's first, until the max x is reached
//then increase y by 1 and reset x and repeat
//once max y is reached, increase z by one and repeat all over again

pWorld[counter] = new Hex(); // <-- new line here

cout << "Setting x coordinate..." <<endl;
pWorld[counter].Set_x_coordinate(x+1);
// etc...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!