NotSerializable Exception

Discussion in 'Plugin Development' started by Molten, Jan 8, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Molten

    Edit: I changed HomeLocation to implement Serializable but the problem still occurs, it looks like it is coming from the bukkit vectors? Here is my exact error log:

    http://pastebin.com/4bZTmd7r

    I'm having a problem when I try and save my datatype to a file when the server closes.

    My datatype is a hashmap of Strings and a double array, a vector, another double array, and another vector.

    The code is as below. I use the SLAPI that it has on the plugin tutorial page, I'm not really sure how to change it so they are serializable or getting rid of that statement.

    The code for my object is:

    Code:java
    1. package com.gmail.betorages.homecommand;
    2.  
    3. import org.bukkit.util.Vector;
    4.  
    5.  
    6. public class HomeLocation{
    7.  
    8. private double[] position;
    9. private Vector direction;
    10. private double[] lastPosition;
    11. private Vector lastDirection;
    12.  
    13. public HomeLocation(double[] position, Vector direction, double[] lastPosition, Vector lastDirection){
    14. this.direction = direction;
    15. this.position = position;
    16. }
    17.  
    18. public double[] getPosition(){
    19. return this.position;
    20. }
    21.  
    22. public Vector getDirection(){
    23. return this.direction;
    24. }
    25.  
    26. public double[] getLastPosition(){
    27. return this.lastPosition;
    28. }
    29.  
    30. public Vector getLastDirection(){
    31. return this.lastDirection;
    32. }
    33.  
    34. public void setLastDirection(Vector direction){
    35. this.lastDirection = direction;
    36. }
    37.  
    38. public void setLastPosition(double[] position){
    39. this.lastPosition = position;
    40. }
    41. }


    And where it saves the file:

    Code:java
    1. @Override
    2. public void onDisable() {
    3. getLogger().info("Just Kidding! Had ya goin though right?");
    4. try{
    5. SLAPI.save(playerHomes,"plugins\\HomeCommand\\playerHomes.bin");
    6. }catch(Exception e){
    7. e.printStackTrace();
    8. }
    9. }
     
  2. Offline

    adam753

    For SLAPI to be able to save a class (including a collection/hashmap of it), it needs to implement Serializable:
    Code:
    public class HomeLocation implements Serializable {
    And when you get a warning asking if you want to add a variable called serialVersionUID, click yes.
     
  3. Offline

    Molten

    I added what you said and I'm still getting the error, here is the new code:

    Code:java
    1. package com.gmail.betorages.homecommand;
    2.  
    3. import java.io.Serializable;
    4.  
    5. import org.bukkit.util.Vector;
    6.  
    7.  
    8. public class HomeLocation implements Serializable{
    9.  
    10. /**
    11.   *
    12.   */
    13. private static final long serialVersionUID = 1L;
    14. private double[] position;
    15. private Vector direction;
    16. private double[] lastPosition;
    17. private Vector lastDirection;
    18.  
    19. public HomeLocation(double[] position, Vector direction, double[] lastPosition, Vector lastDirection){
    20. this.direction = direction;
    21. this.position = position;
    22. }
    23.  
    24. public double[] getPosition(){
    25. return this.position;
    26. }
    27.  
    28. public Vector getDirection(){
    29. return this.direction;
    30. }
    31.  
    32. public double[] getLastPosition(){
    33. return this.lastPosition;
    34. }
    35.  
    36. public Vector getLastDirection(){
    37. return this.lastDirection;
    38. }
    39.  
    40. public void setLastDirection(Vector direction){
    41. this.lastDirection = direction;
    42. }
    43.  
    44. public void setLastPosition(double[] position){
    45. this.lastPosition = position;
    46. }
    47. }
    48.  


    Code:java
    1. package com.gmail.betorages.homecommand;
    2.  
    3. import java.io.FileInputStream;
    4. import java.io.FileOutputStream;
    5. import java.io.ObjectInputStream;
    6. import java.io.ObjectOutputStream;
    7. import java.io.Serializable;
    8.  
    9. /** SLAPI = Saving/Loading API
    10. * API for Saving and Loading Objects.
    11. * You can use this API in your projects, but please credit the original author of it.
    12. * @author Tomsik68<[email][email protected][/email]>
    13. */
    14. public class SLAPI implements Serializable
    15. {
    16. /**
    17.   *
    18.   */
    19. private static final long serialVersionUID = 1L;
    20. public static <T extends Object> void save(T obj,String path) throws Exception
    21. {
    22. oos.writeObject(obj);
    23. oos.flush();
    24. oos.close();
    25. }
    26. public static <T extends Object> T load(String path) throws Exception
    27. {
    28. T result = (T)ois.readObject(); //This is where the warning is
    29. ois.close();
    30. return result;
    31. }
    32. }
    33.  
    34.  


    Not sure if this is causing anything but for the SLAPI I'm getting a warning:

    Type safety: Unchecked cast from Object to T SLAPI.java /HomeCommand/src/com/gmail/betorages/homecommand line 30 Java Problem
     
Thread Status:
Not open for further replies.

Share This Page