java.lang.parseInt(UnknownSource) Error when trying to parse and insert rows in JDBC

StackOverflow https://stackoverflow.com/questions/19416628

  •  01-07-2022
  •  | 
  •  

Pergunta

I am trying to parse information from data and insert it into a Database using JDBC. However, I get the java.lang.parseInt(UnknownSource) error when trying to do so. Following is the code that I have put together:

public void testAddStation(String r){   
    long st = System.currentTimeMillis();
    String str = r;
    String row[]= str.split(",");
    Station s = new Station();
    s.setPrimaryID(row[0]);
    s.setSecondaryID(row[1]);
    s.setStationName(row[2]);
    s.setState(row[3]);
    s.setCountry(row[4]);
    s.setLatitude(Double.parseDouble(row[5]));
    s.setLongitude(Double.parseDouble(row[6]));
    s.setElevation(Double.parseDouble(row[7]));
    s.setMesoNetID(row[8]);
    s.setNetworkName(row[9]);
    s.setStatus(row[10]);
    s.setPrimaryProviderId(Integer.parseInt(row[11]));
    s.setPrimaryProvider(row[12]);
    s.setSecondaryProviderId(Integer.parseInt(row[13]));
    s.setSecondaryProvider(row[14]);
    s.setTertiaryProviderId(Integer.parseInt(row[15]));
    s.setTertiaryProvider(row[16]);
    s.setWims_Id(row[17]);
    //System.out.println(s.getStationName());
    //System.out.println(s.getPrimaryID());

    rep.addStation(s);
    N=N+1;
    long et = System.currentTimeMillis();
    System.out.println("total station created: " + N + "\tinsert per row: "+(et - st));
}

And following is the error message I am getting in the console window:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at karki.jdbc.repo.Check.testAddStation(Check.java:40)
at karki.jdbc.repo.CSVFile.readCSV(CSVFile.java:36)
at karki.jdbc.repo.CSVFile.listFiles(CSVFile.java:20)
at karki.jdbc.repo.CSVFile.listStationCSV(CSVFile.java:59)
at karki.jdbc.repo.CSVFile.main(CSVFile.java:82)

Can anyone please shed some light as to what I might be doing wrong here?

Foi útil?

Solução

You should check to make sure the input string is valid, and further, within your code you need to include checks for invalid input data. The relevant part of the exception is this: java.lang.NumberFormatException: For input string: "" I can't tell from the script itself, because I don't know the exact line numbers in your code, but one of the parseInt calls in the lines below is receiving an empty string ("") instead of a valid number input:

s.setPrimaryProviderId(Integer.parseInt(row[11]));
s.setSecondaryProviderId(Integer.parseInt(row[13]));
s.setTertiaryProviderId(Integer.parseInt(row[15]));

You should include checks like this:

if(!"".equals(row[11])){ //If row[11] is not an empty string
    s.setPrimaryProviderId(Integer.parseInt(row[11]));
}
else{
    // Handle the case where this part of the data is empty
}

You should also include these check with the parseDouble lines as well.

Outras dicas

Based on the Exception information, it appears your problem is likely to be in one of the following lines:

s.setPrimaryProviderId(Integer.parseInt(row[11]));
s.setPrimaryProvider(row[12]);
s.setSecondaryProviderId(Integer.parseInt(row[13]));
s.setSecondaryProvider(row[14]);
s.setTertiaryProviderId(Integer.parseInt(row[15]));
s.setTertiaryProvider(row[16]);

Specifically, the ones that have the Integer.parseInt() code in them because the Exception is most likely complaining that row[11] row[13] or row[15] is actually an empty String "" instead of a numeric value.

You could test for valid numeric values there before setting the values, and then place some default Integer value in there if it is non-numeric.

Have you checked your CSV file for validity in all the various fields you wish to parse?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top