Avatar billede Slettet bruger
16. august 2003 - 21:34 Der er 5 kommentarer og
1 løsning

XMLEncoder klarer JavaBeans men ikke LinkedList i en Javabean

Jeg bruger en linkedlist til at gemme en variabel mængde sammenhørende data i en JavaBean (faktisk 3 linkedliste), når jeg så gemmer (skriver stream til disk) via XMLEncoder, får jeg denne grimme (har dog set værre *gg*) fejl:

java.lang.InstantiationException: FileLocation
Continuing ...
java.lang.Exception: discarding statement LinkedList0.add<FileLocation0>;
Continuing ...

Hrmmm.... der må jo være noget i vejen med min FileLocation-class også, så i får lige min bean-klasse samt FileLocation-class....

Først FileLocation.java (JavaBean):

public class FileLocation
{    private String fileExtension;
    private String fileLocation;
    private String description;
   
    public FileLocation(String fileExtension, String fileLocation, String description)
    {    this.fileExtension = fileExtension;
        this.fileLocation = fileLocation;
        this.description = description;
    }
   
    public String toString() { return "[."+fileExtension+"] @ "+fileLocation; }
   
    // Java-Bean getter-properties
    public String getFileExtension() { return fileExtension; }
    public String getFileLocation() { return fileLocation; }
    public String getDescription() { return description; }
   
    // Java-Bean setter-properties
    public void setFileExtension(String fileExtension) { this.fileExtension = fileExtension; }
    public void setFileLocation(String fileLocation) { this.fileLocation = fileLocation; }
    public void setDescription(String description) { this.description = description; }
}

Så Configuration.java (også JavaBean):

import java.util.*;

public class Configuration
{    private LinkedList fileLocations;
    private LinkedList grabLocations;
    private LinkedList excludeFileTypes;
    private String logFileLocation;
    private boolean saveLogToFile;

    public Configuration()
    {    fileLocations = new LinkedList();
        grabLocations = new LinkedList();
        excludeFileTypes = new LinkedList();
        excludeFileTypes.add("dat");
        excludeFileTypes.add("tmp");
        logFileLocation = "";
        saveLogToFile = false;
    }

    public void addFileLocation(FileLocation toAdd) { fileLocations.addLast(toAdd); }
    public void addGrabLocation(String path) { grabLocations.add(path); }
    public void addFileExclusion(String fileType) { excludeFileTypes.add(fileType); }
    public void removeFileLocation(int index) { fileLocations.remove(index); }
    public void removeGrabLocation(int index) { grabLocations.remove(index); }
    public void removeFileExclusion(int index) { excludeFileTypes.remove(index); }
    public Object[] getFileLocationAsArray() { return fileLocations.toArray(); }
    public Object[] getGrabLocationAsArray()
    {     Object[] toReturn = new Object[grabLocations.size()];
        for (int i = 0; i < grabLocations.size(); i++)
            toReturn[i] = (String)grabLocations.get(i);
        return toReturn;
    }
    public Object[] getExcludesAsArray()
    {     Object[] toReturn = new Object[excludeFileTypes.size()];
        for (int i = 0; i < excludeFileTypes.size(); i++)
            toReturn[i] = (String)excludeFileTypes.get(i);
        return toReturn;
    }
   
   
    // Java-Bean setter-properties
    public void setFileLocations(LinkedList fileLocations) { this.fileLocations = fileLocations; }
    public void setGrabLocations(LinkedList grabLocations) { this.grabLocations = grabLocations; }
    public void setExcludeFileTypes(LinkedList excludeFileTypes) { this.excludeFileTypes = excludeFileTypes; }
    public void setLogFileLocation(String logFileLocation) { this.logFileLocation = logFileLocation; }
    public void setSaveLogToFile(boolean saveLogToFile) { this.saveLogToFile = saveLogToFile; }
   
    // Java-Bean getter-properties
    public LinkedList getFileLocations() { return fileLocations; }
    public LinkedList getGrabLocations() { return grabLocations; }
    public LinkedList getExcludeFileTypes() { return excludeFileTypes; }
    public String getLogfileLocation() { return logFileLocation; }
    public boolean getSaveLogToFile() { return saveLogToFile; }
}
Avatar billede arne_v Ekspert
16. august 2003 - 22:08 #1
Prøv og lav en constructor for FileLocation uden argumenter !
Avatar billede arne_v Ekspert
16. august 2003 - 22:09 #2
public FileLocation()
    {
        this.fileExtension = null;
        this.fileLocation = null;
        this.description = null;
    }
Avatar billede Slettet bruger
16. august 2003 - 22:17 #3
Værsgo 60 gode E-point for et godt, kontant, kort, præcist svar.... :-)
Avatar billede arne_v Ekspert
16. august 2003 - 22:29 #4
Det er en af de ting man tit glemmer: beans skal have en constructor uden
argumenter.
Avatar billede Slettet bruger
18. august 2003 - 10:57 #5
Hrmmm.....har da før lavet Beans' uden constructor overhovedet ?!?.... men så kaldes der vist automatisk en no-argument constructor....hrmmm, er nu skrevet bag øret..... tak :-)
Avatar billede arne_v Ekspert
18. august 2003 - 11:01 #6
Hvis du ingen constructor laver overhovedet så laver Java en no argument
constructor for dig.

Men hvis du selv laver en constructor med argumenter så laver Java ikke
en no argument constructor.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester