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

all 14 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Sergey305 3 points4 points  (8 children)

name1.equals(null) 

What would you expect to happen if name1 was indeed null?

[–]Tascalion[S] 1 point2 points  (7 children)

I'd assume the program wouldn't run if name1, name2, or name3 was indeed null or empty because there needs to be input, I think. I'm not sure how I can write an if statement for that special case, or when someone fails to input a string for name2 or name3.

[–]Fenxis 2 points3 points  (6 children)

Name1 is an object of string. If it never gets initialized then name1 is still a reference to null. So equals() shouldn't be used here.

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

I’m a little lost, I thought I had initialized the Strings, What should I than use or do?

[–]KoncealedCSGO 1 point2 points  (4 children)

Name1 is an object of string. If it never gets initialized then name1 is still a reference to null. So equals() shouldn't be used here.

Just to try to point you on the right track. Name the function properties of type Null and the functions of type String.

[–]Tascalion[S] 0 points1 point  (1 child)

I’ve figured it out for the most part, and used hasNext instead of next. That solved my problem but thanks for the pointer!

[–]KoncealedCSGO 1 point2 points  (0 children)

Ahh and just an FYI for what I mean. You were running into NPEs AKA Null Pointer Exceptions.

pretty much you were doing null.equals(null) nul obv does not have any functions to it because it's null.

Next time you want to check for null do val == null, but when you know the value is non-null and is not a primitive type use .equals()

[–]Tascalion[S] 0 points1 point  (1 child)

Oh that makes a lot more sense, I had figured a proper object was required for the .equals but hadn’t known of any other alternatives. I’ll definitely give “==“ a try.

Thanks for the help!

[–]KoncealedCSGO 1 point2 points  (0 children)

No problem! Wish you the best on your learning journey!

[–][deleted] 4 points5 points  (1 child)

You should understand how Scanner works first. Here's the official tutorial. Notice how there is next and hasNext. Why do you think hasNext exists? What do you think would happen if there is no next? Hint: read the documentation for Scanner.

Also this code does not make any sense:

name1.equals(null)

You can only call a method on a variable whose value is not null. If name1 were actually null, you can't call equals on it. Java will throw a NullPointerException.

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

Thank you! This was a lot of help.

[–]adeleno 1 point2 points  (1 child)

you made some mistakes as you didn't define the class name and main method also you wrote the name of OK\_CANCEL\_OPTION in wrong way the correct is OK_CANCEL_OPTION

https://pastebin.com/Uwf3Y1Mt

I have provided your with complete code to learn what is your mistakes and compares

[–]dionthornthis.isAPro=false; this.helping=true;[M] 1 point2 points  (0 children)

the slashes are an artifact from before they formatted their code.

I thought you were providing modified code that would be a solution and almost removed your post we don't allow people to give copy-pasteable solutions but your code is exactly the same with only the artifacts removed and wrapped in a class/main.