2

I'm working on writing a method for my discord bot that writes all the member data to a csv` file.

However, I'm struggling to do so using BeanToCsv. The only output to the file is the headers and nothing else. I am able to get each field from each object as seen with the prints, each field is a String.

I previously used a CSVWriter which worked, but opted for the ability to write object data instead.

public static void generateNewCSV(GuildMessageReceivedEvent event) throws IOException, CsvDataTypeMismatchException, CsvRequiredFieldEmptyException
    {
        Writer writer = new FileWriter(MEMBER_DATA_FILE_PATH);

        List<User> users = new ArrayList<User>();

        for(Member mem : event.getGuild().getMembers()) {
            users.add(new User("\"" + mem.getUser().getId() + "\"", mem.getUser().getName(), mem.getEffectiveName(), mem.getRoles().toString()));
            //          System.out.println("id : " + users.get(users.size()-1).getUserId());
//          System.out.println("name : " + users.get(users.size()-1).getUserName());
//          System.out.println("nickname : " + users.get(users.size()-1).getUserNickname());
//          System.out.println("roles : " + users.get(users.size()-1).GetUserRoles());
//          System.out.println( "=============================");
        }

        ColumnPositionMappingStrategy<User> strategy = new ColumnPositionMappingStrategy<User>();
        strategy.setType(User.class);
        String[] columns = {"id", "name", "nickname", "roles"};
        strategy.setColumnMapping(columns);

        StatefulBeanToCsv<User> beanToCsv = new StatefulBeanToCsvBuilder<User>(writer)
                .build(); 


        beanToCsv.write(users);
        writer.close();
        users.clear();
        System.out.println("save complete");
    }

The only output to the csv file is the given headers, but nothing else. It is supposed to write all the information obtained from the created user object.

SirFartALot
  • 1,215
  • 5
  • 25
Eclipse
  • 21
  • 3
  • 1
    Where is your `strategy` used? Shouldn't there be any `withMappingStrategy(MappingStrategy mappingStrategy)` somewhere near your `StatefulBeanToCsvBuilder`? – SirFartALot Jun 28 '19 at 08:03
  • Correct, it can be added by doing StatefulBeanToCsv beanToCsv = new StatefulBeanToCsvBuilder(writer).withMappingStrategy(strategy).build(); but for whatever reason it wont actually write anything then. No header either. – Eclipse Jun 28 '19 at 20:12
  • 1
    Nevermind, I solved my problem. I never made the bean serializable. – Eclipse Jun 28 '19 at 20:52
  • @Eclipse how did you solve this? – techmagister Oct 11 '22 at 05:05
  • @techmagister based on my comment from 3 years ago, I believe I just made the object serializable. By implementing the interface iirc. – Eclipse Oct 12 '22 at 16:04

0 Answers0