hi

I came up with this code to help me convert a string into an integer array but i keep getting a null exception when i run it and i am not sure how to fix it.

import java.io.*;
import java.lang.reflect.Array;

public class test {

	static String message;
	static int[] arr;
	static int j;
	static String[] arr2;
	 
	public static void main(String[] args) {
	
		message = "123456";	
		
		
		arr2 = new String [] {message};
		j = message.length();
	
		for (int i=0; i<=arr2.length; i++){
			
			arr[i] = Integer.parseInt(arr2[i]);
			
		}
		
		
		System.out.println(arr[1]);
		System.out.println(arr[2]);

	}

}

Thanks

Use i < j in your for loop.

Use i < j in your for loop.

thanks for the post still getting the same error though

The error message gives the line number where the error happened. Which line is it?

The error message gives the line number where the error happened. Which line is it?

line 22

Good suggestion JamesCherrill.
Also danielagaba, what "size" or "length" is the array built on line 17 when it is created?

That would be length 1
danielagaba: Can you please post the complete error message - this looks funny to me. I would expect an array bounds exception, not a null exception

That would be length 1
danielagaba: Can you please post the complete error message - this looks funny to me. I would expect an array bounds exception, not a null exception

Exception in thread "main" java.lang.NullPointerException
at test.main(test.java:21)

Good suggestion JamesCherrill.
Also danielagaba, what "size" or "length" is the array built on line 17 when it is created?

the size is 6

Really? arr2.length is six (6) ?
What would happen if you found out it was one (1)?

i tested individual elements of the code and found that the problem comes from

arr2 = new String [] {message};

when i try to print arr2[1], arr2[2], etc. it brings an indexoutofbounds exception.

Really? arr2.length is six (6) ?
What would happen if you found out it was one (1)?

yeah. turned out it was, changed it to j = message.length(); (which is 6) but still get the same problem

Please check again.
arr2 is an array of 1 element. length 1.
That one element arr2[0] contains a String of length 6.
The length of the array is not the same thing as the length of a single String arrayelement.

Please check again.
arr2 is an array of 1 element. length 1.
That one element arr2[0] contains a String of length 6.
The length of the array is not the same thing as the length of a single String arrayelement.

i figured the issue is with

arr2 = new String [] {message};

then the conversion to an integer array

I would suggest converting the original string directly into an integer array ( >>>of appropriate size <<<) with something like this:

// convert each character in message to an int (using String to int converter)
      for(int i=0; i< intLen; i++)
      {
         int_arr[i] = Integer.parseInt(Character.toString(message.charAt(i)));
      }

yes, or more simply...

int_arr[i] = message.charAt(i) - '0';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.