You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Walter Rugora <mi...@sudus.eu> on 2011/05/28 12:40:56 UTC

Suddenly byte-array returns NULL

Hi there,

so far I never had issues to read out of an array of bytes to display an
image. Within the entity class the image field is specified as:
    @Lob
    private byte[] image;

I read and write an image via:
public Image getImage() {
        Data.EM.refresh(this);
        InputStream in = new ByteArrayInputStream(this.image);
        BufferedImage bu = null;
        try {
            bu = javax.imageio.ImageIO.read(in);
        } catch (Exception e) {
        }

        return bu;
    }

    public void setImage(Image image) {
        try {
            BufferedImage bu = new BufferedImage(image.getWidth(null),
image.getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = bu.createGraphics();
            g2.drawImage(image, 0, 0, null);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(bu, "png", baos);
            this.image = baos.toByteArray();
            baos.close();
        } catch (IOException ex) {

Logger.getLogger(Imagereference.class.getName()).log(Level.SEVERE, null,
ex);
        }
    }

What is disturbing is that I suddenly cannot read the image from the
derby database via OpenJPA. What I get in return as soon as I invoke
getImage is an exception (Exception in thread "AWT-EventQueue-0"
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0). When debugging
it shows that getImage (this.image is NULL) returns Null. However, when
I read out the data of the image column in the database table it is
clearly not null but filled with an array of bytes. Btw. all other
fields can be accessed without any problem via OpenJPA.

Did someone experienced a similar problem?

Thanks,
Walter


Re: Suddenly byte-array returns NULL

Posted by Rick Curtis <cu...@gmail.com>.
Walter -

So this worked at some point, but now you can't get it working on 2.1 and
1.2? Perhaps you could try a different version of Derby?

If all else fails, can you come up with a unit test for this issue?

On Wed, Jun 1, 2011 at 2:21 AM, Walter Rogura <mi...@sudus.eu> wrote:

> Dear All,
>
> did anyone experiences the same problem? Or does anyone have a slight
> idea where the problem is?
>
> Please let me know if I should provide you with further information?
>
> Thanks,
> Walter
>
> On Sun, 2011-05-29 at 12:51 +1000, Walter Rugora wrote:
> > Dear Rick,
> >
> > Thanks for your response!!
> >
> > It is OpenJPA 2.1 and derby version 10.3.2.1 (comes with javadb on
> > Ubuntu). But I have to admit that the project was running on OpenJPA
> > 1.2.x once and that a different derby version was used as well.
> > I now did a consistency check against all tables with derby ij, as well
> > I compressed them all ... all good.
> > Unfortunately I cannot recall if the image-field issue appeared as soon
> > as I swap OpenJPA version or database. But I just tried it with OpenJPA
> > 1.2 again and NULL is still returned for the image. Beside the image
> > member of that entity class I have other members and they all get read
> > out properly.
> >
> > Appreciate any kind of help!!
> >
> > Thanks,
> > Walter
> >
> > On Sat, 2011-05-28 at 07:10 -0500, Rick Curtis wrote:
> > > What version of OpenJPA and what DB are you running against?
> > >
> > > On Sat, May 28, 2011 at 5:40 AM, Walter Rugora <mi...@sudus.eu> wrote:
> > >
> > > > Hi there,
> > > >
> > > > so far I never had issues to read out of an array of bytes to display
> an
> > > > image. Within the entity class the image field is specified as:
> > > >    @Lob
> > > >    private byte[] image;
> > > >
> > > > I read and write an image via:
> > > > public Image getImage() {
> > > >        Data.EM.refresh(this);
> > > >        InputStream in = new ByteArrayInputStream(this.image);
> > > >        BufferedImage bu = null;
> > > >        try {
> > > >            bu = javax.imageio.ImageIO.read(in);
> > > >        } catch (Exception e) {
> > > >        }
> > > >
> > > >        return bu;
> > > >    }
> > > >
> > > >    public void setImage(Image image) {
> > > >        try {
> > > >            BufferedImage bu = new BufferedImage(image.getWidth(null),
> > > > image.getHeight(null), BufferedImage.TYPE_INT_RGB);
> > > >            Graphics2D g2 = bu.createGraphics();
> > > >            g2.drawImage(image, 0, 0, null);
> > > >            ByteArrayOutputStream baos = new ByteArrayOutputStream();
> > > >            ImageIO.write(bu, "png", baos);
> > > >            this.image = baos.toByteArray();
> > > >            baos.close();
> > > >        } catch (IOException ex) {
> > > >
> > > > Logger.getLogger(Imagereference.class.getName()).log(Level.SEVERE,
> null,
> > > > ex);
> > > >        }
> > > >    }
> > > >
> > > > What is disturbing is that I suddenly cannot read the image from the
> > > > derby database via OpenJPA. What I get in return as soon as I invoke
> > > > getImage is an exception (Exception in thread "AWT-EventQueue-0"
> > > > java.lang.IndexOutOfBoundsException: Index: 0, Size: 0). When
> debugging
> > > > it shows that getImage (this.image is NULL) returns Null. However,
> when
> > > > I read out the data of the image column in the database table it is
> > > > clearly not null but filled with an array of bytes. Btw. all other
> > > > fields can be accessed without any problem via OpenJPA.
> > > >
> > > > Did someone experienced a similar problem?
> > > >
> > > > Thanks,
> > > > Walter
> > > >
> > > >
> > >
> > >
> >
> >
>
>
>


-- 
*Rick Curtis*

Re: Suddenly byte-array returns NULL

Posted by Walter Rogura <mi...@sudus.eu>.
Dear All,

did anyone experiences the same problem? Or does anyone have a slight
idea where the problem is?

Please let me know if I should provide you with further information?

Thanks,
Walter

On Sun, 2011-05-29 at 12:51 +1000, Walter Rugora wrote:
> Dear Rick,
> 
> Thanks for your response!!
> 
> It is OpenJPA 2.1 and derby version 10.3.2.1 (comes with javadb on
> Ubuntu). But I have to admit that the project was running on OpenJPA
> 1.2.x once and that a different derby version was used as well.
> I now did a consistency check against all tables with derby ij, as well
> I compressed them all ... all good.
> Unfortunately I cannot recall if the image-field issue appeared as soon
> as I swap OpenJPA version or database. But I just tried it with OpenJPA
> 1.2 again and NULL is still returned for the image. Beside the image
> member of that entity class I have other members and they all get read
> out properly.
> 
> Appreciate any kind of help!!
> 
> Thanks,
> Walter
> 
> On Sat, 2011-05-28 at 07:10 -0500, Rick Curtis wrote:
> > What version of OpenJPA and what DB are you running against?
> > 
> > On Sat, May 28, 2011 at 5:40 AM, Walter Rugora <mi...@sudus.eu> wrote:
> > 
> > > Hi there,
> > >
> > > so far I never had issues to read out of an array of bytes to display an
> > > image. Within the entity class the image field is specified as:
> > >    @Lob
> > >    private byte[] image;
> > >
> > > I read and write an image via:
> > > public Image getImage() {
> > >        Data.EM.refresh(this);
> > >        InputStream in = new ByteArrayInputStream(this.image);
> > >        BufferedImage bu = null;
> > >        try {
> > >            bu = javax.imageio.ImageIO.read(in);
> > >        } catch (Exception e) {
> > >        }
> > >
> > >        return bu;
> > >    }
> > >
> > >    public void setImage(Image image) {
> > >        try {
> > >            BufferedImage bu = new BufferedImage(image.getWidth(null),
> > > image.getHeight(null), BufferedImage.TYPE_INT_RGB);
> > >            Graphics2D g2 = bu.createGraphics();
> > >            g2.drawImage(image, 0, 0, null);
> > >            ByteArrayOutputStream baos = new ByteArrayOutputStream();
> > >            ImageIO.write(bu, "png", baos);
> > >            this.image = baos.toByteArray();
> > >            baos.close();
> > >        } catch (IOException ex) {
> > >
> > > Logger.getLogger(Imagereference.class.getName()).log(Level.SEVERE, null,
> > > ex);
> > >        }
> > >    }
> > >
> > > What is disturbing is that I suddenly cannot read the image from the
> > > derby database via OpenJPA. What I get in return as soon as I invoke
> > > getImage is an exception (Exception in thread "AWT-EventQueue-0"
> > > java.lang.IndexOutOfBoundsException: Index: 0, Size: 0). When debugging
> > > it shows that getImage (this.image is NULL) returns Null. However, when
> > > I read out the data of the image column in the database table it is
> > > clearly not null but filled with an array of bytes. Btw. all other
> > > fields can be accessed without any problem via OpenJPA.
> > >
> > > Did someone experienced a similar problem?
> > >
> > > Thanks,
> > > Walter
> > >
> > >
> > 
> > 
> 
> 



Re: Suddenly byte-array returns NULL

Posted by Walter Rugora <mi...@sudus.eu>.
Dear Rick,

Thanks for your response!!

It is OpenJPA 2.1 and derby version 10.3.2.1 (comes with javadb on
Ubuntu). But I have to admit that the project was running on OpenJPA
1.2.x once and that a different derby version was used as well.
I now did a consistency check against all tables with derby ij, as well
I compressed them all ... all good.
Unfortunately I cannot recall if the image-field issue appeared as soon
as I swap OpenJPA version or database. But I just tried it with OpenJPA
1.2 again and NULL is still returned for the image. Beside the image
member of that entity class I have other members and they all get read
out properly.

Appreciate any kind of help!!

Thanks,
Walter

On Sat, 2011-05-28 at 07:10 -0500, Rick Curtis wrote:
> What version of OpenJPA and what DB are you running against?
> 
> On Sat, May 28, 2011 at 5:40 AM, Walter Rugora <mi...@sudus.eu> wrote:
> 
> > Hi there,
> >
> > so far I never had issues to read out of an array of bytes to display an
> > image. Within the entity class the image field is specified as:
> >    @Lob
> >    private byte[] image;
> >
> > I read and write an image via:
> > public Image getImage() {
> >        Data.EM.refresh(this);
> >        InputStream in = new ByteArrayInputStream(this.image);
> >        BufferedImage bu = null;
> >        try {
> >            bu = javax.imageio.ImageIO.read(in);
> >        } catch (Exception e) {
> >        }
> >
> >        return bu;
> >    }
> >
> >    public void setImage(Image image) {
> >        try {
> >            BufferedImage bu = new BufferedImage(image.getWidth(null),
> > image.getHeight(null), BufferedImage.TYPE_INT_RGB);
> >            Graphics2D g2 = bu.createGraphics();
> >            g2.drawImage(image, 0, 0, null);
> >            ByteArrayOutputStream baos = new ByteArrayOutputStream();
> >            ImageIO.write(bu, "png", baos);
> >            this.image = baos.toByteArray();
> >            baos.close();
> >        } catch (IOException ex) {
> >
> > Logger.getLogger(Imagereference.class.getName()).log(Level.SEVERE, null,
> > ex);
> >        }
> >    }
> >
> > What is disturbing is that I suddenly cannot read the image from the
> > derby database via OpenJPA. What I get in return as soon as I invoke
> > getImage is an exception (Exception in thread "AWT-EventQueue-0"
> > java.lang.IndexOutOfBoundsException: Index: 0, Size: 0). When debugging
> > it shows that getImage (this.image is NULL) returns Null. However, when
> > I read out the data of the image column in the database table it is
> > clearly not null but filled with an array of bytes. Btw. all other
> > fields can be accessed without any problem via OpenJPA.
> >
> > Did someone experienced a similar problem?
> >
> > Thanks,
> > Walter
> >
> >
> 
> 



Re: Suddenly byte-array returns NULL

Posted by Rick Curtis <cu...@gmail.com>.
What version of OpenJPA and what DB are you running against?

On Sat, May 28, 2011 at 5:40 AM, Walter Rugora <mi...@sudus.eu> wrote:

> Hi there,
>
> so far I never had issues to read out of an array of bytes to display an
> image. Within the entity class the image field is specified as:
>    @Lob
>    private byte[] image;
>
> I read and write an image via:
> public Image getImage() {
>        Data.EM.refresh(this);
>        InputStream in = new ByteArrayInputStream(this.image);
>        BufferedImage bu = null;
>        try {
>            bu = javax.imageio.ImageIO.read(in);
>        } catch (Exception e) {
>        }
>
>        return bu;
>    }
>
>    public void setImage(Image image) {
>        try {
>            BufferedImage bu = new BufferedImage(image.getWidth(null),
> image.getHeight(null), BufferedImage.TYPE_INT_RGB);
>            Graphics2D g2 = bu.createGraphics();
>            g2.drawImage(image, 0, 0, null);
>            ByteArrayOutputStream baos = new ByteArrayOutputStream();
>            ImageIO.write(bu, "png", baos);
>            this.image = baos.toByteArray();
>            baos.close();
>        } catch (IOException ex) {
>
> Logger.getLogger(Imagereference.class.getName()).log(Level.SEVERE, null,
> ex);
>        }
>    }
>
> What is disturbing is that I suddenly cannot read the image from the
> derby database via OpenJPA. What I get in return as soon as I invoke
> getImage is an exception (Exception in thread "AWT-EventQueue-0"
> java.lang.IndexOutOfBoundsException: Index: 0, Size: 0). When debugging
> it shows that getImage (this.image is NULL) returns Null. However, when
> I read out the data of the image column in the database table it is
> clearly not null but filled with an array of bytes. Btw. all other
> fields can be accessed without any problem via OpenJPA.
>
> Did someone experienced a similar problem?
>
> Thanks,
> Walter
>
>


-- 
*Rick Curtis*