I am a beginner in programming and decided to make my own binary to decimal converter program for the fun of it. In my program, I move the String array's content to the Int array. the problem is that I seem to keep getting a NullPointerException error at the code where I change the String to Int. I have read the error and tried a lot of different methods to get rid of that error, but nothing helps. What could I be doing wrong?
"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"
My code:
int ans = 0;
int multi = 0;
strArray = null;
intArray = null;
if (rbBin.isSelected()) {
txaNew.setText("");
String num = ftxfOld.getText();
strArray = num.replaceAll("\\[", "").replaceAll("\\]", "").split(",");
for (int i = 0; i < strArray.length; i++) {
try {
intArray[i] = Integer.parseInt(strArray[i]); //I GET THE ERROR HERE
} catch (NumberFormatException nfe) {
}
}
for (int j = 0; j < num.length() + 1; j++) {
multi = intArray[j] * 2 ^ j;
ans = ans + multi;
}
}
txaNew.append(Integer.toString(ans));
strArraylooks before parsing?numvalue is formatted like this[1,2,3]and also could you print out thestrArrayvalue after you splitter thenumvariable?