Urgent: VS showing error null ptr but I cant find it

My visual studio won't let me run this program and I tried for a bit to find the error but am coming up empty handed. It give this error in a breakpoint when I run it

Exception thrown: read access violation.
**this** was nullptr.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  #include <iostream>
#include "Person.h"
#include <string>
#include <vector>

using namespace std;

//Initiating PersonData class
class PersonData {
private:
	// Varibles for the class. Person to temporarily store, person list array, and capacity and size
	Person tempPerson;
	Person* personList;
	int capacity, size;

public:
	// Constructor to set the person class + size and capacity
	PersonData(Person x) {
		this->capacity = 1;
		this->size = 0;
		this->tempPerson = x;
	}
	~PersonData() {
		delete[] personList;
		personList = nullptr;
	}
	void resizeArray() {
		int sizeOf = capacity;

		// double the capacity
		capacity += 1;

		// dynamically create new array
		Person* tempArray = new Person[size];

		// Copy contents of old array to new array
		// tempArray - pointer to new array, arrayPtr = pointer to old array
		for (int counter = 0; counter < size; counter++) {

			tempArray[counter] = personList[counter];

		}

		//Deallocate to prevent memory leaks
		delete[] personList;

		personList = tempArray;
	}

	void add(Person x) {

		int sizeOf = size;

		// Check if the array is the same size as it's capacitiy-- in other words, is the array full?
		// resizeCopy() function makes more room in the array.
		if (size >= capacity) {
			resizeArray();
		}

		//We have room to add the integer, so let's do it
		// add the integer num to the end of the array and increase size by 1

		personList[sizeOf] = x;

		size++;
	}


	void listPersons() {

		if (size > 0) {
			for (int i = 0; i < size; i++) {

				cout << "Name: " << personList[i].getName() << endl;
				cout << "Age: " << personList[i].getAge() << endl;
				cout << "Weight: " << personList[i].getWeight() << endl << endl;

				cout << "Capacity of the array: " << capacity << endl;
				cout << "Records in the array: " << size << endl;

			}
		}
		else {
			cout << "Capacity of the array: " << capacity << endl;
			cout << "Records in the array: " << size << endl;
		}
	}

};

int main()
{
	Person personOne("John", 22, 140.6);
	PersonData person1(personOne);
	
	person1.add(personOne);
	
	Person personTwo("Mike", 55, 147.6);

	person1.add(personTwo);

	person1.listPersons();
	
}

Person.h file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once
#define PERSON_H
#include <string>

using namespace std;

class Person
{
private:
	string name;
	int age;
	double weight;

public:
	Person(string, int, double);
	Person();
	void setName(string);
	void setAge(int);
	void setWeight(double);
	string getName();
	int getAge();
	double getWeight();
};


Person.cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "Person.h"
#include <string>


Person::Person(string x, int y, double z) {
	this->name = x;
	this->age = y;
	this->weight = z;
}
Person::Person() {
	this->name = "";
	this->age = 0;
	this->weight = 0.0;
}

void Person::setName(string nameOne) {
	this->name = nameOne;
}

void Person::setAge(int ageOne) {
	this->age = ageOne;
}

void Person::setWeight(double weightOne) {
	this->weight = weightOne;
}

string Person::getName() {
	return this->name;
}
int Person::getAge() {
	return this->age;
}

double Person::getWeight() {
	return this->weight;
}

Last edited on
As a single code unit (for easy testing), consider (not fully tested):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <string>
#include <iostream>

class Person
{
private:
	std::string name;
	int age {};
	double weight {};

public:
	Person(const std::string&, int, double);
	Person();
	void setName(const std::string&);
	void setAge(int);
	void setWeight(double);
	std::string getName() const;
	int getAge() const;
	double getWeight() const;
};

Person::Person() {}
Person::Person(const std::string& x, int y, double z) : name(x), age(y), weight(z) {}

void Person::setName(const std::string& nameOne) {
	name = nameOne;
}

