![]() Processing does that a lot, because it wants to avoid exposing exceptions to users, as they are quite complex to handle. It is also often used as return value from functions supposed to return an object, to indicate an error has happened and the object isn't available. Some of these errors come from a programming error, so you just fix the error to get rid of it (we will see that for NPE here), others, like not being able to open a file, must be handled by a try / catch construct, but that's outside of the scope of this article.Īnd what is Null, or, rather, null? It is a special Java value, indicating that an object reference hasn't been initialized yet (or have been reset). Your program, or some class, throws an exception when an unrecoverable error happens (programming mistake, file isn't there, program can't connect to network, etc.) and should be handled at a higher level (by the programmer). Java has no pointers but references, which fill the same role, in a safer way (no risk of memory corruption!).Īn Exception is a language mechanism allowing to handle exceptional cases, usually errors. Null? Pointer? Exception?įirst, what is this name? It is one of the most misnamed class of Java, because this language has no pointers! This has probably historical roots, when people wrote the Java compiler in C or C++, where the word 'pointer' comes from. Fortunately, it is also one of the easiest to fix, once the most common cases are known. These are the most common, but other ways are listed on the NullPointerException javadoc page.One of the most common errors in Java (hence, in Processing) is this NullPointerException (short-named NPE). Calling a method on a null reference or trying to access a field of a null reference will trigger a NullPointerException. NullPointerExceptions are exceptions that occur when you try to use a reference that points to no location in memory (null) as though it were referencing an object. * Note that the JLS probably also says a lot about NPEs indirectly. Method references of the form name1::name2 or primaryExpression::name throws a NullPointerException when evaluated when name1 or primaryExpression evaluates to null.Ī note from the JLS here says that, someInstance.someStaticMethod() doesn't throw an NPE, because someStaticMethod is static, but someInstance::someStaticMethod still throw an NPE! In the most trivial cases, the compiler will catch the problem and let you know that " num may not have been initialized," but sometimes you may write code that does not directly create the object.įor instance, you may have a method as follows: public void doSomething(SomeObject obj) (whether its an expression or statement) can throw a NullPointerException when foo is null.įoo.new SomeInnerClass() throws a NullPointerException when foo is null. If you attempt to dereference num before creating the object you get a NullPointerException. In the second line, the new keyword is used to instantiate (or create) an object of type Integer, and the reference variable num is assigned to that Integer object. Since you have not yet said what to point to, Java sets it to null. The first line declares a variable named num, but it does not actually contain a reference value yet. So you have a reference to something that does not actually exist. The NullPointerException (NPE) typically occurs when you declare a variable but did not create an object and assign it to the variable before trying to use the contents of the variable. If a reference variable is set to null either explicitly by you or through Java automatically, and you attempt to dereference it you get a NullPointerException. You can get a null value in a reference variable if you explicitly set it that way, or a reference variable is uninitialized and the compiler does not catch it (Java will automatically set the variable to null). Reference variables can be set to null which means " I am referencing nothing". Now here is where things get interesting. All primitives have to be initialized to a usable value before they are manipulated. These two lines will crash the program because no value is specified for x and we are trying to use x's value to specify y. For example variables of type Object are references.Ĭonsider the following code where you declare a variable of primitive type int and don't initialize it: int x By convention reference types are usually denoted with a type that starts in uppercase. to access a method or field, or using [ to index an array. ![]() ![]() If you want to manipulate the Object that a reference variable refers to you must dereference it. References: variables that contain the memory address of an Object i.e. For example variables of type int or char are primitives. By convention primitive types start with a lowercase letter. If you want to manipulate the data in a primitive variable you can manipulate that variable directly. There are two overarching types of variables in Java: ![]()
0 Comments
Leave a Reply. |