Hi, @Ezekiel dela Peña (https://teamtreehouse.com/ezekieldelapena) the NullPointerException you're getting here is because you're running your Java Code in an IDE (i.e. Eclipse, NetBeans), in an IDE environment, the console object isn't available to you; hence the NullPointerException. In order for it to work, you have to run your code in a console environment. IF you prefer to you Eclipse to follow along with the course; I'd suggest that you use Scanner , BufferedReader , or InputStreamReader class to get user input; and something like System.out.printf() for output. Answer from William Li on teamtreehouse.com
🌐
Reddit
reddit.com › r/fabricmc › the game crashed whilst unexpected error, error: java.lang.nullpointerexception: unexpected error, exit code: -1
r/fabricmc on Reddit: The game crashed whilst unexpected error, Error: java.lang.NullPointerException: Unexpected error, Exit code: -1
October 20, 2023 -

Hi!

I have been playing the RAD (Rougelike Adventures and Dungeons) modpack for Minecraft this week. The mod has been working great. There has been an occasional lag, but nothing to complain about. To overcome this occasional lag, I restart my computer and it always works fine. I restarted my computer this time, opened RAD, and it crashes and gives me this error:

Below is the crash report:

https://paste.gg/p/anonymous/126071bac6514edba79453a24dcb95f3

And here is the latest log file:

https://paste.gg/p/anonymous/42d7108f74974297bc7ba19d044f09e8

RAD runs on Minecraft 1.12.2, and the RAD version is 1.49. I also use Curseforge to start the mod. I appreciate any help with this!!! Let me know if I need to add anything or if you need more info!

Thank you so much!!!

🌐
Minecraft Forum
minecraftforum.net › home › minecraft forum › support › java edition support › modded server error: java.lang.nullpointerexception: unexpected error
Modded Server Error: java.lang.NullPointerException: Unexpected error - Java Edition Support - Support - Minecraft Forum - Minecraft Forum
August 26, 2022 - I was playing a modded server, tried to use a spell from Ars Magica 2, and got kicked. Every time I join the launcher crashes and it gives the same error message. Someone else on the server was able to use the exact same spell with no problem before I did.
Discussions

java.lang.NullPointerException: Unexpected error
Ok so im using the Origins adventure modpack and my friend can join the server but somehow i cant. it goes through all the normal processes until it gets to joining world at which point it just crashes and gives me the error. any ideas as to whats doing… More on board.aternos.org
🌐 board.aternos.org
7
January 7, 2022
Unexpected error Error: java.lang.NullPointerException - Support & Bug Reports - Forge Forums
Good Evening, I am trying to figure out what is wrong with my modded version of minecraft. If its too many mods or if it's something I need to add. Below is the crash report. ---- Minecraft Crash Report ---- WARNING: coremods are present: Contact their authors BEFORE contacting forge // Would you... More on forums.minecraftforge.net
🌐 forums.minecraftforge.net
November 11, 2018
How to fix java.lang.NullPointerException?
Ezekiel dela Peña is having issues with: I'm currently using Eclipse LUNA (JAVA EE IDE) and I'm currently doing the prompting guess activity on a Java Basic Course. ERROR MESSAG... More on teamtreehouse.com
🌐 teamtreehouse.com
2
March 26, 2015
How to solve java.lang.NullPointerException error? - Stack Overflow
This error occures when you try to refer to a null object instance. I can`t tell you what causes this error by your given information, but you can debug it easily in your IDE. I strongly recommend you that use exception handling to avoid unexpected program behavior. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Broadcom
knowledge.broadcom.com › external › article › 229470 › user-has-unexpected-error-javalangnullpo.html
User has unexpected error java.lang.nullpointerException
November 2, 2023 - "We're Sorry! An unexpected error has occurred. We have recorded this error and will begin to investigate it. In the meantime, if you would like to speak with our Support Team, Please reference the information below:java.lang.NullPointerException"
🌐
Aternos Community
board.aternos.org › thread › 85052-java-lang-nullpointerexception-unexpected-error
java.lang.NullPointerException: Unexpected error - Server - Aternos Community
January 7, 2022 - Ok so im using the Origins adventure modpack and my friend can join the server but somehow i cant. it goes through all the normal processes until it gets to joining world at which point it just crashes and gives me the error. any ideas as to whats doing this? https://mclo.gs/FOksoxM crash report · Code · java.lang.NullPointerException: Unexpected error ·
🌐
Forge Forums
forums.minecraftforge.net › home › minecraft forge › support & bug reports › unexpected error error: java.lang.nullpointerexception
Unexpected error Error: java.lang.NullPointerException - Support & Bug Reports - Forge Forums
November 11, 2018 - Before digging deeper, I’d try stripping the pack down to just Create and Structure Gel to see which one triggers the crash, then add mods back one by one. The error in your log points to something firing during world load, usually a bad mixin or missing dependency.
🌐
Hypixel Forums
hypixel.net › home › forums › games › skyblock › skyblock general discussion
java.lang.NullPointerException: Unexpected error | Hypixel Forums
March 31, 2021 - Click to expand... if it was literally any other mod then I would know from the crash report. because SBE's code is obfuscated the error would be coming from a file like !I1iILli1ILL!iLi!LiL1IL1L.java so I wouldn't know what it was trying to do when the error occurred.
Find elsewhere
🌐
Sentry
sentry.io › sentry answers › java › what is a nullpointerexception, and how do i fix it?
What is a NullPointerException, and how do I fix it? | Sentry
A NullPointerException in Java is one of the most common errors. It means that you are trying to access a part of something that doesn’t exist. For example, in the code below we call .length() on myString, which would usually return the length of the string. In this case, the string doesn’t exist (we set it to null), and so this throws a NullPointerException.
🌐
Boomi
community.boomi.com › s › article › What-may-be-the-causes-of-java-lang-NullPointerException-error
Article: What may be the causes of java.lang.NullPointerException error? - Boomi Community
September 30, 2025 - This is a common runtime error ... data in scripts or integrations. ... A java.lang.NullPointerException occurs when a Java application attempts to use an object reference that has not been initialized (i.e., it is null)....
🌐
GitHub
github.com › Asek3 › Oculus › issues › 402
The game crashed whilst unexpected error Error: java.lang. ...
July 4, 2023 - The game crashed whilst unexpected error Error: java.lang.NullPointerException: Cannot invoke "net.minecraft.client.renderer.ShaderInstance.m_173350_(String, Object)" because "$$13" is null exit code: -1
Author: Asek3
🌐
Rollbar
rollbar.com › home › how to catch and fix nullpointerexception in java
How to Catch and Fix NullPointerException in Java
June 30, 2026 - In Java, primitives cannot have null values. Writing methods that return empty objects rather than null where possible. For example, returning empty collections and empty strings from a method. To fix the NullPointerException in the earlier example, the String object should be checked for null or empty values before it is used any further: import org.apache.commons.lang3.StringUtils; public class NullPointerExceptionExample { private static void printLength(String str) { if (StringUtils.isNotEmpty(str)) { //Check to ensure not null or empty System.out.println(str.length()); } else { System.out.println("Empty string"); } } public static void main(String args[]) { String myString = null; printLength(myString); } }
Top answer
1 of 12
4227

There are two overarching types of variables in Java:

  1. Primitives: variables that contain data. If you want to manipulate the data in a primitive variable you can manipulate that variable directly. By convention primitive types start with a lowercase letter. For example variables of type int or char are primitives.

  2. References: variables that contain the memory address of an Object i.e. variables that refer to an Object. If you want to manipulate the Object that a reference variable refers to you must dereference it. Dereferencing usually entails using . to access a method or field, or using [ to index an array. By convention reference types are usually denoted with a type that starts in uppercase. For example variables of type Object are references.

Consider the following code where you declare a variable of primitive type int and don't initialize it:

int x;
int y = x + x;

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. All primitives have to be initialized to a usable value before they are manipulated.

Now here is where things get interesting. Reference variables can be set to null which means "I am referencing nothing". 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).

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.

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. So you have a reference to something that does not actually exist.

Take the following code:

Integer num;
num = new Integer(10);

The first line declares a variable named num, but it does not actually contain a reference value yet. Since you have not yet said what to point to, Java sets it to null.

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.

If you attempt to dereference num before creating the object you get a NullPointerException. 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.

For instance, you may have a method as follows:

public void doSomething(SomeObject obj) {
   // Do something to obj, assumes obj is not null
   obj.myMethod();
}

In which case, you are not creating the object obj, but rather assuming that it was created before the doSomething() method was called. Note, it is possible to call the method like this:

doSomething(null);

In which case, obj is null, and the statement obj.myMethod() will throw a NullPointerException.

If the method is intended to do something to the passed-in object as the above method does, it is appropriate to throw the NullPointerException because it's a programmer error and the programmer will need that information for debugging purposes.

In addition to NullPointerExceptions thrown as a result of the method's logic, you can also check the method arguments for null values and throw NPEs explicitly by adding something like the following near the beginning of a method:

// Throws an NPE with a custom error message if obj is null
Objects.requireNonNull(obj, "obj must not be null");

Note that it's helpful to say in your error message clearly which object cannot be null. The advantage of validating this is that 1) you can return your own clearer error messages and 2) for the rest of the method you know that unless obj is reassigned, it is not null and can be dereferenced safely.

Alternatively, there may be cases where the purpose of the method is not solely to operate on the passed in object, and therefore a null parameter may be acceptable. In this case, you would need to check for a null parameter and behave differently. You should also explain this in the documentation. For example, doSomething() could be written as:

/**
  * @param obj An optional foo for ____. May be null, in which case
  *  the result will be ____.
  */
public void doSomething(SomeObject obj) {
    if(obj == null) {
       // Do something
    } else {
       // Do something else
    }
}

Finally, How to pinpoint the exception & cause using Stack Trace

What methods/tools can be used to determine the cause so that you stop the exception from causing the program to terminate prematurely?

Sonar with find bugs can detect NPE. Can sonar catch null pointer exceptions caused by JVM Dynamically

Now Java 14 has added a new language feature to show the root cause of NullPointerException. This language feature has been part of SAP commercial JVM since 2006.

In Java 14, the following is a sample NullPointerException Exception message:

in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.List.size()" because "list" is null

List of situations that cause a NullPointerException to occur

Here are all the situations in which a NullPointerException occurs, that are directly* mentioned by the Java Language Specification:

  • Accessing (i.e. getting or setting) an instance field of a null reference. (static fields don't count!)
  • Calling an instance method of a null reference. (static methods don't count!)
  • throw null;
  • Accessing elements of a null array.
  • Synchronising on null - synchronized (someNullReference) { ... }
  • Any integer/floating point operator can throw a NullPointerException if one of its operands is a boxed null reference
  • An unboxing conversion throws a NullPointerException if the boxed value is null.
  • Calling super on a null reference throws a NullPointerException. If you are confused, this is talking about qualified superclass constructor invocations:
class Outer {
    class Inner {}
}
class ChildOfInner extends Outer.Inner {
    ChildOfInner(Outer o) { 
        o.super(); // if o is null, NPE gets thrown
    }
}
  • Using a for (element : iterable) loop to loop through a null collection/array.

  • switch (foo) { ... } (whether its an expression or statement) can throw a NullPointerException when foo is null.

  • foo.new SomeInnerClass() throws a NullPointerException when foo is null.

  • Method references of the form name1::name2 or primaryExpression::name throws a NullPointerException when evaluated when name1 or primaryExpression evaluates to null.

    a 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!

* Note that the JLS probably also says a lot about NPEs indirectly.

2 of 12
972

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. Calling a method on a null reference or trying to access a field of a null reference will trigger a NullPointerException. These are the most common, but other ways are listed on the NullPointerException javadoc page.

Probably the quickest example code I could come up with to illustrate a NullPointerException would be:

public class Example {

    public static void main(String[] args) {
        Object obj = null;
        obj.hashCode();
    }

}

On the first line inside main, I'm explicitly setting the Object reference obj equal to null. This means I have a reference, but it isn't pointing to any object. After that, I try to treat the reference as though it points to an object by calling a method on it. This results in a NullPointerException because there is no code to execute in the location that the reference is pointing.

(This is a technicality, but I think it bears mentioning: A reference that points to null isn't the same as a C pointer that points to an invalid memory location. A null pointer is literally not pointing anywhere, which is subtly different than pointing to a location that happens to be invalid.)

🌐
Mbse-capella
forum.mbse-capella.org › t › fixing-the-error-java-lang-nullpointerexception › 5865
Fixing the error "java.lang.NullPointerException" - Capella - Eclipse Capella Forum
November 7, 2022 - Hello, I read a post from 2019 stating the origin of the error being related to the situation where your program (capella here) points out to a memory address located in a computer which is not the one you are using he…
🌐
Oracle
docs.oracle.com › en › java › javase › 19 › docs › api › java.base › java › lang › NullPointerException.html
NullPointerException (Java SE 19 & JDK 19)
December 12, 2022 - java.lang.NullPointerException · All Implemented Interfaces: Serializable · public class NullPointerException extends RuntimeException · Thrown when an application attempts to use null in a case where an object is required. These include: Calling the instance method of a null object.
🌐
Altera Community
community.altera.com › altera community › knowledge base
Why do I get the error "java.lang.NullPointerException"? | Altera Community - 345567
November 19, 2025 - If you have recently upgraded or reinstalled Altera's SBT4E, or Eclipse or Java tool installations available from 3rd parties, it is recommended that you do a clean installation of the Altera SBT4E to ensure the correct versions of Eclipse and Java tools are installed.
🌐
GitHub
github.com › CaffeineMC › sodium › issues › 1909
[Fabric 1.20.1][CRASH] java.lang.NullPointerException · Issue #1909 · CaffeineMC/sodium
July 24, 2023 - Bug Description This issue occurs at random after having updated some mods recently, but it occurs mostly within my base in which there are many things that need to get rendered. The client crashes immediately to desktop (CTD) upon encou...
Author: CaffeineMC
🌐
Java Journey
java-journey.com › 2025 › 12 › 19 › java-exception-handling-building-robust-and-error-resilient-applications
Java Exception Handling: Building Robust and Error-Resilient Applications – Java Journey
April 28, 2026 - Allows recovery from errors or cleanup of resources. ... Must be handled or declared in the method signature. Examples: IOException, SQLException, FileNotFoundException. ... Do not need to be declared or caught. Examples: NullPointerException, ArrayIndexOutOfBoundsException, ArithmeticException.