Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4260874

DataFlavor.getReaderForText throws NPE

XMLWordPrintable

    • kestrel
    • sparc
    • solaris_2.5.1



      Name: sdC67446 Date: 08/09/99


      The method:
      public Reader getReaderForText(Transferable transferable)
                              throws UnsupportedFlavorException,
                                     IOException
      of class java.awt.datatransfer.DataFlavor
      throws unspecified NullPointerException
      if 'transferable'.getTransferData(..) returns null
      (if DataFlavor.getRepresentationClass() == null).

      Should return null or throw IllegalArgumentException as doc may be
      interpreted:

      > IllegalArgumentException - if the representation class is not
      > java.io.InputStream or java.lang.String

      The doc says:
      --------------------------------------------------
      public Reader getReaderForText(Transferable transferable)
                              throws UnsupportedFlavorException,
                                     IOException

           Gets a reader for an input stream, decoded for the expected
           charset (encoding). This works only if the representation class
           of this flavor is java.io.InputStream (or a subclass).
           Returns:
               a Reader to read the data
           Throws:
               IllegalArgumentException - if the representation class is not
                    java.io.InputStream or java.lang.String
               IllegalArgumentException - if the charset (character
                    encoding) is not supported by the JDK.
               UnsupportedFlavorException - if the transferable does not
                    support this flavor.
               IOException - if the data cannot be read because of an I/O
                    error.

      ---------Test.java--------------------------------
      import java.awt.datatransfer.*;
      import java.io.*;

      public class Test {

          static class FakeTransferable implements Transferable {
              public DataFlavor[] getTransferDataFlavors() {
                  return null;
              }
              public boolean isDataFlavorSupported(DataFlavor flavor) {
                  return false;
              }
              public Object getTransferData(DataFlavor flavor) throws
                  UnsupportedFlavorException, IOException {
                      Class c = flavor.getRepresentationClass();
                      System.out.println("getTransferData: class="+flavor.getRepresentationClass());
                      if (c == null) {
                          return null;
                      } else if (c.equals(java.io.InputStream.class)) {
                          byte[] b = {1, 2, 3};
                          return new java.io.ByteArrayInputStream(b);
                      } else if (c.equals(java.lang.String.class)) {
                          return new String("ABC");
                      } else return new java.lang.Integer(1999);
                  }
          }

          public static void main(String[] args) {
              DataFlavor df = new DataFlavor();
              System.out.println("class="+df.getRepresentationClass());
              FakeTransferable t = new FakeTransferable();
              try {
                  System.out.println(df.getReaderForText(t));
              } catch (IOException e) {
                  e.printStackTrace();
              } catch (UnsupportedFlavorException e) {
                  e.printStackTrace();
              }
          }
      }
      ---------Output-----------------------------------
      Warning: JIT compiler "sunwjit" not found. Will use interpreter.
      class=null
      getTransferData: class=null
      Exception in thread "main" java.lang.NullPointerException: getTransferData() returned null
              at java.awt.datatransfer.DataFlavor.getReaderForText(DataFlavor.java:480)
              at Test.main(Test.java:33)
      --------------------------------------------------
      ======================================================================

            tdv Dmitri Trembovetski (Inactive)
            dsvsunw Dsv Dsv (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: