我正在尝试将输入字符串更改为其 ASCII 代码。该字符串的长度不确定,我需要单独对每个字符代码进行操作。
前几天晚上我可以正常工作,但由于某种原因现在不行了,而且我不明白为什么...我在指定的行处遇到了空指针异常...
这是整个方法。
private void encodeEnableButtonActionPerformed(java.awt.event.ActionEvent evt)
{
String encoded = msgToEncrpt.getText();
int[] text = null;
for (int i=0; i<encoded.length(); i++)
{
text[i] = (int)encoded.charAt(i);//Exception occurs here.
System.out.println(text);
}
}
最佳答案
您正在尝试设置空数组的元素。改变
int[] text = null;
至
int[] text = new int[encoded.length()];
关于java - 将字符串编码为 ASCII 值时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17601297/