0

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)
1
  • it appears that you must call your setSpecial() method to initialize the values of the array Commented Feb 26, 2012 at 15:57

6 Answers 6

1

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

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

Comments

1

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.

Comments

0
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.

Comments

0

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" );
....

Comments

0

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.

Comments

0

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];

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.