Results 1 to 8 of 8
  1. #1
    Embercloud is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default object stack memory id apeears instead of object itself

    Hi!

    So I'm working on this project for school where you have to make a program that functions as a library.

    I have two Jlists liked through defaultlistmodel and some buttons.

    When I press the "borrow" button a copy of the selected object in jlist1 is supposed to appear in jlist2 but instead I get this:

    [Ljava.lang.Object;@5e4be2cc]

    I know this is the numerical representation of the object in the stack memory but how do I make the object appear like it should

    ([Animal Farm, George Orwell, (1946)ex:3])

    Is there some method I forgot to invoke?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Embercloud View Post
    Is there some method I forgot to invoke?
    Nope, there's something you forgot to define in your class (if applicable); when an object is printed its toString() method is called. When that particular class doesn't define such method the superclass method is called, ultimately the toString() method in the Object class is called which produces output like you have noticed.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Embercloud is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    But I already have a toString() in this particular object class

    @Override
    public String toString() {
    return "[" + Boknamn + ", " + Forfattare + ", " + "(" + Year + ")" + ", " + "ex:" + Kopior + "]";
    }


    It says bookname, author, year and copies.

    These values have the corresponding instances.
    Last edited by Embercloud; 01-02-2011 at 03:37 PM.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Embercloud View Post
    But I already have a toString() in this particular object class

    @Override
    public String toString() {
    return "[" + Boknamn + ", " + Forfattare + ", " + "(" + Year + ")" + ", " + "ex:" + Kopior + "]";
    }


    It says bookname, author, year and copies.

    These values have the corresponding instances.
    Can you show us a bit of your code (the relevant parts please)? If your object displays as:

    [Ljava.lang.Object;@5e4be2cc]

    I wonder where those square brackets come from ... It's certainly not from your toString() method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Embercloud is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    My book class

    Java Code:
    public class Bok {
    	
    	
    
    	private String Boknamn;
    	
    	private String Forfattare;
    	
    	private int Year;
    	
    	private int Kopior;
    	
    	
    
    	Bok (String namn, String author, int year, int kopior){
    		this.Boknamn = namn;
    		this.Forfattare = author;
    		this.Year = year;
    		this.Kopior = kopior;
    		
    		}
    	
    	
    	
    	
    
    	public int getKopior() {
    		return Kopior;
    	}
    
    
    
    	public void setKopior(int kopior) {
    		this.Kopior = kopior;
    	}
    
    	public int getYear() {
    		return Year;
    	}
    
    	public void setYear(int year) {
    		this.Year = year;
    	}
    
    	public String getBoknamn() {
    		return Boknamn;
    	}
    
    	public void setBoknamn(String boknamn) {
    		this.Boknamn = boknamn;
    	}
    
    	public String getForfattare() {
    		return Forfattare;
    	}
    
    	public void setForfattare(String forfattare) {
    		this.Forfattare = forfattare;
    	}
    	
    	@Override
    	public String toString() {
    		return "[" + Boknamn + ", " + Forfattare + ", " + "(" + Year + ")" + ", " + "ex:" + Kopior + "]";
    	}

    Declaring objects

    Java Code:
    public class Bibliotek {
    	
    	
    		 static Bok bok1 =   new Bok ("Krig och Fred", "Leo Tolstoj", 1846, 4);
    		 static Bok bok2 =   new Bok ("Mein Kampfy Chair", "Ikea", 1932, 54 );
    		 static Bok bok3 =   new Bok ("Hairy Pothead: The Bong of Destiny", "Smokey McStoner", 9001, 3 );
    		 static Bok bok4 =   new Bok ("Små Blå Män", "Terry Pratchett", 2002, 2 );
    		 static Bok bok5 =   new Bok ("Embercloud: Rising of K'desh", "Urban Johnson", 2008, 5 );
    		 static Bok bok6 =   new Bok ("Deus Ex", "JC Denton", 2000, 3);
    		 static Bok bok7 =   new Bok ("1984", "George Orwell", 1948, 2 );
    		 static Bok bok8 =   new Bok ("Animal Farm", "George Orwell", 1946, 7 );
    		 static Bok bok9 =   new Bok ("Atonement", "Ian McEvan", 2002, 3 );
    		 static Bok bok10 =  new Bok ("The Blind Assassin", "Margareta Knisel", 1893, 29 );
    		 static Bok bok11 =  new Bok ("The Golden Notebook", "Doris Lessing", 1962, 11 );
    		 static Bok bok12 =  new Bok ("Last Goodbye", "Amber Alexander", 2008, 6 );
    		 static Bok bok13 =  new Bok ("Topside", "Nicolas Torpek", 1999, 5 );
    		 static Bok bok14 =  new Bok ("The Ley Walker", "Merlin Madagascar", 1989, 3);
    "he's making a list and checking it twice"

    Java Code:
    public Manager() {
        setLayout(new BorderLayout());
        
        cb = new JComboBox(Personlista.Personlista);
        LanlistModel = new DefaultListModel();
        BoklistModel = new DefaultListModel();
        boklist = new JList(BoklistModel);
        boklist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        boklist.setSelectedIndex(0);
        
        boklist.setVisibleRowCount(5);
        JScrollPane pane = new JScrollPane(boklist);
        lanlist = new JList(LanlistModel);
        JScrollPane pane2 = new JScrollPane(lanlist);
        JButton jbtlana = new JButton("Låna");
        JButton jbttillbaka = new JButton("Lämna tillbaka");
        
        
        boklist.addListSelectionListener(new ListSelectionListener() {
    
    		@Override
    		public void valueChanged(ListSelectionEvent e) {
    			
    			Object selectioncollection = boklist.getSelectedValues();	
    		}
        	
        });
    
    //the listener for my "add" button
        jbtlana.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
        	  Object selected = boklist.getSelectedValues();
        	  insertob(LanlistModel, selected);
        	  
        	        
    
          }
        });
    //listener for my remove button
        jbttillbaka.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
        	  Object selected = lanlist.getSelectedValues();
        	  insertob(BoklistModel, selected);
        	  
    
          }
        });
    
        add(pane, BorderLayout.WEST);
        add(pane2, BorderLayout.EAST);
        add(jbtlana, BorderLayout.CENTER);
        add(jbttillbaka, BorderLayout.SOUTH);
        add(cb, BorderLayout.NORTH);
      }
    
    public static void kor (){
    	  
        JFrame frame = new JFrame("Bibliotek");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new Manager());
        frame.setSize(750, 400);
        frame.setVisible(true);
    }
    
    
      public static void main(String [] args) {
    
    	  kor();
    	
    	  insertob(BoklistModel, Bibliotek.bok1);
    	  insertob(BoklistModel, Bibliotek.bok2);
    	  insertob(BoklistModel, Bibliotek.bok3);
    	  insertob(BoklistModel, Bibliotek.bok4);
    	  insertob(BoklistModel, Bibliotek.bok5);
    	  insertob(BoklistModel, Bibliotek.bok6);
    	  insertob(BoklistModel, Bibliotek.bok7);
    	  insertob(BoklistModel, Bibliotek.bok8);
    	  insertob(BoklistModel, Bibliotek.bok9);
    	  insertob(BoklistModel, Bibliotek.bok10);
    	  insertob(BoklistModel, Bibliotek.bok11);
    	  insertob(BoklistModel, Bibliotek.bok12);
    	  insertob(BoklistModel, Bibliotek.bok13);
    	 
      }
      }

    Here is my insertob(), which i believe is the cause of my problem

    Java Code:
    public static void insertob (DefaultListModel list, Object ob) {
    			list.addElement(ob);
    		}
    Last edited by Embercloud; 01-02-2011 at 04:26 PM.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    You're using 'getSelectedValues()' which returns an entire Object[] and you insert it in the other JList as if it were a single value. Use 'getSelectedValue()' instead (it gets the first selected value from a JList) or use the array returned by the getSelectedValues() method and insert each element in the array individually.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Embercloud is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    That solved my problem.

    Your help has been invaluable.

    Thank you.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Embercloud View Post
    That solved my problem.
    Your help has been invaluable.
    Thank you.
    You're welcome of course (I once made exactly this mistake myself ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 2
    Last Post: 06-22-2010, 04:29 AM
  2. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  3. Replies: 1
    Last Post: 07-22-2008, 04:27 PM
  4. Object creation and memory issues
    By bugger in forum New To Java
    Replies: 11
    Last Post: 11-29-2007, 12:56 PM
  5. removing an object from memory
    By ravian in forum New To Java
    Replies: 1
    Last Post: 11-12-2007, 09:23 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
  •