I'm a beginner developer and I'm trying to make a 2D survival type game where you have to break a trees. I'm using a raycast to detect when you click a tree. When I run the program, it says "Object reference not set to an instance of an object". Usually you just have to declare a public variable, but all variables are set. Never seen this before. Here's what I have:
if (Input.GetMouseButtonDown(0))
{
Debug.DrawRay(transform.position, mousePosition * 0.1f, Color.red);
RaycastHit2D hit = Physics2D.Raycast(playerRb.transform.position, mousePosition, 0.1f);
if (hit.collider != null)
{
hit.transform.GetComponent<TreeScript>().treeHealth -= 1;
}
}
I've pinpointed the problem to be on the last line. I have a script called TreeScript and a variable there for the treeHealth.
I found what my problem was. I used "if (hit.collider != null)" and then attempted to modify a specific component. I believe the raycast was hitting other objects and failing to transform it because it didn't have the treeScript component. I just added an if statement to detect if the object I hit is a tree.
Related
I am creating the PDF documents in Qt using command:
QPdfWriter *pdf;
pdf = new QPdfWriter(filename);
Sometimes the object is not created due to a wrong filename or document exists and is open in other application...
How can I check if object was created (if the pdf pointer points to the valid object)?
EDIT: My mistake was, that object is not created. It is not true. The QPdfWriter object is created, but there is no exception or error generated.
I can set some properties.
The problem will occur later when I am trying to write something to the document - see comments of the last 3 commands:
pdf->setParent(this);
pdf->setPageSize(QPdfWriter::A4);
pdf->setPageOrientation(QPageLayout::Orientation::Landscape);
QPainter *p = new QPainter(pdf); //got message: QPainter::begin(): Returned false
ui->chartView->render(p); // no error
p->end(); // got message: QPainter::end: Painter not active, aborted
I read the documentation of QpdfWriter and parent classes QPagedPaintDevice and QPaintDevice. I cannot find any property/method to check if I can write to the document...
So question - how to check if I can write to the document?
Ive just stumpled over the same issue, the hint with bool QFileInfo::isWritable() const didn't do the trick for me. Though you basically gave the answer yourself with the comment on the third last command, with the return value of QPainter::begin() you can check if the QPdfWriter can generate the pdf.
QPainter *p = new QPainter;
if (p->begin(pdf)) {
ui->chartView->render(p);
p->end();
} else {
// error message...
}
So my question is as follows:
When I run the code for this insert helper method, and I am positive my new node method is correct as it works for instantiating a Binary Search Tree, no nodes are inserted. Why can't I use this certain implementation? What's going wrong here?
I know how to use the other insert implementation where one would check for the left and right nodes of the root and whether or not they are null, but can not figure out the problem of this more elegant possibility. The answer to this will help me in creating other functions that go beyond the scope of the insert function.
btw yes I have another function calling this helper function
Thanks!!!!!
//INSERT METHODS
void BinarySearchTree::insert(int data, struct node* root) {
//If root is null make new node there
if (!root) {
root = new node(data);
}
else if (root -> data > data) {
insert(data, root -> left);
}
else {
insert(data, root -> right);
}
}
The variable root is a parameter, which only has local visibility for that one method call. Meaning root = new node(data) will indeed create a new node, but that will only be pointed to by the parameter. Your method doesn't return anything and it doesn't actually know what it is supposed to do with that new root object of yours (it is NOT the same as any class variable you might have defined that is named the same).
So you create a new node, but can't use it outside that one method call. Which results in an empty tree.
As a side note for future questions: Include a tag for the programming language you are using. A lot of people use that as a filter, so you will actually get more people looking at this if you use the right tag.
Line 24: if (word.Length<3)
Line 25: {
Line 26: Label1.Visible = true;
Source File: C:\Users\c-tac\Documents\Visual Studio 2010\Projects\telephone\telephone\show.aspx.cs Line: 24
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
Seems pretty obvious, word is not set to an instance; in other words word is null.
Put a check to make sure word is not used unless it is instantiated as something, like this:
if(word != null)
{
// Do stuff with word, because you know it actually exists now
}
Note: This is referred to as defensive programming and will eliminate almost all NullReferenceExceptions in your code. It also has the added benefit of making you think about what you should do with your code in case a particular object is null (such as should this should be reported to the user, should this cause the application to end, etc.).
NullReferenceException happens when you want to do something like iteration with an element that normally you can't do it with null value. You should check your code and if the value is null make sure that you assign a default value to in before you other stuff. or just do the easy way and use try and catch.
try
{
// do your stuff with word
}
catch
{
// handle the null exception
}
although based on your code null means that your word length is zero. so you can do this as well.
if(word != null || word.lenght<3)
{
// do your thing
}
I am continuously getting data for my application as it runs, but I am having a bit of trouble displaying the data once I have read it in and stored it in a map.
When I try to display the data in the QML, it simply displays zero, despite the fact that I can see it updating in the application output.
I access the value in QML using property bindings (I was under the impression that these led headingSensor to be updated whenever carData.headingSensor changed?):
property int headingSensor: carData.headingSensor
Text { text: "Heading: " + headingSensor }
In my data class I have:
Q_PROPERTY(int headingSensor READ getHeadingSensor NOTIFY headingSensorChanged)
int headingSensor;
In the c++ implementation I originally had:
int data::getHeadingSensor(){
return data.value(heading)[headingSensorReading];
}
Where it returns the value in the map which is being updated with the incoming information.
This I realized, probably doesn’t work, because the property is dependent upon the headingSensor variable, which is itself not being updated despite the correct value being returned. So, I thought if I changed it to update the headingSensor value and return that it might work.
So in my data aquisition logic I wrote a method to update the variables as well.
data.insert(key, value);
updateVariables();
}
}
}
void data::updateVariables(){
headingSensor = data.value(heading)[headingSensorReading];
}
int data::getHeadingSensor(){
return headingSensor;
}
While this led to the headingSensor variable being updated in addition to the value in the map, the correct value is still not displayed in the QML display. It simply displays 0 (its default value when it is initially displayed since it has not gotten a value from incoming data yet).
So, I am wondering, how can I get the value of sensorHeading displayed in the QML to update as the value of it and/or the value in the map changes in C++? Do I need to do something like:
Connections {
target: carData
onSensorHeadingChanged: updateValues
}
EDIT:
Trying something like this, the onSensorHeadingChanged never fires. I am not sure why, since the value of sensorHeading clearly changes as I watch it in the application output
Connections{
target: carData
onHeadingSensorChanged: console.log("It's noting the change!")
}
It is the responsibility of the C++ element writer to emit headingSensorChanged() in order to cause the binding to be updated.
This tutorial is a good place to start when implementing a C++ element.
In your case you need to do something like this:
void data::updateVariables(){
int sensorReading = data.value(heading)[headingSensorReading];
if (headingSensor != sensorReading) {
headingSensor = sensorReading;
emit headingSensorChanged();
}
}
Note that we don't emit the change notifier unless there really is a change. This prevents needless JS evaluations, and also removes the possibility of binding loops.
I'm fairly new to Rational Functional Tester (Java) but I have one large blank. I have an application that is in an agile development environment so some of the screens can flux as new interfaces are brought online.
For this reason I'm trying to modularize my test scripts. For example: I would like to have a login script, a search script, and a logout script.
I would then stitch these together (pseudo code)
Call Script components.security.Login;
Call Script components.search.Search;
//verification point
Call Script components.security.Logout;
By breaking the testing script into discrete chunks (functional units) I believe that I would be better able to adapt to change. If the login script changed, I would fix or re-record it once for every script in the application.
Then I would call that script, say, "TestSituation_001". It would have need to refer to several different data pools. In this instance a User datapool (instead of a superUser datapool) and a TestSituation_001 datapool, or possibly some other datapools as well. The verfication point would use the situational datapool for its check.
Now, this is how I would do it in an ideal world. What is bothering me at the moment is that it appears that I would need to do something entirely different in order to get the child scripts to inherit the parents.
So my questions are these:
Why don't child scripts just inherit the calling script's data pool?
How can I make them do it?
Am I making poor assumptions about the way this should work?
If #3 is true, then how can I do better?
As a side note, I don't mind hacking the heck out of some Java to make it work.
Thanks!
I solved my own problem. For those of you who are curious, check this out:
public abstract class MyTestHelper extends RationalTestScript
{
protected void useParentDataPool() {
if(this.getScriptCaller() != null) {
IDatapool dp = this.getScriptCaller().getDatapool();
IDatapoolIterator iterator = DatapoolFactory.get().open(dp, "");
if(dp != null && iterator != null) {
//if the datapool is not null, substitute it for the current data pool
this.dpInitialization(dp, iterator);
}
}
}
}
This will use the same iterator too. Happy hunting...
Actually, after some reflection, I made a method that would make any given script use the Root calling script's DataPool. Again, happy hunting to those who need it...
/*
* preconditions: there is a parent caller
* postconditions: the current script is now using the same datapool / datapool iterator as the root script
*/
protected void useRootDataPool() {
//if there is no parent, then this wouldn't work so return with no result;
if(this.getScriptCaller() == null) return;
//assume that we're at the root node to start
RationalTestScript root = this;
while(root.getScriptCaller() != null) {
root = root.getScriptCaller();
}
//if this node is the root node, no need to continue. the default attached datapool will suffice.
if(this.equals(root)) return;
//get the root's data pool (which would be the parent's parent and so on to the topmost)
IDatapool dp = root.getDatapool();
if(dp != null) {
//check to make sure that we're not trying to re-initialize with the same datapool (by name)
//if we are, then leave
if(dp.getName().equals(this.getDatapool().getName())) return;
//this basically says "give me the iterator already associated to this pool"
IDatapoolIterator iterator = DatapoolFactory.get().open(dp, "");
//if we have an iterator AND a data pool (from above), then we can initialize
if(iterator != null) {
//this method is never supposed to be run, but this works just fine.
this.dpInitialization(dp, iterator);
//log information
logInfo("Using data pool from root script: " + root.getScriptName());
}
}
}