-2

I am having a weird problem in this code. When I execute the code with the first if condition, it all works fine and as expected. However, when I comment that if statement and use the other if condition (the one already commented), it gives an NPE.

Both the getUserIP() and getPhoneNumber() are just normal getters for private Strings. And both values are being set before by normal setters. Why is this happening?

public void sendBroadcast(final String broadcast) {
    System.out.println("entered sendBroadcast");

        String fullPeep = broadcast;
        System.out.println("fullPeep: " + fullPeep);
        String array[] = fullPeep.split("<!!>");

        for(User tempUser: friends)
        {
            if(tempUser.getUserIP().equals(this.getUserIP()))
            {
            System.out.println("tempuser:" + tempUser.getPhoneNumber() + " user: " + array[1]);
            //if(tempUser.getPhoneNumber().equals(array[1]))
            //{
                System.out.println("tempuser:" + tempUser.getPhoneNumber() + " user: " + array[1]);
                System.out.println("if statement of broadcast method");

            try {
                DataOutputStream out2 = new DataOutputStream(socket.getOutputStream());
                out2 = tempUser.getUserDataOutputStream();
                out2.writeUTF(fullPeep + "\n");
                out2.flush();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        //}
    }
}

Exception in thread "Thread-6" java.lang.NullPointerException
at User.sendBroadcast(User.java:180)
at Server$ServerThread.run(Server.java:394)

I figured out what is causing the exception and why tempUser.getPhoneNumber() was returning null at some point.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
T-D
  • 373
  • 8
  • 21
  • 1
    `tempUser.getUserIP()` is `null`, debug your code to know why. – Maroun Feb 06 '14 at 10:54
  • 2
    What does `System.out.println("tempuser:" +tempUser.getPhoneNumber() + " user: "+array[1] );` print? – tobias_k Feb 06 '14 at 10:56
  • @sᴜʀᴇsʜᴀᴛᴛᴀ Exception in thread "Thread-6" java.lang.NullPointerException at User.sendBroadcast(User.java:180) at Server$ServerThread.run(Server.java:394) – T-D Feb 06 '14 at 11:01
  • 3
    which one is the 180th line in code? – A Paul Feb 06 '14 at 11:03
  • the code works fine when using `tempUser.getUserIP()` however it doesnt when using `tempUser.getPhoneNumber()` – T-D Feb 06 '14 at 11:03
  • @APaul its the line of code i mentioned that is causing the problem i.e. ` if(tempUser.getPhoneNumber().equals(array[1]))` – T-D Feb 06 '14 at 11:04
  • 2
    Then tempUser.getPhoneNumber() is null. Just check why it is not seting the value. I mean debug the code. – A Paul Feb 06 '14 at 11:05
  • @APaul i did debug it, and it is setting the value, and the value is being printed, but i still i get that exception. – T-D Feb 06 '14 at 11:07
  • 3
    Did you check *every* User object? At least one of them obviously has a null phone number. – Boann Feb 06 '14 at 11:08
  • @Boann why would one of them has a null phone number if i am setting the phone number the same way for all users ? when a user connects to the server, i set the phone number accordingly. unless its the current user's phone number that is returning null for some reason. Thanks – T-D Feb 06 '14 at 11:11
  • of. Just confirm "System.out.println("tempuser:" +tempUser.getPhoneNumber()" never prints null, you have cheked that correct? – A Paul Feb 06 '14 at 11:12
  • or you can change your if statement : if(tempUser != null && tempUser.getPhoneNumber()!= null && array[1] != null && tempUser.getPhoneNumber().equals(array[1])) – pshemek Feb 06 '14 at 11:15
  • @pshemek array[1] != null is not required. if it is null the equals will return false – A Paul Feb 06 '14 at 11:19
  • 2
    Possible duplicate: *[What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it)* – Peter Mortensen Aug 30 '20 at 16:06

1 Answers1

5

Change:

if(tempUser.getPhoneNumber().equals(array[1]))

To:

if(java.util.Objects.equals(tempUser.getPhoneNumber(), array[1]))

Objects.equals is null-safe and will not mind when the phone number is null.

Boann
  • 48,794
  • 16
  • 117
  • 146
  • If I am not wrong this will only work from Java 7. So if you are using java 7+ it is a good option. +1 for the answer. – A Paul Feb 06 '14 at 11:23
  • Iam using java 7+, and this does solve the exception but unfortunately doesnt solve my problem. i still cant get the method to work with phonenumber just like userIP even though they are the same...Thanks. – T-D Feb 06 '14 at 11:37
  • @Toufic They can't be the same. Either the one you're looking for is null, or it is non-null but it has extra space compared to the string split from fullPeep, or something like that. – Boann Feb 06 '14 at 11:59
  • 1
    @Boann Yea, tempUser.getPhoneNumber() was returning null at some point. the connection to the server was closing at some point in the app and reconnecting again so there was an extra tempUser with a null pointer to phone number. Thanks for your help. – T-D Feb 07 '14 at 10:38
  • 1
    @Toufic If the phone number is required to never be null it might make sense to enforce that in the User object's constructor and/or setters. Throwing an exception during construction will identify the problem faster than causing weird exceptions later in unrelated code. – Boann Feb 07 '14 at 10:40
  • @Boann True. I fixed that. But at first i had no idea that the app is closing and opening a new connection with the server hence that problem wasn't easy to find at the first look. – T-D Feb 07 '14 at 11:10