3

In an app I am using SharedPrefernces to save/load (serialize/deserialize) some objects.

This is the deserialization code:

private void loadData() {

    String str = sharedPreferences.getString(PREF_TAG, null);

    byte[] bytes = Base64.decode(str, Base64.DEFAULT);

    try {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        ObjectInputStream input = new ObjectInputStream(bais);
        arrayOfObjects = (ArrayList<MyObject>) input.readObject();
    } catch (Exception e) {
        Log.i("BUG", "error decoding serialized objects: " + e.toString());
    }

    if (arrayOfObjects == null) {
        Log.i("BUG", "serious problem!");
    }

}

But whenever I compile this project, the line:

arrayOfObjects = (ArrayList<MyObject>) input.readObject();

causes a warning that the class containing this method "uses unchecked or unsafe operations."

How can I get rid of this warning or change my code to be more safe?

Guillaume S
  • 1,462
  • 2
  • 19
  • 31
Nerdy Bunz
  • 6,040
  • 10
  • 41
  • 100
  • Did you check this https://stackoverflow.com/questions/197986/what-causes-javac-to-issue-the-uses-unchecked-or-unsafe-operations-warning and this https://stackoverflow.com/questions/23749786/uses-unchecked-or-unsafe-operations – AskNilesh Sep 04 '18 at 10:01

4 Answers4

12

In this case that warinig is shown because you are parsing directly the result of

input.readObject();

That returns an Object of type Object, (that is so generic), into an ArrayList, and the compiler tries to say you that, it could be any other type of objects.

In my opinion its not an important warning if that instruction in your code is always returning ArrayList, so i would add to your method definition.

@SuppressWarnings("unchecked")
Oldskultxo
  • 945
  • 8
  • 19
5

I had the same problem and solved it by adding :

@SuppressWarnings("unchecked")
public class className extends AppCompatActivity {
...
}

I hope this will help

Jhakiz
  • 1,519
  • 9
  • 16
0

I solved this problem with add :

@SuppressWarnings("unchecked")
public class CommLockInfoManager {
Sana Ebadi
  • 6,656
  • 2
  • 44
  • 44
0

To everyone who gave answer earlier. If you write

@SuppressWarnings("unchecked")

You tell to java: "Please not check what is under the command." This is not solution this error. If you want to the solve the problem you must don't use interface and select some name class.

rates336
  • 11
  • 1
  • 3
  • For this particular scenario, using this annotation is okay, because we know why the compiler is showing a warning and the reason behind it. So we can suppress that. – Abhishek Dutt Mar 06 '22 at 04:14
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31197292) – Abhishek Dutt Mar 06 '22 at 04:14