Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I get an error when I do the following operation.

public static String text = "ng"; 
public static String[] specialConsonants = new String[4];
public static String[] specialConsonantsUni = new String[6];

public void setSpecial(){
    specialConsonantsUni[0] = "ං";
    specialConsonants[0] = "ng";
    specialConsonantsUni[1] = "ඃ";
    specialConsonants[1] = "h/g";
    specialConsonantsUni[2] = "ඞ";
    specialConsonants[2] = "N/g";
    specialConsonantsUni[3] = "ඍ";
    specialConsonants[3] = "R/g";
    // special characher Repaya
    specialConsonantsUni[4] = "ර්" + "\u200D";
    specialConsonants[4] = "/R/g";
    specialConsonantsUni[5] = "ර්" + "\u200D";
    specialConsonants[5] = "/\\r/g";
}
public static void main(String args[]){

    for (int i=0; i < specialConsonants.length; i++){
        text = text.replace(specialConsonants[i], specialConsonantsUni[i]);
        System.out.println(text);
    }
}

I'm trying to create a locale app. So you may not see some fonts. The error is following.

Exception in thread "main" java.lang.NullPointerException
at java.lang.String.replace(Unknown Source)
at in.isuru.srtuc.Stuff.main(Stuff.java:223)
share|improve this question
it appears that you must call your setSpecial() method to initialize the values of the array – Luiggi Mendoza Feb 26 '12 at 15:57

6 Answers

specialConsonants and specialConsonantsUni are not initilized. You've just defined setSpecial() but not called it before doing replaces

The correct behavior would be:

public static void main(String args[]){
    setSpecial();
    for (int i=0; i < specialConsonants.length; i++){
        text = text.replace(specialConsonants[i], specialConsonantsUni[i]);
        System.out.println(text);
    }
}

note also that setSpecial should be static in that case

Moreover you have to change dimension of specialConsonants to 6

share|improve this answer

Because specialConsonants[i] is null. You have not initialized it.

its like

specialConsonants = {null,null,null,null}

You need to make function setSpecial static then call it before the loop.

share|improve this answer
public static String[] specialConsonants = new String[4];

public void setSpecial(){
    // ...
    specialConsonants[5] = "/\\r/g";
}

specialConsonants is of array size 4. There is no 5th index for it. Also you haven't called the array initializer method.

share|improve this answer

You're adding elements to an already filled array. Array indexes start from 0, When you declared :

 public static String[] specialConsonants = new String[4];

That means you can only use specialConsonants[0] to specialConsonants[3]

I suggest you use a hashmap for this thing.

HashMap<String, String> specialConsonants = new HashMap<String, String>();
....
specialConsonants.put("ං" , "ng" );
....
share|improve this answer

You've declared specialConsonants as an array of length 4, but you're assigning 6 elements to it.

specialConsonants[4] has enough room for elements 0, 1, 2, and 3. You need to declare it as specialConsonants[6] if you want indices 0 - 5.

Then you need to call setSpecial() before you use the array in your loop.

share|improve this answer

the array definition looks wrong, seems you want two same size array but you defined as :

public static String[] specialConsonants = new String[4];
public static String[] specialConsonantsUni = new String[6];

I think it should be:

public static String[] specialConsonants = new String[6];
public static String[] specialConsonantsUni = new String[6];
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.