Quantcast
Channel: Answers by "The_Magical_Kiwi"
Viewing all articles
Browse latest Browse all 29

Answer by The_Magical_Kiwi

$
0
0
You get a null reference exception when your script tries to access a piece of information that isn't set to anything (it is null). I'm assuming you know what a variable is? Variables normally have default values, for instance: //Some pseudo code int foo; print(foo); This should print "0" because an integer is 0 by default. References are a pointer to an object. For instance if you have a 'Bike' class, you can create a reference to it in the same way you declare a variable. //something like (again pseudo code) int speed = 10; //This next line creates the reference 'myBike'. //myBike is a reference to the new instance of the Bike we are creating. Bike myBike = bike(speed); For simplicity sake, it isn't too important right now that you know the difference between a reference and a variable, what is important to know is that references don't have default values. If you don't set it to something it remains a reference to nothing, it is null. If you try to look at or interact with a null reference you will get the error "null reference exception". if (current.currentExtraAmmo < current.maxExtraAmmo) In your case, it would appear that the reference 'current' is null, it hasn't been pointed to an instance of anything. It can't check if extraAmmo is less than maxExtraAmmo because it doesn't know what object to look in. A very long winded answer I know, but I hope it explains why you got the error and not just how to fix it without you understanding the cause. Also it is extremely difficult to answer this question without using some simple programming jargon such as object, variable, pseudo code, reference and so on. However a quick google search should be able to explain these concepts much better than I can. Hope this has helped.

Viewing all articles
Browse latest Browse all 29

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>