1

I have the following code within a for loop to see if a string equals a search string:

if(Data.coord[i].equals(Data.search))

I've tested the code with exact values i.e if 1=1 and the rest of the code works fine. It just doesn't like the string comparison. The consol gives out this error:

Exception in thread "main" java.lang.NullPointerException
at highercoursework.Search.main(Search.java:16)
at highercoursework.Main.main(Main.java:16)

Thanks

f_puras
  • 2,483
  • 4
  • 28
  • 36

6 Answers6

2

You have an unpopulated element in your array i.e.

Data.coord[i]

is null. Note that Data.search could be null, but the equals() method will handle this. You just need to perform the lement check first.

Brian Agnew
  • 254,044
  • 36
  • 316
  • 423
  • Ahh right, I can't think of a way to avoid this. I could do to ifs first to check if values are null and then only compare none null values? – user1627774 Nov 15 '12 at 10:27
  • 1
    Nope you should call `equals()` on an object you know it exists thus using the form in my answer. This is considered a good practice when comparing objects. Always call `equals()` on the object you already know is not null if possible. – Adam Arold Nov 15 '12 at 10:29
  • i think @AdamArold is absolutely correct , logically it saves you the trouble plus avoids your exception , this is more appropriate approach – Hussain Akhtar Wahid 'Ghouri' Nov 15 '12 at 10:31
2

You should compare the constant to your parameter since it can be null.

For example if Data.search is a constant which you are searching for you should do this:

if(Data.search.equals(Data.coord[i]))

In this case you won't end up trying to call methods on a null reference and you won't need unnecessary null checks either.

Adam Arold
  • 26,256
  • 18
  • 92
  • 176
1
String[] coord = new String[100];

This will mean you can assign something to coord[0] but until you do that coord[0] is null. Hence the null pointer exception.

You can try.

String data= Data.coord[i];
if(data != null && data.equals(Data.search))
Ashwinee K Jha
  • 8,648
  • 2
  • 21
  • 18
1

you can avoid your problem in two ways:

  • In the case coord[i] should not be null

    if (Data.coord[i] != null) {
        if(Data.coord[i].equals(Data.search)) {
    
        }
    } else {
        logger.error("Unexpected Behavior: coord[i] should not be null");
    }
    

Note: You can replace the logger message by a more appropriated code that fit to your requirement.


  • In the case your your coord[i] can be null

comparing in this way won't throw an exception if Data.coord[i] is null. (Assuming Data.search is a constant and can't bu null) So the rules for this case is: use in priority a String object constant to call the method equals.

if (Data.search.equals(Data.coord[i])) {}
Pierre
  • 59
  • 4
0

Try this:

if(DATA != null && Data.coord[i].equals(Data.search))
Jagger
  • 9,590
  • 7
  • 42
  • 78
Sashi Kant
  • 12,422
  • 9
  • 38
  • 65
0

Read this to understand What is a Null Pointer Exception?

if coord[] is initialized properly, value of Data.coord[i] may be null. You can check

if(Data.coord[i] != null && Data.coord[i].equals(Data.search)) {}
Community
  • 1
  • 1
vels4j
  • 10,808
  • 4
  • 35
  • 54