1

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));
3
  • how strArray looks before parsing? Commented May 9, 2015 at 15:37
  • I think you need to make sure that the num value is formatted like this [1,2,3] and also could you print out the strArray value after you splitter the num variable? Commented May 9, 2015 at 15:37
  • Well, it looks like I found one of my problems. the number was not split into strArray. It was only placed into it, so it only has one spot and contains the entire number. Really did not think about testing that first :'( Thanks Sasha and kucing. Is there any way to split a normal number like "100" into an array like [1,0,0]? Or should I open another question for that? (I am new here) Commented May 10, 2015 at 11:59

2 Answers 2

4

This happens because in java you have to initialize a variabile before using it. In your case when you do it:

intArray[i] = Integer.parseInt(strArray[i]); //I GET THE ERROR HERE

the initArray is null.

Change your code:

strArray = num.replaceAll("\\[", "").replaceAll("\\]", "").split(",");

if (strArray != null){
   //You have to initialize your variable
   intArray = new int[strArray.length];

   for (int i = 0; i < strArray.length; i++) {
     try {
        intArray[i] = Integer.parseInt(strArray[i]);
     } catch (NumberFormatException nfe) {
          nfe.printStackTrace();
     }; 
   }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I don't think that the check for strArray != null is necessary here, because String#split won't return null.
indeed, I think it should be if(strArray.length > 0)
@Vinze Even that is not necessary, because that array will always contain at least one element (the whole source string, if no split was applied).
@Vinze No worries, that happens sometimes :).
@Vinze It is not important if the strArray is empty. In any case the for cycle will not give an npe.
|
2

You have to instantiate intArray first.

intArray = new int[strArray.length];

So you will have:

String num = ftxfOld.getText();
strArray = num.replaceAll("\\[", "").replaceAll("\\]", "").split(",");
intArray = new int[strArray.length];

for (int i = 0; i < strArray.length; i++) {
try {
  intArray[i] = Integer.parseInt(strArray[i]); //I GET THE ERROR HERE
 } catch (NumberFormatException nfe) {}; 
}

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.