Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Pieterjan Espeel
Pieterjan Espeel
3,656 Points

Java NullPointer exception on export

I've been trying to implement some sort of basic username and password system in a game I made to experiment with saving and loading .txt files, but I can't see what I am doing wrong.

When exporting I get this error:

Enter your username: hello
Enter your password: abc123
You have made a new account with username: 'hello' and password 'abc123'

1 abc123

Welcome to the game ! Exception in thread "main" java.lang.NullPointerException
at com.TheJPeG.GameOne.Prompter.promptForUser(Prompter.java:60)
at BattleShip.main(BattleShip.java:11)

Location of main() function:

  https://gyazo.com/233ea62efa81fc87a0d74367d48746a3

Here is the prompter code:

  https://gyazo.com/9df8bbb5c0a85f29f5197bb1b200ffb1

  https://gyazo.com/1ef06bd986623430f08088efb53d70f0

Here is the loader code:

  https://gyazo.com/451ede5e169f0b6d894054e85b9dde8b

Thanks in advance for helping me out with this .. :)

1 Answer

Thomas Nilsen
Thomas Nilsen
14,957 Points

This pretty much says it all:

Welcome to the game ! Exception in thread "main" java.lang.NullPointerException at com.TheJPeG.GameOne.Prompter.promptForUser(Prompter.java:60) at BattleShip.main(BattleShip.java:11)

If we go to Prompter.java:60, you have this line:

loader.exportTo("userMap")

In your Prompter-class you have a loader, but never instantiate it.

You need to do this in you Prompter-contructor:

loader = new Loader();