void Person::setAge(int ageOne) {
	age = ageOne;
}

void Person::setWeight(double weightOne) {
	weight = weightOne;
}

std::string Person::getName() const {
	return name;
}

int Person::getAge() const {
	return age;
}

double Person::getWeight() const {
	return weight;
}

//Initiating PersonData class
class PersonData {
private:
	// Variables for the class. Person to temporarily store, person list array, and capacity and size
	Person *personList {};
	int capacity {}, size {};

public:
	// Constructor to set the person class + size and capacity
	PersonData(const Person& x) {
		add(x);
	}

	~PersonData() {
		delete[] personList;
	}

	void resizeArray() {
		// double the capacity
		capacity = (capacity == 0) ? 2 : capacity * 2;

		// dynamically create new array
		auto tempArray {new Person[capacity]};

		// Copy contents of old array to new array
		// tempArray - pointer to new array, arrayPtr = pointer to old array
		for (int counter = 0; counter < size; ++counter)
			tempArray[counter] = personList[counter];

		//Deallocate to prevent memory leaks
		delete[] personList;
		personList = tempArray;
	}

	void add(const Person& x) {
		// Check if the array is the same size as it's capacity-- in other words, is the array full?
		// resizeCopy() function makes more room in the array.
		if (size >= capacity)
			resizeArray();

		// We have room to add the integer, so let's do it
		// add the integer num to the end of the array and increase size by 1
		personList[size++] = x;
	}

	void listPersons() {
		for (int i = 0; i < size; ++i) {
			std::cout << "Name: " << personList[i].getName() << '\n';
			std::cout << "Age: " << personList[i].getAge() << '\n';
			std::cout << "Weight: " << personList[i].getWeight() << "\n\n";
		}

		std::cout << "Capacity of the array: " << capacity << '\n';
		std::cout << "Records in the array: " << size << '\n';
	}
};

int main()
{
	Person personOne("John", 22, 140.6);
	PersonData person1(personOne);
	//person1.listPersons();

	person1.add(personOne);

	Person personTwo("Mike", 55, 147.6);

	person1.add(personTwo);
	person1.listPersons();
}

Last edited on
Hello Mistafyer,

Not that it matters, but you did not mention which version of VS you are using.

I started by putting a break point on every line in main and found that the problem occurred at line person1.add(personOne);.

When I put some break points in the "person1.add" function it produced the run time error at line personList[sizeOf] = x;. Where I found that "personList" is a nullptr.

I have not gone any farther to figure out where "personList" would get its value.

At the beginning of the main file I had to do this to get it to compile:
1
2
3
4
5
#include <iostream>
#include <string>
#include <vector>

#include "Person.h" 

Sometimes order does make a difference. When it comes to your include I find this to work well.
seeplus once wrote:

any required #define statements
std:: headers (eg iostream)
c headers (eg conio.h)
windows headers (eg windows.h)
3rd party headers
user headers



In the header file I think you are trying to use an include guard, but went about it wrong. Try this:
1
2
3
4
5
6
7
8
9
10
#ifndef PERSON_H
#define PERSON_H

//#include <string>  // <--- Not needed here after rearranging the includes in the ".cpp" files.

//using namespace std;  // Never put in a header file.

// Class definition here.

#endif // !PERSON_H 


I will work on it some more, but since you are more familiar with the code you may fix the problem quicker.

Andy
Thanks Andy! I appreciate the help but the issue has been resolved. Thank you for taking the time though to teach me more about my improper usage of code it really cleared up a lot.
Handy Andy wrote:
//#include <string> // <--- Not needed here after rearranging the includes in the ".cpp" files.


This is really bad advice. Header files should include all the other header files needed to define what they use. Since Person.h uses std::string, it should include <string>. It should not have to rely on the CPP file including other header files.

In general, be wary of paying too much attention to Handy Andy. He frequently posts bad advice, or outright nonsense, and refuses to learn from the corrections others give him.
Topic archived. No new replies allowed.