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

5
  • 1
    Restart in Debug Mode and set a breakpoint. Commented Nov 15, 2012 at 10:25
  • The array is initialized in a data class: static String[] coord = new String[100]; However if no data is needed for that array its just stored as a null value. Commented Nov 15, 2012 at 10:25
  • 1
    Are you sure i is a valid key for Data.coord[]? Commented Nov 15, 2012 at 10:25
  • I suggest splitting this row into separate ones so that you can debug this issue from the logs. The nullpointer can be the Data.coord or the Data.coord[i] or even Data.search. the other things is to debug Commented Nov 15, 2012 at 10:25
  • seems like your String array is not initialised Commented Nov 15, 2012 at 10:27

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

In that case he has to null check one. But in this case we know that Data.search is a constant.
Do we know that ? I'm not sure that's explicit (it looks to me like a user input value or similar)
I assumed it from the context. May be it is null but I really don't think so.
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.

3 Comments

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?
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.
i think @AdamArold is absolutely correct , logically it saves you the trouble plus avoids your exception , this is more appropriate approach
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))

Comments

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])) {}

Comments

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)) {}

Comments

0

Try this:

if(DATA != null && Data.coord[i].equals(Data.search))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.