Error using Projection. Trying to dereference null pointer

I have been using root version 5.34 for many years now.
Today I had to switch to version 6.22 and I am trying to use the same program that I used with root v5.34. However I am getting an error. Could you point out how to fix the error?

The error when I call the function is:
Error in TRint::HandleTermInput(): cling::InvalidDerefException caught: Trying to dereference null pointer or trying to call routine taking non-null arguments

void make_proj(string title, string tree_name, string hist_name, Int_t proj_axis, Double_t b1, Double_t b2){
// Projection axis
// proj_axis=0: x axis
// proj_axis=1: y axis

	TFile *ftree = new TFile(tree_name.c_str(),"UPDATE");
	TH2I *h_2d = (TH2I*)ftree->Get(hist_name.c_str());
	cout << proj_axis << "\n";
	if(proj_axis==0){
		TAxis *yaxis = h_2d->GetYaxis();
		TH1D *h1 = h_2d->ProjectionX(title.c_str(),yaxis->FindBin(b1),yaxis->FindBin(b2));
	}
	if(proj_axis==1){
		TAxis *xaxis = h_2d->GetXaxis();
		TH1D *h1 = h_2d->ProjectionY(title.c_str(),xaxis->FindBin(b1),xaxis->FindBin(b2));
	}

	h1->Write();

	ftree->Close();
	return;
}

Looking forward for your answer.

Thank you very much.


Please read tips for efficient and successful posting and posting code

_ROOT Version:_6.22
_Platform:_Ubuntu 20.04
Compiler: Not Provided


Hi @grovira,
and welcome to the ROOT forum!
ROOT 6 has a better interpreter that supports all of C++ and is able to catch more errors than cint did.

In this case, the interpreter is telling you that you are dereferencing a null pointer, which is undefined behavior. I.e. it seems your code has a bug.

You can add a few checks to the code to figure out which pointer is null. Most probably h1? That does not seem to be valid C++, you are defining h1 in the scope of the if conditionals but then using it outside of those scopes.

I hope this helps!
Enrico

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.