Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    Java serializzazione NotSerializableException

    Salve a tutti, sono un neofita di java e della programmazione in generale, ho un problema forse banale, che non riesco a risolvere, ho cercato in rete non sono riuscito a capire come risolvere.L'errore che mi da è il seguente:[Laad] IO Error: writing aborted; java.io.NotSerializableException: JPESX[Update] IO Error: javax.swing.filechooser.FileNameExtensionFilter[Laad] IO Error: writing aborted; java.io.NotSerializableException: javax.swing.filechooser.FileNameExtensionFilterSon o circa 700 righe di codice, questa è la parte di codice che penso causi l'errore,:
    codice:
    public class JPESX implements Serializable{            JPESX() {                    fc = new JFileChooser(System.getProperty("user.dir")); 		    fc.setAcceptAllFileFilterUsed(false); 	            fc.addChoosableFileFilter(new FileNameExtensionFilter("Image files", ImageIO.getReaderFileSuffixes()));public class HighscoreManager implements Serializable{    // An arraylist of the type "score" we will use to work with the scores inside the class    private ArrayList scores;    // The name of the file where the highscores will be saved    private static final String HIGHSCORE_FILE = "scores.dat";    //Initialising an in and outputStream for working with the file    ObjectOutputStream outputStream = null;    ObjectInputStream inputStream = null;    public HighscoreManager() {        //initialising the scores-arraylist        scores = new ArrayList();    }    public ArrayList getScores() {        loadScoreFile();        sort();        return scores;    }    private void sort() {        ScoreComparator comparator = new ScoreComparator();        Collections.sort(scores, comparator);}    public void addScore(String name, long score) {        loadScoreFile();        scores.add(new Score(name, score));        updateScoreFile();}    public void loadScoreFile() {        try {            inputStream = new ObjectInputStream(new FileInputStream(HIGHSCORE_FILE));            scores = (ArrayList) inputStream.readObject();        } catch (FileNotFoundException e) {            System.out.println("[Laad] FNF Error: " + e.getMessage());        } catch (IOException e) {            System.out.println("[Laad] IO Error: " + e.getMessage());        } catch (ClassNotFoundException e) {            System.out.println("[Laad] CNF Error: " + e.getMessage());        } finally {            try {                if (outputStream != null) {                    outputStream.flush();                    outputStream.close();                }            } catch (IOException e) {                System.out.println("[Laad] IO Error: " + e.getMessage());            }        }}    public void updateScoreFile() {        try {            outputStream = new ObjectOutputStream(new FileOutputStream(HIGHSCORE_FILE));            outputStream.writeObject(scores);        } catch (FileNotFoundException e) {            System.out.println("[Update] FNF Error: " + e.getMessage() + ",the program will try and make a new file");        } catch (IOException e) {            System.out.println("[Update] IO Error: " + e.getMessage());        } finally {            try {                if (outputStream != null) {                    outputStream.flush();                    outputStream.close();                }            } catch (IOException e) {                System.out.println("[Update] Error: " + e.getMessage());            }        }}}
    Ringrazio in anticipo chi riuscirà a darmi una mano.

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quote Originariamente inviata da Romano romano Visualizza il messaggio
    non sono riuscito a capire come risolvere.L'errore che mi da è il seguente:[Laad] IO Error: writing aborted; java.io.NotSerializableException: JPESX[Update] IO Error: javax.swing.filechooser.FileNameExtensionFilter[Laad] IO Error: writing aborted; java.io.NotSerializableException: javax.swing.filechooser.FileNameExtensionFilter
    Nota: il codice l'hai postato che è illeggibile. Riposta il codice, ben indentato, poi metti a mano i tag CODE.

    Tornando al tuo problema: tu serializzi un ArrayList, che di per sé è serializzabile, ma lo deve essere anche il contenuto! Da quanto riesco a vedere dal codice (sebbene mal postato), nel ArrayList inserisci oggetti di tipo Score. Bene: Score è serializzabile?
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Scusa per l'errore nel postare il codice.
    Così dovrebbe essere ok:

    codice:
    public class JPESX implements Serializable{
    	JPESX() {
    		....
    		fc = new JFileChooser(System.getProperty("user.dir")); 
    		fc.setAcceptAllFileFilterUsed(false); 
    		fc.addChoosableFileFilter(new FileNameExtensionFilter("Image files", ImageIO.getReaderFileSuffixes()));
    		...
    	}
    	
    	public class Score implements Serializable {
    		private long score;
    		private String naam;
    
    		public long getScore() {
    			return score;
    		}
    
    		public String getNaam() {
    			return naam;
    		}
    
    		public Score(String naam, long score) {
    			this.score = score;
    			this.naam = naam;
    		}
    	}
    	
    	public class HighscoreManager implements Serializable{
    		// An arraylist of the type "score" we will use to work with the scores inside the class
    		private ArrayList<Score> scores;
    
    		// The name of the file where the highscores will be saved
    		private static final String HIGHSCORE_FILE = "scores.dat";
    
    		//Initialising an in and outputStream for working with the file
    		ObjectOutputStream outputStream = null;
    		ObjectInputStream inputStream = null;
    
    		public HighscoreManager() {
    			//initialising the scores-arraylist
    			scores = new ArrayList<Score>();
    		}
    		public ArrayList<Score> getScores() {
    			loadScoreFile();
    			sort();
    			return scores;
    		}
    		private void sort() {
    			ScoreComparator comparator = new ScoreComparator();
    			Collections.sort(scores, comparator);
    		}
    		public void addScore(String name, long score) {
    			loadScoreFile();
    			scores.add(new Score(name, score));
    			updateScoreFile();
    		}
    		public void loadScoreFile() {
    			try {
    				inputStream = new ObjectInputStream(new FileInputStream(HIGHSCORE_FILE));
    				scores = (ArrayList<Score>) inputStream.readObject();
    			} catch (FileNotFoundException e) {
    				System.out.println("[Laad] FNF Error: " + e.getMessage());
    			} catch (IOException e) {
    				System.out.println("[Laad] IO Error: " + e.getMessage());
    			} catch (ClassNotFoundException e) {
    				System.out.println("[Laad] CNF Error: " + e.getMessage());
    			} finally {
    				try {
    					if (outputStream != null) {
    						outputStream.flush();
    						outputStream.close();
    					}
    				} catch (IOException e) {
    					System.out.println("[Laad] IO Error: " + e.getMessage());
    				}
    			}
    		}
    		public void updateScoreFile() {
    			try {
    				outputStream = new ObjectOutputStream(new FileOutputStream(HIGHSCORE_FILE));
    				outputStream.writeObject(scores);
    			} catch (FileNotFoundException e) {
    				System.out.println("[Update] FNF Error: " + e.getMessage() + ",the program will try and make a new file");
    			} catch (IOException e) {
    				System.out.println("[Update] IO Error: " + e.getMessage());
    			} finally {
    				try {
    					if (outputStream != null) {
    						outputStream.flush();
    						outputStream.close();
    					}
    				} catch (IOException e) {
    					System.out.println("[Update] Error: " + e.getMessage());
    				}
    			}
    		}
    	}

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.