Expect matcher for reference object with GoogleMock in C++ - c++

I'm attempting to use mocks to test that a certain method was called with certain arguments and am having trouble testing that *this is one of the arguments.
Here's the code under test:
class Component
{
virtual void update(Entity &entity, const uint32_t dt) {};
...
void Entity::update(const uint32_t dt)
{
for (unsigned int i = 0; i < components_.size(); ++i) {
components_[i]->update(*this, dt);
}
}
And the test:
ea_ = new aronnax::Entity(cla_);
EXPECT_CALL(mockComponent, update(Eq(ByRef(ea_)), testDt)).Times(1);
ea_->update(testDt);
I'm getting errors that the two types aren't comparable. I'm relatively new to c++ and am having trouble understanding how to compare the pointer ea_ to the pointer reference (*&) to ea_ passed into update.

Here's a complete working example. Note that the component method update must be declared with one of the MOCK_METHOD* macros. To check if an argument is a reference to some type, you use the Ref matcher. To bind to the reference with a pointer type, you must dereference the pointer as seen in the example.
Also, its important to understand that you must set the expectations on the object that is actually going to be called. I can't tell if your mock component is in the components container or not in your entity class, but I demonstrate the problems with not setting expectations on the correct object in the commented out portion in the test.
#include <stdint.h>
#include <gmock/gmock.h>
class Entity;
class Component
{
public:
MOCK_METHOD2(update, void(Entity&, uint32_t));
};
class Entity
{
public:
Entity(Component* c) : c_( c )
{
}
void update(uint32_t dt)
{
c_->update(*this, dt);
}
private:
Component* c_;
};
using ::testing::Ref;
TEST(MyTest, Component)
{
Component mockComponent;
Entity* ea = new Entity( &mockComponent );
EXPECT_CALL(mockComponent, update(Ref(*ea), 5));
ea->update( 5 );
// Component mockComponent2;
// Entity* ea2 = new Entity( &mockComponent );
// EXPECT_CALL(mockComponent2, update(Ref(*ea2), 5));
// ea2->update( 5 );
}
You may not need this, but it should allow you to quickly build the example if you have cmake.
cmake_minimum_required(VERSION 3.4)
include(CTest)
include(ExternalProject)
# Add googletest
ExternalProject_Add( googletest
GIT_REPOSITORY https://github.com/google/googletest.git
CMAKE_ARGS = "-Dgtest_disable_pthreads=1"
# Don't run update
UPDATE_COMMAND ""
# Disable install step
INSTALL_COMMAND ""
# BUILD_BYPRODUCTS googletest-prefix/src/googletest-stamp/googletest-gitinfo.txt
# BUILD_BYPRODUCTS googletest-prefix/tmp/googletest-cfgcmd.txt
BUILD_BYPRODUCTS "googletest-prefix/src/googletest-build/googlemock/libgmock_main.a"
)
# Get include dirs for googletest framework
ExternalProject_Get_Property(googletest source_dir)
set(GTEST_INCLUDE_DIRS
${source_dir}/googlemock/include
${source_dir}/googletest/include
)
# Create library target for gmock main, which is used to create
# test executables
ExternalProject_Get_Property(googletest binary_dir)
set(GTEST_LIBRARY_PATH ${binary_dir}/googlemock/libgmock_main.a)
set(GTEST_LIBRARY gmock_main)
add_library(${GTEST_LIBRARY} UNKNOWN IMPORTED)
set_property(TARGET ${GTEST_LIBRARY} PROPERTY IMPORTED_LOCATION ${GTEST_LIBRARY_PATH})
add_dependencies(${GTEST_LIBRARY} googletest)
add_executable(mytest main.cpp)
add_dependencies(mytest googletest)
target_include_directories(mytest BEFORE PRIVATE ${GTEST_INCLUDE_DIRS})
target_link_libraries(mytest gmock_main)
add_test(NAME mytest COMMAND mytest)
If you run the example, it should give you the following output.
$ ./mytest.exe
Running main() from gmock_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from MyTest
[ RUN ] MyTest.Component
[ OK ] MyTest.Component (0 ms)
[----------] 1 test from MyTest (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[ PASSED ] 1 test.

Hope this helps you :
instead of:
ea_ = new aronnax::Entity(cla_);
EXPECT_CALL(mockComponent, update(Eq(ByRef(ea_)), testDt)).Times(1);
ea_->update(testDt);
you should convert you pointer to a reference (note * inside ByRef):
ea_ = new aronnax::Entity(cla_);
EXPECT_CALL(mockComponent, update(Eq(ByRef(*ea_)), testDt)).Times(1);
ea_->update(testDt);
or simply define the object on the stack and pass it as is (it will be passed as a reference by the compiler):
aronnax::Entity ea_(cla_); // NOT a pointer any more
EXPECT_CALL(mockComponent, update(Eq(ByRef(ea_)), testDt)).Times(1);<br>
ea_->update(testDt);

Related

Call Expect_Call outside of Test_F

I want to make several gtests which all have to have a connect in the beginning, and a disconnect in the end. The things that are tested in between vary. They look as such:
TEST_F(..., ...)
{
// Connect - always the same
EXPECT_CALL(...);
ASSERT_TRUE(...);
// different things happen
EXPECT_CALL(...);
EXPECT_CALL(...);
ASSERT_TRUE(...);
// Disconnect - always the same
EXPECT_CALL(...);
ASSERT_TRUE(...);
}
Hence, I would like to have something like this:
void connect()
{
EXPECT_CALL(...);
ASSERT_TRUE(...);
}
void disconnect()
{
EXPECT_CALL(...);
ASSERT_TRUE(...);
}
TEST_F(..., ...)
{
connect();
// different things happen
EXPECT_CALL(...);
EXPECT_CALL(...);
ASSERT_TRUE(...);
disconnect();
}
Is this possible? Are there smarter ways to solve this problem?
The other answer provides example how it's usually done - you'd likely put connect() in test fixture constructor and disconnect() in test fixture destructor. You may want to read this section of documentation to learn more.
To answer the question "Is this possible?" with some documentation (from Advanced googletest Topics):
You can use assertions in any C++ function. In particular, it doesn't have to be a method of the test fixture class. The one constraint is that assertions that generate a fatal failure (FAIL* and ASSERT_*) can only be used in void-returning functions.
Here is a full example for setting up the mock object in the test fixture, which is instantiated once for each TEST_F:
#include <gmock/gmock.h>
#include <gtest/gtest.h>
class Object {
public:
void someMethod();
};
class MockObject : public Object {
public:
MOCK_METHOD(void, someMethod, (), ());
};
class TestFixture : public testing::Test {
public:
MockObject object;
TestFixture() {
std::cout << "Creating fixture." << std::endl;
EXPECT_CALL(object, someMethod()).Times(1);
}
~TestFixture() { std::cout << "Destroying fixture." << std::endl; }
};
TEST_F(TestFixture, SomeTest1) {
std::cout << "Performing test 1." << std::endl;
object.someMethod();
}
TEST_F(TestFixture, SomeTest2) {
std::cout << "Performing test 2." << std::endl;
object.someMethod();
}
The example can be compiled with:
g++ $(pkg-config --cflags --libs gtest gtest_main gmock) test.cpp
The output would look like this:
$ ./a.out
Running main() from /build/gtest/src/googletest-release-1.10.0/googletest/src/gtest_main.cc
[==========] Running 2 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 2 tests from TestFixture
[ RUN ] TestFixture.SomeTest1
Creating fixture.
Performing test 1.
Destroying fixture.
[ OK ] TestFixture.SomeTest1 (0 ms)
[ RUN ] TestFixture.SomeTest2
Creating fixture.
Performing test 2.
Destroying fixture.
[ OK ] TestFixture.SomeTest2 (0 ms)
[----------] 2 tests from TestFixture (0 ms total)
[----------] Global test environment tear-down
[==========] 2 tests from 1 test suite ran. (0 ms total)
[ PASSED ] 2 tests.

Why is EXPECT_CALL causing a read access violation GMock?

EDIT: I'm not the only one having this issue, maybe it's VS https://github.com/google/googletest/issues/2628
I'm having trouble getting GMock setup correctly. I'm using Windows and CMake, and adding GMock to my existing project.
The code that I'm trying to run is:
#include <gtest/gtest.h>
#include <gmock/gmock.h>
using ::testing::AnyNumber;
namespace ifac
{
class Foo
{
public:
virtual ~Foo() = default;
virtual void Bar(int i) = 0;
};
class FooMock : public Foo
{
public:
MOCK_METHOD1(Bar, void(int));
};
TEST(FooTest, BarCalled)
{
FooMock m;
EXPECT_CALL(m, Bar(1)).Times(AnyNumber());
m.Bar(1);
}
}
When it runs I get an error saying:
Exception thrown: read access violation. this was 0xFFFFFFFFFFFFFFFB.
The error is in the void Mutex::ThreadSafeLazyInit() of gtest-port.cc.
This seems to only happen if I set CMAKE_CXX_STANDARD to 17 too. I'm not really sure where to start with this, since I don't know a lot about C++, CMake or gtest/gmock.
Here's a stripped down version of the CMakeLists.txt files I have.
cmake_minimum_required (VERSION 3.8)
project ("TestingDemo")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
enable_testing()
add_subdirectory("googletest")
add_executable(Tests
"Main.cpp"
"Tests.cpp")
target_link_libraries(Tests PUBLIC gtest gtest_main gmock gmock_main)
add_test(RunTests Tests)

C++ / CMake : Making a plugin system for many 'source plugins'

I am working on a C++ library consisting of many plugins, which can be included independently of each other. The set of plugins is only dependent on the users requirements at compile time.
The plugins are source code only, they are not standalone binaries.
For this purpose, the main (and only) CMakeLists.txt of the library has a predefined plugins list, and every plugin found in a plugins directory is added to the binary target.
In addition, a preprocessor #define with the name of the plugin is set:
set (plugins
plugin1
plugin2
plugin3
...)
#optional plugins
foreach(library ${plugins})
file(TO_CMAKE_PATH "plugins/${library}" librarypath)
get_filename_component(librarypath ${librarypath} ABSOLUTE)
if(EXISTS ${librarypath})
message("--> found ${library}")
include_directories(${librarypath}/include)
file(GLOB libsources "${librarypath}/src/*.cpp" "${librarypath}/src/*.f90")
set(sources ${sources} ${libsources})
string(TOUPPER ${library} LIBRARY)
add_definitions(-D${LIBRARY})
endif()
endforeach(library)
Now in my main library, what I basically do is the following:
#ifdef PLUGIN1
# include "plugin1.h"
#endif
#ifdef PLUGIN2
# include "plugin2.h"
#endif
#ifdef PLUGIN3
# include "plugin3.h"
#endif
...
// each plugin has a unique id:
enum PluginID : int {
Plugin1 = 1,
Plugin2 = 2,
Plugin3 = 3,
};
// the name of each plugin is associated with its ID,
PluginID getPluginIDFromName( const std::string& PluginName )
{
static std::map<std::string, PluginID> PluginIDMap = {
{"PLUGIN1", Plugin1},
{"PLUGIN2", Plugin2},
{"PLUGIN3", Plugin3},
};
return PluginIDMap[PluginName];
}
// Load a plugin by its ID
PluginBaseClass* pluginFactory( PluginID pluginID)
{
switch ( pluginID ) {
#ifdef PLUGIN1
case Plugin1: { return new class Plugin1();}
#endif
#ifdef PLUGIN2
case Plugin2: { return new class Plugin2();}
#endif
#ifdef PLUGIN3
case Plugin3: { return new class Plugin3();}
#endif
}}
So the result is that in the main source I can load the plugin via:
PluginBaseClass* thePlugin1 = pluginFactory ( getPluginIDFromName ("PLUGIN1") );
Everything works as intended, but I feel that what I do is some kind of abusing cmake and preprocessor macros. Is there any better way to achieve my goals?
In addition, manually updating the map and switch for each possible plugin is rather cumbersome.
The requirement I have is that the user should not need to modify CMakeLists.txt manually. Thank you in advance!
Edit: I want to make the plugins available both via their IDs or their names, hence the two functions. In addition, static linking is preferred; I see no reason for dynamic loading.
Instead of manually creating the mapping from id to plugin name and the factory function by hand you can use what is known as "self-registration" and have the compiler do most of the work for you.
A static plugin factory
First, we need a factory class where the individual plugins can register themselves. The declaration might look something like this:
class PluginFactory
{
public:
using PluginCreationFunctionT = PluginBaseClass(*)();
PluginFactory() = delete;
static bool Register(std::string_view plugin name,
PluginID id,
PluginCreationFunctionT creation_function);
static PluginBaseClass* Create(std::string_view name);
static PluginBaseClass* Create(PluginID id);
private:
static std::map<std::string_view, PluginCreationFunctionT> s_CreationFunctionByName;
static std::map<PluginID, PluginCreationFunctionT> s_CreationFunctionById;
};
The corresponding source file then contains
std::map<std::string_view, PluginFactory::PluginCreationFunctionT>
PluginFactory::s_CreationFunctionByName;
std::map<PluginID, PluginFactory::PluginCreationFunctionT>
PluginFactory::s_CreationFunctionById;
bool PluginFactory::Register(std::string_view const plugin name,
PluginId const id,
PluginCreationFunctionT const creation_function)
{
// assert that no two plugins accidentally try to register
// with the same name or id
assert(s_CreationFunctionByName.find(name) == s_CreationFunctionByName.end());
assert(s_CreationFunctionById.find(id) == s_CreationFunctionById.end());
s_CreateFunctionByName.insert(name, creation_function);
s_CreateFunctionById.insert(id, creation_function);
return true;
}
PluginBaseClass* PluginFactory::Create(std::string_view const name)
{
auto const it = s_CreationFunctionByName.find(name);
return it != s_CreationFunctionByName.end() ? it->second() : nullptr;
}
PluginBaseClass* PluginFactory::Create(std::string_view const id)
{
auto const it = s_CreationFunctionById.find(name);
return it != s_CreationFunctionById.end() ? it->second() : nullptr;
}
Note that Register always returns true - we'll need it to return a value in order to use the Register function as an initializer for a global variable. Being an initializer of a global variable causes the compiler to emit code to call the Register function during program startup.
In your main function you can now obtain an instance of a particular plugin via
PluginBaseClass* thePlugin1 = PluginFactory::Create("PLUGIN1");
or via
PluginBaseClass* thePlugin1 = PluginFactory::Create(PluginID::Plugin1);
Modifying the plugins
The plugins themselves now need to be modified so that they register themselves. In theory any global variable would to, but to avoid name clashes between different plugins it is easiest to just add a static data member to each plugin class, e.g.
class Plugin1 : public PluginBaseClass {
public:
...
private:
static bool s_IsRegistered;
};
and then add the following to the source file for the plugin:
namespace {
PluginBaseClass* create_plugin1()
{
return new Plugin1{};
}
}
bool Plugin1::s_IsRegistered
= PluginFactory::Register("PLUGIN1", PluginID::Plugin1, create_plugin1);
Simplifying the CMake code
Now that the compiler generates the mapping you no longer need the preprocessor definitions. All that your CMake-code now needs to do is add the correct include directories and sources. But that doesn't need to be part of the main CMake file. Instead, you can put a CMakeLists.txt into each of the plugin folders and then include them either via add_subdirectory or include into the main CMakefile:
foreach(library ${plugins})
if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/plugins/${library}/CMakeLists.txt)
message(STATUS "--> found ${library}"
include(${CMAKE_CURRENT_LIST_DIR}/plugins/${library}/CMakeLists.txt)
else()
message(FATAL "Unknown plugin ${library} requested!")
endif()
endforeach()
The CMakeLists.txt for plugin1 in the plugins/plugin1 folder then contains just
include_directories(${CMAKE_CURRENT_LIST_DIR}/include)
file(GLOB sources_plugin1 "${CMAKE_CURRENT_LIST_DIR}/src/*.cpp" "${CMAKE_CURRENT_LIST_DIR}/src/*.f90")
list(APPEND sources ${sources_plugin1})
It might not look like much of an improvement in this particular case, but having these separate CMakeLists.txt files now also allows conditionally adding dependencies.
For example, assume that Plugin2 is the only plugin that uses boost. With the separate CMakeLists.txt you can add everything you need to find and use boost to the CMakeLists.txt of Plugin2 without polluting the main CMakeLists.txt file.
read plugin list from external file. read here. Basically move
set (plugins
plugin1
plugin2
plugin3
...)
into plugins.cmake file, and use
include(plugins.cmake) in your main cmake file.
In order to avoid the need to manually modify the map and switch: if it is OK to assume e.g. max number of plugins is 64, you may use bit masks in the following manner:
auto plugin_mask = PLUGINS;
auto id = 1;
while (plugin_mask)
{
if (plugin_mask & 1)
{
// found plugin
// add to map
std::pair<std::string, PluginID> p;
p.first = "PLUGIN" + std::to_string(id);
p.second = static_cast<PluginID>(id++);
PluginIDMap.insert(p);
} plugin_mask >> 1;
}

Odd Behavior When Using ON_CALL before EXPECT_CALL with Gmock

Has anyone ever seen seen odd behavior in gmock when following an ON_CALL statement with an EXPECT_CALL statement? For me, the EXPECT_CALL statement in the following code doesn't work (it doesn't actually enforce the Times part):
ON_CALL(myMockObject, myMockMethod()).WillByDefault(Return("hello mock")));
EXPECT_CALL(myMockObject, myMockMethod()).Times(99999);
myMockObject.myMockMethod();
Other solutions that I've tried:
Overriding the myMockMethod from the super class and have it simply return a string literal. The problem with this is that I can't determine how many times it's been called later on.
Skipping the ON_CALL part in favor of something like this:
EXPECT_CALL(myMockObject, myMockMethod())
.Times(1)
.WillRepeatedly(Return("hello mock"));
This results in a compilation error.
Also of note, the string literal I'm using in this example is custom in reality and something that gmock won't be able to come up with a default for (such as bool).
You have some other error in your original code which is not being mentioned in your question. The code provided in the question behaves as you expected if you construct a minimal self-contained example.
For example, the following code:
#include <string>
#include "gmock/gmock.h"
using ::testing::Return;
struct MyClass {
virtual ~MyClass() {}
virtual std::string myMockMethod() = 0;
};
struct MyMockClass : MyClass {
MOCK_METHOD0(myMockMethod, std::string());
};
TEST(MyClass, Fails) {
MyMockClass myMockObject;
ON_CALL(myMockObject, myMockMethod()).WillByDefault(Return("hello mock"));
EXPECT_CALL(myMockObject, myMockMethod()).Times(99999);
myMockObject.myMockMethod();
}
TEST(MyClass, Passes) {
MyMockClass myMockObject;
EXPECT_CALL(myMockObject, myMockMethod()).Times(1).WillRepeatedly(Return("hello mock"));
myMockObject.myMockMethod();
}
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
produces the following (expected) output:
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[==========] 2 tests from MyClass
[ RUN ] MyClass.Fails
..\src\main.cc(18): error: Actual function call count doesn't match EXPECT_CALL(myMockObject, myMockMethod())...
Expected: to be called 99999 times
Actual: called once - unsatisfied and active
[ FAILED ] MyClass.Fails (0 ms)
[ RUN ] MyClass.Passes
[ OK ] MyClass.Passes (0 ms)
[----------] 2 tests from MyClass (2 ms total)
[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (2 ms total)
[ PASSED ] 1 test.
[ FAILED ] 1 test, listed below:
[ FAILED ] MyClass.Fails
1 FAILED TEST
If you want your mock object to be held in a test fixture, you can do:
class MyClassTest : public testing::Test {
protected:
MyMockClass myMockObject_;
};
TEST_F(MyClassTest, Fails) {
ON_CALL(myMockObject_, myMockMethod()).WillByDefault(Return("hello mock"));
EXPECT_CALL(myMockObject_, myMockMethod()).Times(99999);
myMockObject_.myMockMethod();
}
Mock::VerifyAndClearExpectations(&myMockObject);
This did the trick. I'm still not sure how expectations are managed behind the scenes, but this got me working.
Any further explanation of this would be greatly appreciated, though.

SEH exception when using googlemock

I am starting to use googlemock with googletest but am getting an SEH exception that I can't figure out.
The error message is:
unknown file: error: SEH exception with code 0xc0000005 thrown in the test body.
I have read some similar questions on SO and elsewhere but am yet to find an answer for such a simple example.
i.e. This is happening on my real code, but I've also reproduced the error on the very simple example below. I am building with MSVC2008.
code that reproduces the error:
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include <iostream>
using testing::Exactly;
class Production
{
public:
virtual ~Production() {};
virtual void fn() = 0;
};
class ProductionCode : public Production
{
public:
virtual ~ProductionCode() {};
void fn()
{
std::cout << "CALLED ProductionCode::fn" << std::endl;
}
};
class MockProduction : public Production
{
public:
virtual ~MockProduction() {};
MOCK_METHOD0(fn, void());
};
class ProductionUser
{
public:
void methodUnderTest(Production *p)
{
p->fn();
}
};
TEST(ProductionTest, CallTheProductionFunction) {
ProductionCode p;
ASSERT_NO_THROW( p.fn() );
}
TEST(ProductionTest, CallTheMethodUnderTest) {
Production* p = new ProductionCode;
ProductionUser u;
ASSERT_NO_THROW( u.methodUnderTest(p) );
delete p;
}
TEST(ProductionTest, CallTheMethodUnderTestWithMock) {
MockProduction m;
EXPECT_CALL(m, fn())
.Times(Exactly(1));
ProductionUser u;
ASSERT_NO_THROW(u.methodUnderTest(&m));
}
my test output from the console:
[==========] Running 3 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 3 tests from ProductionTest
[ RUN ] ProductionTest.CallTheProductionFunction
CALLED ProductionCode::fn
[ OK ] ProductionTest.CallTheProductionFunction (4 ms)
[ RUN ] ProductionTest.CallTheMethodUnderTest
CALLED ProductionCode::fn
[ OK ] ProductionTest.CallTheMethodUnderTest (2 ms)
[ RUN ] ProductionTest.CallTheMethodUnderTestWithMock
unknown file: error: SEH exception with code 0xc0000005 thrown in the test body.
[ FAILED ] ProductionTest.CallTheMethodUnderTestWithMock (0 ms)
[----------] 3 tests from ProductionTest (10 ms total)
[----------] Global test environment tear-down
[==========] 3 tests from 1 test case ran. (13 ms total)
[ PASSED ] 2 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] ProductionTest.CallTheMethodUnderTestWithMock
1 FAILED TEST
.\simple.cpp(59): ERROR: this mock object (used in test ProductionTest.CallTheMe
thodUnderTestWithMock) should be deleted but never is. Its address is #000000000
014F800.
ERROR: 1 leaked mock object found at program exit.
Press any key to continue . . .
I am using my own main function as follows:
#include "gtest/gtest.h"
#include "gmock/gmock.h"
int main(int argc, char** argv) {
// The following line must be executed to initialize Google Mock
// (and Google Test) before running the tests.
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
I am guessing that I am making a pretty basic mistake here, Can anyone see where I am going wrong?
Thanks!
[Original Edited to make code & console output match]
I think you could force gtest to don't cloak the exact exception (what might be done using the code:
::testing::GTEST_FLAG(catch_exceptions) = false;
or the same from the command line)
And if then you use a debugger, you easily get the stack. Or even if you don't, I expect *nix-like OS to write core file
I met the same problem when I compiled the gmock as DLL and linked it in another project.
After a lot of try, I found the reason is:
You have to compile the gmock and your project in the same configuration!
That means you have to compile the gmock in DEBUG(RELEASE) configuration, if you want to link it in the DEBUG(RELEASE) mode. If not, the
unknown file: error: SEH exception with code 0xc0000005 thrown in the test body.
always occurs.
I hope my experience could help you, though you may encounter this problem in a different scene.
I was getting this error because I was dereferencing a null pointer.