3

I am creating a CSV file using BufferedWriter from a ResultSet object. A few of the database columns hold null values.

When I call obj.write(rs.get()) I am getting a NullPointerException when the output is null. Is there any other class other than BufferedWriter which I can use to overcome this.

I do not want to check for null condition on the database columns since there is large number of columns. I am new to Java and tried searching for solution but could not find anything.

Jasper
  • 2,166
  • 4
  • 30
  • 50
NewBee
  • 228
  • 4
  • 14
  • What would you want it to do with null values anyway? I'm surprised it's throwing an exception though - could you give a more realistic code sample, given that `ResultSet` doesn't have just a `get` method? – Jon Skeet Nov 22 '11 at 13:05
  • 1
    Problem is not with bufferedwritter, Null exception is there because of ResultSet object :). And it's java so you have no option other then checking for null. If it is null than write some null related information on csv and if not write data on csv – TeaCupApp Nov 22 '11 at 13:06
  • writer.write(resultSet.getString(1)); writer.write(resultSet.getString(2)); writer.write(resultSet.getString(3)); writer.write(resultSet.getString(4)); writer.write(resultSet.getString(5)); writer.write(resultSet.getString(6)); This is my sample. The resultset obj is not null. When I try to print the value everythig is printing on the screen. I tried calling writer.write(null) to confirm, which throws null pointer exception. – NewBee Nov 22 '11 at 13:18
  • just to be precise, time spent in checking for null value is not dependent on the size of the pointed elements. – D. Cannone Nov 22 '11 at 13:19
  • I can understand that I cant write a null to a file. Is there any other class which I can use which will be more sophisticated than this bw to create a csv file. – NewBee Nov 22 '11 at 13:25
  • Thanks all for your comments. I will proceed with checking null condition. – NewBee Nov 22 '11 at 13:30

3 Answers3

5

You can use a cleverer Buffered writer that performs the nullity check. Here is a code sample that will probably need to be tweaked a bit, I don't currently have a Java compiler available.

public class MyBufferedWriter extends BufferedWriter {
    public void write(String str) {
        if (str==null)
            str="";
        super.write(str);
    }
}
solendil
  • 8,432
  • 4
  • 28
  • 29
0

I know this is an old question, but here is a suggestion anyway: Use OpenCSV. The class you want to use is au.com.bytecode.opencsv.CSVWriter

  • OpenCSV is good API. There is also a feature to CSVWriter so you can pass the ResultSet argument directly to writeAll(). See http://opencsv.sourceforge.net/#sql-integration – CodeMonkey Oct 19 '12 at 21:02
0

Just solendil's code, fixed so that it compiles, and formatted a bit:

package mypackage;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Writer;

public class MyBufferedWriter extends BufferedWriter {

    public MyBufferedWriter(Writer out) {
        super(out);
    }

    @Override
    public void write(String str) throws IOException {
        if (str == null)
            str = "";
        super.write(str);
    }
}
Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373