1

this is my code:

    for (int i = 0 ;i < starting.Element_N ;i++) {
    String name = starting.AllImages[i];
    File[] files = new File("element/" + name + "/").listFiles();
    for (File file : files) {
        if (file.isFile() == true) {
            try {
            BufferedImage four2 = ImageIO.read(new File("element/" + name + "/" + file.getName()));
            int w = (int)Double.parseDouble(NewImagesSize.getText());
            int h = 100;//(int)((double)four.getHeight()/((double)four.getWidth()/(double)w));
            BufferedImage image_copy = four2;
            four2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = image_copy.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2.drawImage(image_copy, 0, 0, w, h, null);
            g2.dispose();
            File ouptut = new File(file.getName().substring(0,3));
            ImageIO.write(four2, "jpg", ouptut);
            }
            catch (IOException e) {}
        }
            }
}

and these errors showed compiler(netbeans 8):

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at AA.ResizeImage.jButton1ActionPerformed(ResizeImage.java:132)
at AA.ResizeImage.access$000(ResizeImage.java:24)
at AA.ResizeImage$1.actionPerformed(ResizeImage.java:64)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)

...

i want to resize all image in some folder in java. if is there the source of this program , put it. thanks a lot.

online6731
  • 56
  • 7
  • The problem is that you didn't allocate memory for the object on **line 132** at **ResizeImage.java**. The error message says it all. – karlphillip Dec 29 '14 at 11:36
  • 132:Graphics2D g2 = image_copy.createGraphics(); - how can do this work? – online6731 Dec 29 '14 at 11:45
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – MihaiC Dec 29 '14 at 12:02
  • Check if `ImageIO.read(new File("element/" + name + "/" + file.getName()))` is returning anything or returning null. As that value is assigned to `four2` and `four2` is assigned to `image_copy` on which the function returns the exception. – Playmaker Dec 29 '14 at 12:04

0 Answers0