-4

First I should lead with how very inexperienced I am with this language. Everything I've tried has blown up. It doesn't seem to like .replace or .replaceAll anyway I've tried to use it. I've used Pattern and Matcher. no go.

I need to place this code in an already work class that is reading a ResultSet from a query and it's sending it to a .csv file. Here is the code I'm working with. My latest try you'll see between the comments //All of Nikki's mess and //end of Nikki's mess. I'm really going out of my mind if someone could help shed some light. Just seems like such a simple thing but the language barrier is real!!

public class CSVUtil {

public static final String QUOTE = "\"";
public static final String DELIMETER = ",";

public static String convertToCsv(ResultSet rs) throws SQLException {
    StringBuilder csv = new StringBuilder();

    while (rs.next()) {

        // all Nikki's mess
        // between the comments
        
        String comments = rs.getString("ImageAnnotation");
        //Pattern pt = Pattern.compile(",");
        //Matcher match = pt.matcher(comments);
        //while(match.find()){
            comments = comments.replaceAll(",","");
        //}
        System.out.println(comments);
        
        //end of Nikki's mess

        csv.append(QUOTE).append(rs.getString(1)).append(QUOTE);

        for (int i = 2; i <= rs.getMetaData().getColumnCount(); i++) {
            csv.append(DELIMETER).append(QUOTE).append(rs.getString(i) == null ? "": rs.getString(i)).append(QUOTE);

        }

        csv.append(System.lineSeparator());
    }
    return csv.toString();
}

}

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
nzabloc
  • 1
  • 1
  • I was hesitant about posting here. it's a tough crowd. I've read sooooo many questions about solving this and none of them are working so I thought there was something more specific about it being done in AWS Lambda java, hence the title including it. Apologies for my inexperience. The only error log I have is this: – nzabloc Feb 12 '21 at 17:39
  • "errorMessage": "java.lang.NullPointerException", "errorType": "java.lang.NullPointerException", "stackTrace": [ "com.sgk.redshift.ingestion.CSVUtil.convertToCsv(CSVUtil.java:54)", "com.sgk.redshift.ingestion.RdsDataService.exportBMRDataTo(RdsDataService.java:80)", "com.sgk.redshift.ingestion.LambdaFunctionHandler.handleRequest(LambdaFunctionHandler.java:27)", "com.sgk.redshift.ingestion.LambdaFunctionHandler.handleRequest(LambdaFunctionHandler.java:18)" – nzabloc Feb 12 '21 at 17:41
  • Doesn't really tell me anything other than my line 54 is where the error is occurring. and that is this line comments = comments.replaceAll(",",""); – nzabloc Feb 12 '21 at 17:42
  • thanks - I've already read this explanation. – nzabloc Feb 12 '21 at 17:58

1 Answers1

0

I've been able to resolve. I added the replaceAll into this line:

csv.append(DELIMETER).append(QUOTE).append(rs.getString(i) == null ? "": rs.getString(i)).append(QUOTE);

csv.append(DELIMETER).append(QUOTE).append(rs.getString(i) == null ? "": rs.getString(i).replaceAll(",","")).append(QUOTE);

Looks to have done the trick.

nzabloc
  • 1
  • 1
  • How are you building your Lambda Java function - are you building it via Eclipse tool or using the Java runtime API in a Maven project (which is better way). – smac2020 Feb 12 '21 at 23:23