Why do i keep getting a null pointer exception when I call a pointer.

It is very odd to me. Regular variables do not give this much of a head ace. I am just testing some pointer code I made in the artist class.I know it starts out as null because i declare it null but i assigned values with the set functions so in theory it should not be doing that.Any help would be appreciated.

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
  artist.h

#pragma once
#include <string>
#include <iostream>

using namespace std;
//Artist Class Definition.
class Artist
{
public:
	//Defualt Constructer 
	Artist();
	// Getter Function.
	string getName();
	string getCountry();
	// Set Function
	void setName(string);
	void setCountry(string);
	~Artist();
	

private:
	string *firstName;
	string country;
};


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


Artist.cpp
#include <string>
#include <iostream>
#include <fstream>
#include "artist.h"

using namespace std;

//Default Constructer
Artist::Artist() {
	//Now a pointer.
	*firstName = "";
	country = "";
}

//Artist& Artist::operator=(const Artist& p) {
		//firstName = p.firstName;  
		//return *this;
	
//}//end operator=

//Deconstructer
Artist::~Artist() {
	delete firstName;
	firstName = NULL;
}
//Set & Get Functions
void Artist::setName(string f) {
	*firstName = f;
}

void Artist::setCountry(string c) {
	country = c;
}

string Artist::getName() {
	return *firstName;
}
string Artist::getCountry() {
	return country;
}


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
//****************************************************
// File: source.cpp
//
// Purpose: to get user input and call functions. 
//

//
// Compiler: Visual C++ 2013


#include <string>
#include <iostream>
#include <fstream>
#include "song.h"
#include "album.h"

int main() {
	//Testing Code.

	Artist theArt;

	Artist *s = NULL;

	s = new Artist;

	
	
	theArt.setName("name");
	s->setCountry("USA");
	s->getCountry();
	theArt.getName();


	
}
I didnt dig past the first issue I see.

the constructor for your class sets the firstname pointer but there is no memory for it (a 'new' statement) anywhere.

this should probably be

//Default Constructer
Artist::Artist() {
//Now a pointer.
*firstName = new string;
*firstName = "";

and your destructor needs to delete it.

if the point of this is to learn about pointers, carry on.
if the point is something else, there is no reason in the world to make a string * in your class. the guts of the string are a pointer already, so you don't gain anything for your aggravation.


Last edited on
Hi,

Even if this is an exercise in learning about pointers, don't use new to merely obtain a pointer. There is std::addressof or plain & (address operator).

As jonin pointed out std::string puts it's data on the heap.

Other things to consider:

Use an STL container, like std::vector, all it's data is on the heap too.

References were invented to use instead of raw pointers. They are similar, but a reference can't be made to refer to something else, unlike a pointer which can be made to point at anything, including nothing. Consider using nullptr instead of NULL

There are smart pointers such as std::unique_ptr which involves move semantics, and std::shared_ptr. This stuff might be a bit advanced right now, but at least you have heard of them.

All these suggestions are about doing proper C++ : not using raw pointers, and not doing ones own memory management.

Unfortunately there are many who still teach a mixture of C and C++, so we get C techniques wrapped up in C++ which is terrible.

Good Luck !!

Edit:

Rather than creating a new topic each time, please just continue with 1 topic about the same code. Multiple topics are a time waster for those who reply: the same things are said over and over. Just a friendly reminder :+)
Last edited on
Topic archived. No new replies allowed.