Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
123 views
by Contributing Tester (92 points)
retagged by

Getting NPE while using getChars method.

Code

public class GetCharsDemo {
    public static void main(String[] args) {

        String temp = "Some string!";
        int from = 5;
        int to = 9;
        char[] store = null;

        temp.getChars(from, to, store, 1);

        System.out.println("Character array - " + store[0]
            + store[1] + store[2] + store[3]);
        
        //System.out.println(temp.substring(from));

    }

Output -

Exception in thread "main" java.lang.NullPointerException
    at java.lang.System.arraycopy(Native Method)
    at java.lang.String.getChars(Unknown Source)
    at com.dataTypes.GetCharsDemo.main(GetCharsDemo.java:11)

Values should store to char array named store.

Please help here.

1 Answer

0 like 0 dislike
by The go-to Tester (181 points)

You have declared char, but you have not defined it.

You need to use

 char[] store =new char[4];


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...