Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-14-2007, 02:10 PM
Member
 
Join Date: Nov 2007
Posts: 12
daredavil82 is on a distinguished road
ArrayIndexOutOfBoundsException
can anyone show solve the problem of this code.....

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Picture.main(Picture.java:163)


Code:
import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JLabel; import javax.swing.JMenuItem; import javax.swing.ImageIcon; import javax.swing.KeyStroke; import java.awt.FileDialog; import java.awt.Toolkit; import java.awt.Color; import java.awt.image.BufferedImage; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.io.File; import java.io.IOException; import java.net.URL; public final class Picture implements ActionListener { private BufferedImage image; // the rasterized image private JFrame frame; // on-screen view private String filename; // name of file // create a blank w-by-h image public Picture(int w, int h) { image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); // set to TYPE_INT_ARGB to support transparency filename = w + "-by-" + h; } // create an image by reading in the PNG, GIF, or JPEG from a filename public Picture(String filename) { this.filename = filename; try { // try to read from file in working directory File file = new File(filename); if (file.isFile()) { image = ImageIO.read(file); } // now try to read from file in same directory as this .class file else { URL url = getClass().getResource(filename); if (url == null) { url = new URL(filename); } image = ImageIO.read(url); } } catch (IOException e) { // e.printStackTrace(); throw new RuntimeException("Could not open file: " + filename); } // check that image was read in if (image == null) { throw new RuntimeException("Invalid image file: " + filename); } } // create an image by reading in the PNG, GIF, or JPEG from a file public Picture(File file) { try { image = ImageIO.read(file); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("Could not open file: " + file); } if (image == null) { throw new RuntimeException("Invalid image file: " + file); } } // to embed in a JPanel, JFrame or other GUI widget public JLabel getJLabel() { if (image == null) { return null; } // no image available ImageIcon icon = new ImageIcon(image); return new JLabel(icon); } // view on-screen, creating new frame if necessary public void show() { // create the GUI for viewing the image if needed if (frame == null) { frame = new JFrame(); JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("File"); menuBar.add(menu); JMenuItem menuItem1 = new JMenuItem(" Save... "); menuItem1.addActionListener(this); menuItem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())); menu.add(menuItem1); frame.setJMenuBar(menuBar); frame.setContentPane(getJLabel()); // f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setTitle(filename); frame.setResizable(false); frame.pack(); frame.setVisible(true); } // draw frame.repaint(); } // accessor methods public int height() { return image.getHeight(null); } public int width() { return image.getWidth(null); } // return Color of pixel (i, j) public Color get(int i, int j) { return new Color(image.getRGB(i, j)); } // change color of pixel (i, j) to c public void set(int i, int j, Color c) { if (c == null) { throw new RuntimeException("can't set Color to null"); } image.setRGB(i, j, c.getRGB()); } // save to given filename - suffix must be png, jpg, or gif public void save(String name) { save(new File(name)); } // save to given filename - suffix must be png, jpg, or gif public void save(File file) { this.filename = file.getName(); if (frame != null) { frame.setTitle(filename); } String suffix = filename.substring(filename.lastIndexOf('.') + 1); suffix = suffix.toLowerCase(); if (suffix.equals("jpg") || suffix.equals("png")) { try { ImageIO.write(image, suffix, file); } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("Error: filename must end in .jpg or .png"); } } // open a save dialog when the user selects "Save As" from the menu public void actionPerformed(ActionEvent e) { FileDialog chooser = new FileDialog(frame, "Use a .png or .jpg extension", FileDialog.SAVE); chooser.setVisible(true); if (chooser.getFile() != null) { save(chooser.getDirectory() + File.separator + chooser.getFile()); } } // test client: read in input file and display public static void main(String[] args) { Picture pic = new Picture(args[0]); pic.show(); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-14-2007, 05:07 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
Are you sure you are passing main a String argument. That's the only place in your code that I see any mention of arrays unless I missed something.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-14-2007, 11:29 PM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
When you run the app, you should add a single argument:
Code:
java Picture ARGUMENT
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
java.lang.ArrayIndexOutOfBoundsException riccian New To Java 0 03-18-2008 11:38 AM
java.lang.ArrayIndexOutOfBoundsException mew New To Java 2 12-02-2007 11:40 PM
Error: java.lang.ArrayIndexOutOfBoundsException fernando Java 2D 1 08-01-2007 01:47 AM
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException romina New To Java 1 07-26-2007 12:55 AM
java.lang.ArrayIndexOutOfBoundsException Marcus New To Java 1 07-05-2007 07:15 AM


All times are GMT +3. The time now is 03:29 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org