Regex is giving me a NullPointerException...

Discussion in 'Plugin Development' started by TheExoduser, Dec 30, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    TheExoduser

    Hi all,

    at the moment I'm trying to detect all Numbers in a String and Strip them away... I've already tried it with Regex, but there is always a NullPointerException.

    My code:
    Code:java
    1. String CustomName = e.getEntity().getCustomName();
    2. String Name = CustomName.replaceAll("[0-9]+", "");


    The NullPointerException:
    Code:
    Caused by: java.lang.NullPointerException
            at me.theexoduser.plugins.minequestlevels.Events.DeathEvent.onDeath(Deat
    hEvent.java:23)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425)
            ... 21 more
    (On line 23 is the above Java Code)

    The monsters on my test server have a number in there CustomName but also a String. So I want to strip any Number in the CustomName away and keep only the String. But I Don't know what I'm making wrong.

    Help would be much appreciated.
     
  2. Offline

    Chinwe

    .getCustomName() returns null if there is no custom name, so make sure 'CustomName' isn't null before trying to remove numbers :)

    Also, your variables should be lower camel case, such as 'customName' and 'name' :>
     
  3. Offline

    LucasEmanuel

    It is probably something else, that regex pattern looks fine to me. Can you show us the rest of your code?

    EDIT:
    Ninjad by Chinwe ;)
     
  4. Offline

    TheExoduser

    I think I'm going mad -.- Chinwe had the idea... the .getCustomName() was null and I didn't catched it... Now it's working... after I've added an if :/

    Thanks to all :D didn't expected the problem to be solved that quick :)
     
Thread Status:
Not open for further replies.

Share This Page