0

Currently, I'm having problem with my work.


List<Map<String,String>> tmp = //blahblahblah//(for my privacy)
        int size = tmp.size();
        String[] linearr = new String[size];
        String[] numarr= new String[size];
        String[] namearr= new String[size];
        String[] datearr= new String[size];
        int i = 0;

for (i = 0; i < size; i++) {
    linearr[i] = (String) tmp.get(i).get("line").toString();
    numarr[i] = (String) tmp.get(i).get("number").toString();
    namearr[i] = (String) tmp.get(i).get("name").toString();
    datearr[i] = (String) tmp.get(i).get("date").toString();
            }

I made a List<Map<String,String>> however when i got to for-loop. InvocationTargetException came up. And I could not debug anymore and cannot track it

I tried the code surrounds with try and catch block with throws InvocationTargetException e but Eclipse told me to remove it.

please help

specifically it said that invocationtargetexception.<init>(throwable) line: not available

Jay
  • 45
  • 1
  • 7
  • Please [edit] your question to add the entire stack trace—see [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/q/3988788/6395627). – Slaw Feb 12 '20 at 06:58

2 Answers2

1

You may find this link helpful. However, I'd like to suggest that you need to make sure that all keys are available in your map.

P.S. You don't need to use toString() and (String) cast.

Mohammed Deifallah
  • 1,290
  • 1
  • 10
  • 25
  • thank you i found it it was the string and timestamp cast error – Jay Feb 12 '20 at 07:29
  • 1
    Congrats!... I recommend answering your question by yourself, then accept it after two days to make it a reference for others. – Mohammed Deifallah Feb 12 '20 at 07:32
  • 1
    @Jay Before answering your own question I recommend you first edit your question to make it answerable. Specifically, you should _create_ and provide a [mre] demonstrating the problem as well as the exact error(s) you're getting, both in the question itself as text (not images). Currently your question gives no indication of any errors related to casting; you only mention an `InvocationTargetException` which is never the real error. Otherwise, consider deleting the question. – Slaw Feb 12 '20 at 07:44
  • @slaw thank you for your commet, I solved the problem. However, since others have invested on my question i could not delete the question – Jay Feb 13 '20 at 04:26
0

You Don't want to use List ? And if you don't have the exact keys in map ,it will throw Exception . Using get or default will prevent it.

List<String> line = new ArrayList<>();
        List<String> number = new ArrayList<>();
        List<String> name = new ArrayList<>();
        List<String> date = new ArrayList<>();
        for(Map<String, String> map:tmp){
           line.add(map.getOrDefault("line",""));
           number.add(map.getOrDefault("number",""));
           name.add(map.getOrDefault("name",""));
           date.add(map.getOrDefault("date",""));
    }
hotaru
  • 19
  • 1
  • 4