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 Java Objects Meet Objects Final

Why am I getting "null" in the console when I run code?

Thank you!

what code?

Nourez Rawji
Nourez Rawji
3,303 Points

It's hard to say when you don't have any code for us to look over ;)

Ilja Ankipovits
Ilja Ankipovits
1,630 Points

Please Help, i have the same problem with "null"

public class Example {

public static void main(String[] args) { // Your amazing code goes here... System.out.println("We are making a new PEZ Dispenser"); PezDispenser dispenser = new PezDispenser ("Yoda"); System.out.printf("The dispenser is %s %n", dispenser.getCharacterName() );

String before = dispenser.swapHead("Darth Vader"); System.out.printf("It wast %s but Chris swither it to %s %n", before, dispenser.getCharacterName());

}

}

Console: We are making a new PEZ Dispenser The dispenser is null It wast null but Chris swither it to Darth Vader

3 Answers

Please copy and send us your code for review, that would help greatly in trying to help you solve your problem

Ilja Ankipovits
Ilja Ankipovits
1,630 Points

Please Help, i have the same problem with "null"

public class Example {

public static void main(String[] args) { // Your amazing code goes here... System.out.println("We are making a new PEZ Dispenser"); PezDispenser dispenser = new PezDispenser ("Yoda"); System.out.printf("The dispenser is %s %n", dispenser.getCharacterName() );

String before = dispenser.swapHead("Darth Vader"); System.out.printf("It wast %s but Chris swither it to %s %n", before, dispenser.getCharacterName());

}

}

Console: We are making a new PEZ Dispenser
The dispenser is null
It wast null but Chris swither it to Darth Vader

Mouz Asfaw Aligaz
Mouz Asfaw Aligaz
1,827 Points

Miguel and Ilja, Check PezDispenser.java for the "name" variable in the constructor and change it to "characterName" without the quotes. That was what solved it for me.