1

I am getting the following error when compiling a Java class in BlueJ.

AuctionManager.java uses unchecked or unsafe operations.

This error is only displayed when the following deserialization code is in one of my functions:

try {
  ObjectInputStream in = new ObjectInputStream(new FileInputStream(Filename));
  ArrayList<AuctionItem> AuctionList = (ArrayList<AuctionItem>) in.readObject();
  in.close();
}
catch (Exception e) {
  e.printStackTrace();
}

Can I please have some information as to why this error is being displayed, and some help to use the deserialization code without the error.

MikO
  • 18,243
  • 12
  • 77
  • 109
user2351151
  • 725
  • 4
  • 12
  • 16

2 Answers2

1

First the message you get is not an error, is it a warning; it doesn't prevent you program from compiling, or running.

As for the source, it is this line:

ArrayList<AuctionItem> AuctionList = (ArrayList<AuctionItem>) in.readObject();

Since you are casting an object (readObject returns an Object) to a parameterized type (ArrayList<AuctionItem>).

zw324
  • 26,764
  • 16
  • 85
  • 118
1

user2351151: You can do this for warning doesn't show:

ArrayList tempList; // without parameterized type 
try {
  ObjectInputStream in = new ObjectInputStream(new FileInputStream(Filename));
  tempList = (ArrayList) in.readObject();
  for (int i = 0; i < tempList.size(); i++) {
    AuctionList.add(i, (AuctionItem)tempList.get(i)); // explict type conversion from Object to AuctionItem
  }
  in.close();
}
catch (Exception e) {
  e.printStackTrace();
}

You must rewrite ArrayList with explict type conversion (you can use temporary ArrayList without parameterized type).

Community
  • 1
  • 1
mkczyk
  • 2,460
  • 2
  • 25
  • 40