简体   繁体   中英

How do I convert a JTextFeild to an Integer array?

Code:

char[] charData;
int[] data;    
JTextField message = new JTextField(60);
charData = message.getText().toUpperCase().toCharArray();
for(int i = 0; i < charData.length; i++){
    data[i] = (int) charData[i];             //line 62
}
for(int i : data){
    System.out.println(i);
}

I am given this error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at Main$1.actionPerformed(Main.java:62)

Line 62 is data[i] = (int) charData[i];

Can anyone figure it out? I guess we will find out!

You're forgetting to initialize your data variable, so you should move and modify its declaration to:

charData = message.getText().toUpperCase().toCharArray();
int[] data = new int[charData.length];

Now data will be initialized to an empty array of equal size to your array of characters.

However, of note is since you are leaving data uninitialized, your program should not be able to compile. Are you sure you are compiling successfully, and that this is the most up-to-date version of the code?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM