简体   繁体   中英

Complex String Conversion to integer

I am trying to convert an array that holds string values into integer values, I have used the parseInt function but get an error message (java.lang.NullPointerException) at the point of execution. My code snippet is below

public int NumberOfColumns;
public int[][]CMax,CMin;

for (int i = 1; i < datas.length; i++)
     {
         for (int j = 1; j < NumberOfColumns+1; j++)
         {
           CMax[i][j] = Integer.parseInt(datas[i][j]);
           System.out.println(CMax[i][j]);
           System.out.println(datas[i][j]);
         }
     } 

I have a feeling that I am getting this error message because my data array contains some characters like "-". Examples of some values stored in my data array include 0-31, 8-*, <4-7>. How can I successfully resolve this problem? Also, I need help on how to extract only the integer numbers out of these strings (eg 0-31). I will like to store the upper limit (eg 31) into a separate integer array and the lower limit(eg 0) into another integer array.

Below is an extract of some of the values in my data array.

<0-15> <0-7>
<0-15> <0-7>
<0-15> <0-7>
<32-47> <0-7>
<32-47> <0-7>

As pointed in the comments, the only possibilities for a NPE is one of the below ones are null.

  • datas
  • datas[i]
  • CMax
  • CMax[i]
  • NumberOfColumns

Hopefully you can get the program corrected by looking at those.

Note: Java array index starts with 0, not 1.

My guess would be that datas[datas.length-1][NumberOfColumns] == null.

A little advice: iterate your indexes from 0 to N-1 (inclusive), and not from 1 to N.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM