Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Norm

Thread: ArrayStoreException with Object[][] array and ImageIcon

  1. #1
    X75TIGER75X is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default ArrayStoreException with Object[][] array and ImageIcon

    Hi everybody,

    I've ran to an runtime error where I couldn't figure what's wrong with it. I'm trying to create a JTable (with custom DefaultCellRender of course) with a .gif pictures on 5th row.

    a tiny part of my code;
    Java Code:
    Object[][] = new Object[3][6];
    //lot of coding using loops, etc -- all work fine except this
    ImageIcon icon = new ImageIcon("unmoved.gif");
    tableinformation[0][5] = icon;
    I'm confused about this, it should have worked fine because I've tested it on another class and it ran fine

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class TableIcon extends JFrame
    {
        public TableIcon()
        {
            ImageIcon aboutIcon = new ImageIcon("moved.gif");
            ImageIcon addIcon = new ImageIcon("moved.gif");
            ImageIcon copyIcon = new ImageIcon("unmoved.gif");
    
            //String[] columnNames = {"Picture", "Description"};
    		  String[] columnNames = {"1", "2", "3", "4", "5", "6"};
            /*Object[][] data =
            {
                {aboutIcon, "About"},
                {addIcon, "Add"},
                {copyIcon, "Copy"},
            };
    			*/
    		  Object[][] data = 
    		  {
                {"about", "name", "size", "dd", "bb", aboutIcon},
                {"abt", "nme", "e", "dd", "cc", copyIcon},
            };
            DefaultTableModel model = new DefaultTableModel(data, columnNames);
            JTable table = new JTable( model )
            {
                //  Returning the Class of each column will allow different
                //  renderers to be used based on Class
                public Class getColumnClass(int column)
                {
                    return getValueAt(0, column).getClass();
                }
            };
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
    
            JScrollPane scrollPane = new JScrollPane( table );
            getContentPane().add( scrollPane );
        }
    
        public static void main(String[] args)
        {
            TableIcon frame = new TableIcon();
            frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
            frame.pack();
            frame.setVisible(true);
        }
    
    }
    You can go ahead and test it yourself with code above. Don't forget to assign a .gif file inside the folder. You will see that it works just fine. I copied and pasted data object 2d array to my code where I have problem. It ran just fine with icons on the table. Whats a problem with assigning "tableinformation[0][5] = icon;"?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: ArrayStoreException with Object[][] array and ImageIcon

    Consider posting brief (but compilable) code that does *not* wrok. That is code which, at runtime, does not do what you expect or intend. By "brief" I mean without code that is irrelevent to illustrating the problem. Less is better when you're hunting bugs: the same goes for dependencies (images etc).

    If the problem isa runtime exception, post the whole thing along wkth the code to which it refers.

  3. #3
    X75TIGER75X is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: ArrayStoreException with Object[][] array and ImageIcon

    Alright, Ill post a method that returns JPanel

    Java Code:
    //You will need 
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.JTableHeader;
    //Attributes that you readers will need to know
    private ArrayList<String[]> data = new ArrayList<String[]>(); //To readers: please assume that there is data inside the ArrayList
    private DefaultTableModel tableModel;
    private JTable table;
    //
    public JPanel buildTable(){
    			JPanel tablePanel = new JPanel(new GridLayout(1,1));
    			String colHeader[] = {"#", "Name", "Size", "Host Directory", "Repository Directory", ""};
    			//Defining the table
    				Object[][] tableinformation  = new String[data.size()][6];
    				if(data.size() != 0){
    					for(int i = 0; i < data.size(); i++){
    						String[] information = data.get(i);
    						for(int j = 0; j < 6; j++){
    							if(j == 0){
    								tableinformation[i][0] = Integer.toString((i+1));
    							}else if(j == 1){
    								tableinformation[i][1] = information[0];
    							}else if(j == 2){
    								tableinformation[i][2] = information[5];
    							}else if(j == 3 || j == 4){
    								tableinformation[i][j] = information[(j-2)];
    							}else if(j == 5){
    								ImageIcon icon = null;
    								if(information[4].equals("")){
    									icon = new ImageIcon("unmoved.gif");
    								}else{
    									icon = new ImageIcon("moved.gif");
    								}
    								tableinformation[i][5] = icon; //THIS DOESNT WORK RIGHT - THROWS ARRAYSTOREEXCEPTION EXCEPTION
    							}
    						}
    					}//End of for loop
    				}//End of if statement
    			//Defining the table ends
      			DefaultTableModel tableModel = new DefaultTableModel(tableinformation, colHeader);
    			//JTable Formatting
    			table = new JTable(tableModel)
    				{public Class getColumnClass(int column){
                return getValueAt(0, column).getClass();}};
    			//JTable Formatting End
    			JTableHeader header = table.getTableHeader();
    			JScrollPane pane = new JScrollPane(table);
    			table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    			tablePanel.add(pane);
    			
    			return tablePanel;
      		}//End of buildTable() method
    I hope this will be sufficient for you guys to understand my situation. I'm tired of having to explain my situation more than enough.

    EDIT: You might need this for testing - add this to anyplace like constructor
    Java Code:
    String[] information = {"AppName", "AppDirectory", "RepoDirectory", "SL", "", "1.23GB"};
    data.add(information);
    Last edited by X75TIGER75X; 05-03-2012 at 07:13 PM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: ArrayStoreException with Object[][] array and ImageIcon

    What type of array do you create on line 11? Can that array hold an ImageIcon?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    X75TIGER75X is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: ArrayStoreException with Object[][] array and ImageIcon

    Quote Originally Posted by DarrylBurke View Post
    What type of array do you create on line 11? Can that array hold an ImageIcon?

    db
    ArrayList, it contains raw String data. No ImageIcons on that arraylist. You can see between line 12 and 33, program loads and translates the data into Object[][] which will be used on JTable. That Object[][] array is supposed to be able to hold ImageIcon. In my example code, it worked beautifully, but my current code fails at runtime when I try to assign ImageIcon to Object[][]. That what puzzles me.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default Re: ArrayStoreException with Object[][] array and ImageIcon

    For testing we need a program that compiles, executes and shows the problem. Partial code is useless. see post#2

    Also it would help if you posted the full text of the error messages.

    Did you read the API doc the the exception you are getting? Its pretty clear on what your problem is.
    Last edited by Norm; 05-03-2012 at 03:17 PM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    X75TIGER75X is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: ArrayStoreException with Object[][] array and ImageIcon

    Quote Originally Posted by Norm View Post
    For testing we need a program that compiles, executes and shows the problem. Partial code is useless. see post#2

    Also it would help if you posted the full text of the error messages.

    Did you read the API doc the the exception you are getting? Its pretty clear on what your problem is.
    Copy/Paste of Error Code in console

    ----jGRASP exec: java SoftwareMover

    Exception in thread "main" java.lang.ArrayStoreException: javax.swing.ImageIcon
    at SoftwareMover.buildTable(SoftwareMover.java:160)
    at SoftwareMover.buildUI(SoftwareMover.java:238)
    at SoftwareMover.<init>(SoftwareMover.java:45)
    at SoftwareMover.main(SoftwareMover.java:263)

    ----jGRASP wedge2: exit code for process is 1.

    This caused the error
    Java Code:
    ImageIcon icon = null;
    if(information[4].equals("")){
    	icon = new ImageIcon("unmoved.gif");
    }else{
    	icon = new ImageIcon("moved.gif");
    }
    tableinformation[i][5] = icon; //This is where Runtime Error points
    In attempt to avoid any further confusions and questions, tableinformation is Object[][]. information is String[]. If you need more information, look at my runnable method code post above (it returns JPanel, this means you can build a quick JFrame and use add(buildTable()) method. It shouldn't be hard for you guys). It should provide you more than enough information about my situation. Now how do I solve this "tableinformation[i][5] = icon;"

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default Re: ArrayStoreException with Object[][] array and ImageIcon

    Did you read the API doc for the exception? It explains what the problem is.
    If you have any questions about what it says, copy the doc here and ask you questions.

    it shouldn't be hard for you guys
    We're not paid for this. If you are too lazy to create a testing program, you'll be lucky to get much help.
    Last edited by Norm; 05-03-2012 at 07:13 PM.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    X75TIGER75X is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: ArrayStoreException with Object[][] array and ImageIcon

    Quote Originally Posted by Norm View Post
    Did you read the API doc for the exception? It explains what the problem is.
    If you have any questions about what it says, copy the doc here and ask you questions.


    We're not paid for this. If you are too lazy to create a testing program, you'll be lucky to get much help.
    Yes... I checked the API. It simply says "Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects." Believe me, I've done my homework, researching every possible solution and I cannot find it. This is why I asked a question on this forum to look for solution to this problem.

    EDIT: Fine, heres tester program. You can copy and paste it in any blank .java and compile it.

    Java Code:
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.JTableHeader;
    public class TestGUI extends JFrame{
    	private ArrayList<String[]> data = new ArrayList<String[]>(); //To readers: please assume that there is data inside the ArrayList
    	private DefaultTableModel tableModel;
    	private JTable table;
    	public TestGUI(){
    		String[] information = {"AppName", "AppDirectory", "RepoDirectory", "SL", "", "1.23GB"};
    		data.add(information);
    		add(buildTable());
    		setSize(400,400);
    		setLocation(200,200);
    		setVisible(true);
    	}
    	public JPanel buildTable(){
                JPanel tablePanel = new JPanel(new GridLayout(1,1));
                String colHeader[] = {"#", "Name", "Size", "Host Directory", "Repository Directory", ""};
                //Defining the table
                    Object[][] tableinformation  = new String[data.size()][6];
                    if(data.size() != 0){
                        for(int i = 0; i < data.size(); i++){
                            String[] information = data.get(i);
                            for(int j = 0; j < 6; j++){
                                if(j == 0){
                                    tableinformation[i][0] = Integer.toString((i+1));
                                }else if(j == 1){
                                    tableinformation[i][1] = information[0];
                                }else if(j == 2){
                                    tableinformation[i][2] = information[5];
                                }else if(j == 3 || j == 4){
                                    tableinformation[i][j] = information[(j-2)];
                                }else if(j == 5){
                                    ImageIcon icon = null;
                                    if(information[4].equals("")){
                                        icon = new ImageIcon("unmoved.gif");
                                    }else{
                                        icon = new ImageIcon("moved.gif");
                                    }
                                    tableinformation[i][5] = icon; //THIS DOESNT WORK RIGHT - THROWS ARRAYSTOREEXCEPTION EXCEPTION
                                }
                            }
                        }//End of for loop
                    }//End of if statement
                //Defining the table ends
                DefaultTableModel tableModel = new DefaultTableModel(tableinformation, colHeader);
                //JTable Formatting
                table = new JTable(tableModel)
                    {public Class getColumnClass(int column){
                return getValueAt(0, column).getClass();}};
                //JTable Formatting End
                JTableHeader header = table.getTableHeader();
                JScrollPane pane = new JScrollPane(table);
                table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                tablePanel.add(pane);
                 
                return tablePanel;
            }//End of buildTable() method
    		  public static void main(String[] args){
    		  	new TestGUI();
    		  }
    }
    EDIT2: And please try this template code. I used the template from this code. This code works fine. But my code above doesnt work.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class TableIcon extends JFrame
    {
        public TableIcon()
        {
            ImageIcon aboutIcon = new ImageIcon("moved.gif");
            ImageIcon addIcon = new ImageIcon("moved.gif");
            ImageIcon copyIcon = new ImageIcon("unmoved.gif");
    
            //String[] columnNames = {"Picture", "Description"};
    		  String[] columnNames = {"1", "2", "3", "4", "5", "6"};
            /*Object[][] data =
            {
                {aboutIcon, "About"},
                {addIcon, "Add"},
                {copyIcon, "Copy"},
            };
    			*/
    		  Object[][] data = 
    		  {
                {"about", "name", "size", "dd", "bb", aboutIcon},
                {"abt", "nme", "e", "dd", "cc", copyIcon},
            };
            DefaultTableModel model = new DefaultTableModel(data, columnNames);
            JTable table = new JTable( model )
            {
                //  Returning the Class of each column will allow different
                //  renderers to be used based on Class
                public Class getColumnClass(int column)
                {
                    return getValueAt(0, column).getClass();
                }
            };
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
    
            JScrollPane scrollPane = new JScrollPane( table );
            getContentPane().add( scrollPane );
        }
    
        public static void main(String[] args)
        {
            TableIcon frame = new TableIcon();
            frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
            frame.pack();
            frame.setVisible(true);
        }
    
    }
    Last edited by X75TIGER75X; 05-03-2012 at 07:27 PM.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default Re: ArrayStoreException with Object[][] array and ImageIcon

    attempt has been made to store the wrong type of object into an array of objects."
    What about the example with the API doc:
    For example, the following code generates an ArrayStoreException:
    Java Code:
         Object x[] = new String[3];
         x[0] = new Integer(0);
    Your code parallels that exactly:
    Java Code:
     Object[][] tableinformation  = new String[data.size()][6];
     .... 
     tableinformation[i][5] = icon; // defined by  ImageIcon icon
    Change the value given to tableinformation
    X75TIGER75X likes this.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    X75TIGER75X is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: ArrayStoreException with Object[][] array and ImageIcon

    Quote Originally Posted by Norm View Post
    What about the example with the API doc:

    Java Code:
         Object x[] = new String[3];
         x[0] = new Integer(0);
    Your code parallels that exactly:
    Java Code:
     Object[][] tableinformation  = new String[data.size()][6];
     .... 
     tableinformation[i][5] = icon; // defined by  ImageIcon icon
    Change the value given to tableinformation
    Ohh, I see my mistake. I meant
    Java Code:
    Object[][] tableinformation  = new String[data.size()][6];
    //to this
    Object[][] tableinformation  = new Object[data.size()][6];
    now it works. Thanks Norm.

Similar Threads

  1. Replies: 6
    Last Post: 05-09-2013, 09:51 AM
  2. Is an object array considered to be an object?
    By guest_user in forum New To Java
    Replies: 1
    Last Post: 07-13-2011, 07:40 AM
  3. Replies: 23
    Last Post: 09-07-2010, 09:12 PM
  4. Array of Object
    By haiforhussain in forum New To Java
    Replies: 1
    Last Post: 06-25-2008, 12:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •