0

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

3
Contributors
15
Replies
16
Views
6 Years
Discussion Span
Last Post by JamesCherrill
Featured Replies
  • [QUOTE=JamesCherrill;1700277]The error message gives the line number where the error happened. Which line is it?[/QUOTE] line 22 Read More

0

Use i < j in your for loop.

thanks for the post still getting the same error though

1

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

line 22

0

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

Edited by thines01: clarity

0

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

0

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)

0

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

the size is 6

0

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

Edited by thines01: n/a

0

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.

0

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

0

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.

0

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

0

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)));
      }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.