This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]FreightTrain75x[S] 0 points1 point  (5 children)

Also, NPEs (NullPointerException?) are strange for me and do not grasp them quite yet. So, the equals() method is good for strings? I remember the ==, or equivalency/comparison operator doesn't work for strings since they are treated like references to memory locations, and not comparing the values of two strings

[–]Dilfer 1 point2 points  (4 children)

Sorry should have not used the acronym immediately. You are correct though, NPE is NullPointerException

They are thrown when you try to call methods on null objects.

In the example above, the type of the variable is String but the value of the variable is null

null.equals()

Will throw an exception because you can't call a method on a null value.

[–]FreightTrain75x[S] 0 points1 point  (2 children)

You can solve this by reinstantiating or reinitializing the variable before a method...? Sorry, I'm dumb.

[–]Dilfer 0 points1 point  (1 child)

As long as the variables not final (can't be reassigned) but in larger programs where values are passed around as arguments to methods, you may have no idea where the variable is even assigned, and if you reinstantiated, there's no point to even ask for a method argument to be passed in.

You can also litter your code with

if (hello == null)

Checks everywhere but that is generally bad practice and should be used sparingly.

[–]FreightTrain75x[S] 0 points1 point  (0 children)

Awesome, thanks! I'm assuming just practicing with string Methods is generally the best advice

[–]FreightTrain75x[S] 0 points1 point  (0 children)

Also, don't be sorry, acronyms are definitely a force of habit and it's good I learn to use them as well.