0

I got these 3 errors on my code :

private static void downloadFile(Drive service, String fileId) {
    try {

        OutputStream outputStream = new FileOutputStream("test.csv");
        service.files().export(fileId, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet").executeMediaAndDownloadTo(outputStream);

    } catch (IOException e) {
        // An error occurred.
        e.printStackTrace();
    }
}

here's the error results :

Exception in thread "main" java.lang.NullPointerException at googleBaru.mainClass.downloadFile(mainClass.java:51) at googleBaru.mainClass.main(mainClass.java:60)

I hope anyone can fix my code, or how am I suppose to do to calling the method (with parameter service and fileId)

many thanks, sorry for my bad English

  • Assuming your `Drive` object exists, was initialized properly, etc., another problem is that you are trying to open an output stream at a relative location `test.csv`, which might not make sense. Try using a full path instead. – Tim Biegeleisen Oct 24 '16 at 04:07
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Linda Lawton - DaImTo Oct 24 '16 at 07:04

1 Answers1

0

I got these 3 errors on my code.

Actually, this is a stacktrace ... and it describes ONE error, not three.

The error is on line 51 of your mainclass.java file. That will be this one:

  service.files().export(fileId, 
      "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet").
      executeMediaAndDownloadTo(outputStream);

Assuming that service is supposed to be a Drive object, then looking at this from a Java perspective:

  • the service reference could be null,
  • the files() method could have returned null (impossible I think), or
  • the export() method could have returned null (impossible I think).

The "impossible" cases according to my reading of the Drive API javadocs.

In short, the most plausible explanation is that your service reference is null. Find out why, and fix the cause.


@TimBiegeleisen's comment about the file path may also be correct, but that won't be the cause of the NullPointerException you are seeing.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216