Visualizzazione dei risultati da 1 a 8 su 8
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2005
    Messaggi
    153

    Inseriment in HashMap non avviene.

    Salve ho questa classe:
    public class Ludoteca {

    private Map<String, Item> item = null;
    private Map<String, Ambient> ambientService = null;
    private String name = null;

    public Ludoteca(String name) {
    if (name == null || name.equals("")) {
    throw new IllegalArgumentException("Name ludoteca invalid");
    }
    this.name = name;
    this.item = Collections.synchronizedMap(new HashMap<String, Item>());
    this.ambientService = Collections.synchronizedMap(new HashMap<String, Ambient>());
    }

    public String getName(){
    return this.name;
    }

    public Item addItem(Item item){
    if (item!=null){
    return this.item.put(item.getId(), item);
    }
    else return null;
    }

    public Item removeItem(Item item){
    if(item!=null){
    if(item.isAvailable() && item.isLent()){
    return this.item.remove(item.getId());
    }
    else return null;
    }
    else return null;
    }
    }

    ed una classe che la testa

    public class TestLudoteca {

    public static void main(String[] args){
    Ludoteca l=new Ludoteca("Synclab");
    System.out.println("Ludoteca:"+l.getName());
    Item it=new Item("Gioco1","Toy_1");
    Item i=l.addItem(it);
    if(i.equals(it)) System.out.println("Inserimento avvenuto");
    else System.out.println("Inserimento non avvenuto");
    i=l.removeItem(it);
    if(i.equals(it)) System.out.println("Cancellazione avvenuta");
    else System.out.println("Cancellazione non avvenuta");
    }
    }

    perchè l'inserimento nella HashMap non avviene ritornandomi null?

  2. #2
    Utente di HTML.it
    Registrato dal
    Feb 2009
    Messaggi
    502
    Per testare il tuo codice mi manca la classe Item e Ambient.

    Il codice mettilo tra i tag CODE che risulta molto più leggibile, grazie.
    al volante son nervoso

  3. #3
    Utente di HTML.it
    Registrato dal
    Jan 2005
    Messaggi
    153
    Ti posto tutto il codice, proprio non riesco a capire il perchè non inserisce.

    codice:
    public class Ludoteca {
    
    private Map<String, Item> item = null;
    private Map<String, Ambient> ambientService = null;
    private String name = null;
    
    public Ludoteca(String name) {
    if (name == null || name.equals("")) {
    throw new IllegalArgumentException("Name ludoteca invalid");
    }
    this.name = name;
    this.item = Collections.synchronizedMap(new HashMap<String, Item>());
    this.ambientService = Collections.synchronizedMap(new HashMap<String, Ambient>());
    }
    
    public String getName(){
    return this.name;
    }
    
    public Item addItem(Item item){
    if (item!=null){
    return this.item.put(item.getId(), item);
    }
    else return null;
    }
    
    public Item removeItem(Item item){
    if(item!=null){
    if(item.isAvailable() && item.isLent()){
    return this.item.remove(item.getId());
    }
    else return null;
    }
    else return null;
    }
    }
    codice:
    public class TestLudoteca {
    
    public static void main(String[] args){
    Ludoteca l=new Ludoteca("Synclab");
    System.out.println("Ludoteca:"+l.getName());
    Item it=new Item("Gioco1","Toy_1");
    Item i=l.addItem(it);
    if(i.equals(it)) System.out.println("Inserimento avvenuto");
    else System.out.println("Inserimento non avvenuto");
    i=l.removeItem(it);
    if(i.equals(it)) System.out.println("Cancellazione avvenuta");
    else System.out.println("Cancellazione non avvenuta");
    }
    }
    codice:
    public class Item {
    
        private String id = null;
        private int ageMin = 0;
        private int ageMax = 0;
        private boolean lent = true;
        private String activity = null;
        private boolean available = true;
        private String name = null;
    
        /**
         * @param name
         * @param id
         */
        public Item(String name, String id) {
            if (name == null || name.equals("")) {
                 throw new IllegalArgumentException("Name invalid");
            }
            this.name = name;
            if (id == null || id.equals("")) {
                 throw new IllegalArgumentException("Lastname invalid");
            }
            this.id = id;
        }
    
        /**
         * @param name
         * @param id
         * @param agemin
         * @param agemax
         */
        public Item(String name, String id, int agemin, int agemax) {
            this(name, id);
            if (agemin < 0) {
                 throw new IllegalArgumentException("Minimum age is not valid");
            }
            this.ageMin = agemin;
            if (agemax < 0 || agemax < this.ageMin) {
                 throw new IllegalArgumentException("Maximum age invalid");
            }
            this.ageMax = agemax;
        }
    
        /**
         * @return
         */
        public String getId() {
            return this.id;
        }
    
        /**
         * @return
         */
        public int getAgeMin() {
            return this.ageMin;
        }
    
        /**
         * @return
         */
        public int getAgeMax() {
            return this.ageMax;
        }
    
        /**
         * @param age
         * @return
         */
        public boolean recommended(int age) {
            return this.ageMin <= age && age <= this.ageMax;
        }
    
        /**
         * @param date
         * @return
         */
        public boolean reccommended(Date date) {
            GregorianCalendar gc = new GregorianCalendar();
            gc.setTime(date);
            long year = ((new GregorianCalendar().getTimeInMillis() - gc.getTimeInMillis()) / 1000);
            year = year / 86400;
            year = year / 365;
            return this.ageMin <= year && year <= this.ageMax;
        }
    
        /**
         * @return
         */
        public String getActivity() {
            return this.activity;
        }
    
        /**
         * @param name
         */
        public void setName(String name) {
            this.name = name;
        }
    
        /**
         * @return
         */
        public String getName() {
            return this.name;
        }
    
        /**
         * @param id
         */
        public void setId(String id) {
            this.id = id;
        }
    
        /**
         * @param agemin
         */
        public void setAgeMin(int agemin) {
            this.ageMin = agemin;
        }
    
        /**
         * @param agemax
         */
        public void setAgeMax(int agemax) {
            this.ageMax = agemax;
        }
    
        /**
         * @param activity
         */
        public void setActivity(String activity) {
            this.activity = activity;
        }
    
        /**
         * @param lent
         */
        public void setLent(boolean lent) {
            this.lent = lent;
        }
    
        /**
         * @param lent
         */
        public void setAvailable(boolean available) {
            this.available = available;
        }
    
        /**
         * @return
         */
        public boolean isLent() {
            return this.lent;
        }
    
        /**
         * @return
         */
        public boolean isAvailable() {
            return this.available;
        }
    }
    codice:
    public class Ambient implements AmbientService {
    
        private String name = null;
        private String id = null;
        private Map<String, Spaces> spaces = null;
    
        public Ambient(String name, String id) {
            if (name == null || name.equals("")) {
                throw new IllegalArgumentException("Name invalid");
            }
            this.name = name;
            if (id == null || id.equals("")) {
                 throw new IllegalArgumentException("Id invalid");
            }
            this.id = id;
    
            this.spaces = Collections.synchronizedMap(new HashMap<String, Spaces>());
        }
    
        public void available(Spaces space) {
            if(space!=null) {
                Spaces sp=this.spaces.get(space.getId());
                sp.setAvailable(true);
                this.spaces.put(sp.getId(), space);
            }
        }
    
        public String getId() {
            return this.id;
        }
    
        public void setId(String id) {
            if (id != null && !id.equals("")) {
                this.id = id;
            }
        }
    
        public String getName() {
            return this.name;
        }
    
        public void setName(String name) {
            if (name != null && !name.equals("")) {
                this.name = name;
            }
        }
    
        public Map getSpaces(){
            return this.spaces;
        }
    
        /**
         * @param spaces
         * @return
         */
        public Spaces addSpace(Spaces spaces) {
            return this.spaces.put(spaces.getId(), spaces);
        }
    
        /**
         * @param spaces
         */
        public void removeSpace(Spaces spaces) {
            this.spaces.remove(spaces.getId());
        }
    
        public Spaces isFree() {
            //Verifico che almeno uno spazio nell'ambiente sia libero
            Iterator<String> i = this.spaces.keySet().iterator();
            Spaces s = null;
            while (i.hasNext()) {
                s = this.spaces.get(i.next());
                if (s != null && s.isAvailable()) {
                    return s;
                }
            }
            return null;
        }
        
        public boolean isAvailable(){
            Iterator<String> i = this.spaces.keySet().iterator();
            Spaces s = null;
            while (i.hasNext()) {
                s = this.spaces.get(i.next());
                if (!s.isAvailable()) {
                    return false;
                }
            }
            return true;
        }
    
        public Spaces take(String a) {
            Iterator<String> i = this.spaces.keySet().iterator();
            Spaces sp = null;
            while (i.hasNext()) {
                sp = this.spaces.get(i.next());
                if (sp != null) { 
                    if (sp.isAvailable() && sp.getActivity().equals(a)) {
                        sp.setAvailable(false);
                        this.spaces.remove(sp.getId());
                        this.spaces.put(sp.getId(), sp); 
                        return sp;
                    }
                }
            }
            return null;
        }
    
        public void issue(Spaces spaces) {
            spaces.setAvailable(true);
            this.spaces.put(spaces.getId(), spaces);
        }
    
        public Set getActivity() {
            HashSet<String> activity = new HashSet<String>();
            //scorro la mappa degli spazi per reperire le attività possibili
            Iterator<String> i = this.spaces.keySet().iterator();
            Spaces s = null;
            while (i.hasNext()) {
                s = this.spaces.get(i.next());
                if (s != null) {
                    activity.add(s.getActivity());
                }
            }
            return activity;
        }
    }

  4. #4
    Utente di HTML.it
    Registrato dal
    Feb 2009
    Messaggi
    502
    Originariamente inviato da prog
    Ti posto tutto il codice, proprio non riesco a capire il perchè non inserisce.
    AmbientService e Spaces il mio piccì non sa che sono
    al volante son nervoso

  5. #5
    Utente di HTML.it
    Registrato dal
    Jan 2005
    Messaggi
    153
    Scusa ma a cosa ti serve AmbientService e Spaces se non inserisce nella hashmap relativa agli Item?

  6. #6
    Utente di HTML.it
    Registrato dal
    Feb 2009
    Messaggi
    502
    Originariamente inviato da prog
    Scusa ma a cosa ti serve AmbientService e Spaces se non inserisce nella hashmap relativa agli Item?
    Verissimo. Ma se devo trovare il perché dovendo debuggare decine di righe di codice... gradirei avere tutto il codice.

    Comunque l'errore è che hai un bel null ritornato da addItem.
    Consulta le javadoc delle Map, troverai la spiegazione.
    al volante son nervoso

  7. #7
    Utente di HTML.it
    Registrato dal
    Jan 2005
    Messaggi
    153
    Originariamente inviato da Rubox
    Verissimo. Ma se devo trovare il perché dovendo debuggare decine di righe di codice... gradirei avere tutto il codice.

    Comunque l'errore è che hai un bel null ritornato da addItem.
    Consulta le javadoc delle Map, troverai la spiegazione.
    Guarda che il metodo è questo
    codice:
    public Item addItem(Item item){
    if (item!=null){
    return this.item.put(item.getId(), item);
    }
    else return null;
    }
    c'è un if dove debbugato entra poi fa un return di this.item.put(item.getId(), item); che se l'inserimento va a buon fine deve semplicemente ritornare l'item inserito, il null è nella condizione else che ovviamente non viene eseguita, visto che il parametro item non contiene un riferimento a null!!!

    E poi la documentazione java di Map dice esattamente quello che io faccio il put ritorna l'oggetto inserito se va a buon fine altrimenti null, io quello che domando è perchè l'inserimento non avviene?

  8. #8
    Utente di HTML.it
    Registrato dal
    Feb 2009
    Messaggi
    502
    Originariamente inviato da prog
    Guarda che il metodo è questo
    codice:
    public Item addItem(Item item){
    if (item!=null){
    return this.item.put(item.getId(), item);
    }
    else return null;
    }
    c'è un if dove debbugato entra poi fa un return di this.item.put(item.getId(), item); che se l'inserimento va a buon fine deve semplicemente ritornare l'item inserito, il null è nella condizione else che ovviamente non viene eseguita, visto che il parametro item non contiene un riferimento a null!!!
    A me risulta che non essendoci nessuna chiave con quel valore, il this.item.put() ritorna null e lo assegna a i, su cui poi invochi un equals()...
    Ma forse è l'ora tarda e inizio a non capire più nulla.
    al volante son nervoso

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.