You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by tazan007 <ta...@gmail.com> on 2009/07/31 21:46:08 UTC

Null pointer error with ReflectData.getProtocol

Hi guys, I am experimenting with avro and trying to generate a protocol from
an existing class.  As as test, I created this class:

public class Hello {
    public void hello(String name)
    {
        System.out.println("Hello " +name);
    }
}

I am using the following method to create the procotol:

        ReflectData.getProtocol(Hello.class);

And here is the exception I am getting:

Exception in thread "main" java.lang.NullPointerException
    at
org.apache.avro.reflect.ReflectData.createSchema(ReflectData.java:153)
    at
org.apache.avro.reflect.ReflectData.createSchema(ReflectData.java:183)
    at org.apache.avro.reflect.ReflectData.getMessage(ReflectData.java:228)
    at org.apache.avro.reflect.ReflectData.getProtocol(ReflectData.java:204)
    at
com.ask.tiburon.frill.test.AvroProtocolGenerator.main(AvroProtocolGenerator.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)

I am using the avro 1.0 jars/source.

Any ideas to what I am doing wrong would be appreciated.  I do have
PARANAMER being run and the following code does show the parameter names:

         try
        {
            Method[] methods = Hello.class.getMethods();
            Paranamer paranamer = new CachingParanamer();

            for(Method method: methods)
            {
                String[] parameterNames =
paranamer.lookupParameterNames(method);
                System.out.println(method.getName() + " : ");
                for(String name : parameterNames)
                {
                    System.out.println("\t" + name);
                }
            }

        } catch (Exception e)
        {
            System.out.println("Exception: " + e.getMessage());
        }

Thanks,
Hiral

Re: Null pointer error with ReflectData.getProtocol

Posted by Doug Cutting <cu...@apache.org>.
tazan007 wrote:
>     public class MyString
>     {
> *        public String string;*


That's https://issues.apache.org/jira/browse/AVRO-80.  String is not 
currently used to represent Avro strings, Utf8 is.  Yes, that's not very 
friendly, and the error message is horrible, but it is a lot faster than 
java.lang.String.  We should either make java.lang.String work too or at 
least generate a better error message.

> --- a/trunk/src/java/org/apache/avro/reflect/ReflectData.java
> +++ b/trunk/src/java/org/apache/avro/reflect/ReflectData.java
> @@ -123,6 +123,8 @@ public class ReflectData {
>                                       Map<String,Schema> names) {
>      if (type == Utf8.class)
>        return Schema.create(Type.STRING);
> +    else if (type == String.class)
> +      return Schema.create(Type.STRING);
>      else if (type == ByteBuffer.class)
>        return Schema.create(Type.BYTES);
>      else if ((type == Integer.class) || (type == Integer.TYPE))

That's sufficient to generate the schema, but don't think it will work 
when you attempt to read or write the record.  So fixing AVRO-80 will 
take a bit more work than that.

Doug

Re: Null pointer error with ReflectData.getProtocol

Posted by tazan007 <ta...@gmail.com>.
Hi Doug, it seems the getSchema method is calling getPackage().getName() on
for example the java String class and that is what's failing.  I am
providing a package for my class.

The following generates a protocol without errors:

public class HelloWorld {
    public class MyString
    {
    }
    public void hello(MyString name)
    {
        System.out.println("test");
    }
}

But if I add the following field, it fails with the same error:

public class HelloWorld {
    public class MyString
    {
*        public String string;*
    }
    public void hello(MyString name)
    {
        System.out.println("test");
    }
}

Exception in thread "main" java.lang.NullPointerException
    at
org.apache.avro.reflect.ReflectData.createSchema(ReflectData.java:153)
    at
org.apache.avro.reflect.ReflectData.createSchema(ReflectData.java:183)
    at
org.apache.avro.reflect.ReflectData.createSchema(ReflectData.java:183)
    at org.apache.avro.reflect.ReflectData.getMessage(ReflectData.java:228)
    at org.apache.avro.reflect.ReflectData.getProtocol(ReflectData.java:204)

The following seems to fix it:

--- a/trunk/src/java/org/apache/avro/reflect/ReflectData.java
+++ b/trunk/src/java/org/apache/avro/reflect/ReflectData.java
@@ -123,6 +123,8 @@ public class ReflectData {
                                      Map<String,Schema> names) {
     if (type == Utf8.class)
       return Schema.create(Type.STRING);
+    else if (type == String.class)
+      return Schema.create(Type.STRING);
     else if (type == ByteBuffer.class)
       return Schema.create(Type.BYTES);
     else if ((type == Integer.class) || (type == Integer.TYPE))


Thanks,
Hiral

On Fri, Jul 31, 2009 at 1:38 PM, Moderator <cu...@apache.org> wrote:

> tazan007 wrote:
>
>> Exception in thread "main" java.lang.NullPointerException
>>    at
>> org.apache.avro.reflect.ReflectData.createSchema(ReflectData.java:153)
>>
>
> In the 1.0.0 source, ReflectData.java:153 is:
>
>  String space = c.getPackage().getName();
>
> So it seem that if you don't declare a package the package of a class is
> null, and Avro's reflect code does not check for that.  As a workaround
> you can put your class into a package.
>
> I've created a bug report to track this:
>
>  https://issues.apache.org/jira/browse/AVRO-86
>
> Doug
>
>

Re: Null pointer error with ReflectData.getProtocol

Posted by Moderator <cu...@apache.org>.
tazan007 wrote:
> Exception in thread "main" java.lang.NullPointerException
>     at 
> org.apache.avro.reflect.ReflectData.createSchema(ReflectData.java:153)

In the 1.0.0 source, ReflectData.java:153 is:

   String space = c.getPackage().getName();

So it seem that if you don't declare a package the package of a class is
null, and Avro's reflect code does not check for that.  As a workaround
you can put your class into a package.

I've created a bug report to track this:

   https://issues.apache.org/jira/browse/AVRO-86

Doug