why is my this pointer pointing to nullptr?

here is a picture of the actual error and exception throw error i'm getting.
https://cdn.discordapp.com/attachments/370147666673532940/376183527466729472/9.png
but essentially, i get an exception thrown right as i try to get my temperature
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

#ifndef weatherData_h
#define weatherData_h

#include <list>
#include "observer.h"
#include "subject.h"

class weatherData : public subject {
public:
    weatherData()
    : weatherData(0.0, 0.0, 0.0) {}
    weatherData(double temp, double humidity, double pressure)
    : observers_(), temp_(temp), humidity_(0.0), pressure_(pressure)  {}
    
    void registerObserver(observer* o) {
		observers_.push_front(o);
    }
    void removeObserver(observer* o) {
        auto it = find(observers_.begin(), observers_.end(), o);
        if (it != observers_.end()) {
            observers_.erase(it);
        }
    }
    void notifyObservers() const {
		for (auto o : observers_)
		{
			o->update();
		}
    }
    double temp() const {
		return temp_;                //here is where i get an error
    }
    double humidity() const {
		return humidity_;
    }
    double pressure() const {
		return pressure_;
    }
    
    void setMeasurements(double temp, double humidity, double pressure) {
		temp_ = temp;
		humidity_ = humidity;
		pressure_ = pressure;
        measurementsChanged();
    }
    void measurementsChanged() const { notifyObservers(); }
//-------------
private:
    std::list<observer*> observers_;
    double temp_;
    double humidity_;
    double pressure_;
};

#endif /* weatherData_h */


here is subject.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef subject_h
#define subject_h

#include "observer.h"


//----------------------------------------------------------------------
class subject {     
public:
    virtual void registerObserver(observer* o) = 0;    // Interface == *all* pure virtual methods
    virtual void removeObserver(observer* o) = 0;
    virtual void notifyObservers() const = 0;
};

#endif /* subject_h */ 


and here is observer.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef observer_h
#define observer_h


//----------------------------------------------------------------------
class observer {                
public:
    virtual void update() = 0;
};

#endif /* observer_h */ 
Last edited on
The error is saying that the this pointer is null. This can happen if you try to do something like
1
2
Foo *foo = nullptr;
foo->bar();


Post currentConditionsDisplay::update().
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
#ifndef currentConditionsDisplay_h
#define currentConditionsDisplay_h

#include <iostream>
#include <iomanip>
#include "observer.h"
#include "subject.h"
#include "weatherData copy.h"
#include "displayElement.h"


//----------------------------------------------------------------------
class currentConditionsDisplay : public observer, public DisplayElement {
public:
    currentConditionsDisplay(subject* wd) { wd->registerObserver(this); }
	virtual void update()
	{
		temp_ = toCent(dynamic_cast<weatherData*>(subject_)->temp());
		humidity_ = dynamic_cast<weatherData*>(subject_)->humidity();
		display();
	}

   
    virtual void display() { std::cout << *this; }
    
    friend std::ostream& operator<<(std::ostream& os,
                                    const currentConditionsDisplay& ccd) {
		return os << "CURRENTLY: " << ccd.temp_ << std::endl;
    }
    static double toCent(double fahr) { return (fahr - 32.0) * 5.0/9; }

//----------------
private:
    double temp_;       // all temps displayed in deg C
    double humidity_;
	
	subject* subject_;
};


#endif /* currentConditionsDisplay_h */ 
i get that if i make a pointer point to null, it will give me null. but i can't find in my code where the pointer points to null
(that's a lot of points)
Last edited on
dynamic_cast<T *>(p) evaluates to nullptr if the run time type of the object pointed to by p is not T:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class A{
};

class B : public A{
};

class C : public A{
};

int main(){
    A *p = new B;
    auto q = dynamic_cast<C *>(p);
    assert(q == nullptr);
}
okay, i think i understood that.. sorry i'm still a beginner in OOP, how would i resolve this problem?
You should either add a check to see if the casted pointer is null before trying to use it, or if the object should have been of the type you specified with dynamic_cast, you should investigate why you got an object of the wrong type. The debugger can tell you the type of the object pointed to by subject_. See where you're creating objects of that type.
hello!
sorry for the late update but it turned out that I didn't define my pointer within the constructor and thus my compiler automatically made it point to nullptr! I appreciate the help!
Registered users can post here. Sign in or register to post.