Android: NPE: general enhancements

Created on 23 Feb 2018  路  11Comments  路  Source: nextcloud/android

From google play console:

Caused by: java.lang.NullPointerException: 
  at com.owncloud.android.ui.activity.FileDisplayActivity$DownloadFinishReceiver.onReceive (FileDisplayActivity.java:1432)

which is
getStorageManager().getFileByPath(mWaitingToSend.getRemotePath())

This method can return null if the file does not exist in our database.

We could

  • add @Nullable to method declaration
  • throw IllegalArgumentException, which does not need to be handled
  • throw FileNotFoundException, which needs to be handled

This is just an example, there are a lot more functions where we might return null and then have to check with "returnedObject != null".

This is not something we need to address now and should be discussed on case by case.

approved enhancement

Most helpful comment

@tanvidadu if you want to start with this, I am happy to help you.
As you said it is the best to have one commit for each case, so in doubt we can just kill one commit.

All 11 comments

I can start working on this issue and add commit for each fix case by case

Thanks @tanvidadu, but we should first clarify what do of the 3 options we use.
What would you prefer?

Sometimes it is even acceptable that no file exists already, e.g. in case of creating a folder...

I prefer option FileNotFoundException as it will allow us to take appropriate action as an when required like display an alert etc.

Here we use the null check on purpose...
But for me a real FileNotFoundException is also the best.

https://github.com/nextcloud/android/blob/b6e84b66fd72e2dd86caae00cb3788036fd12008/src/main/java/com/owncloud/android/operations/CreateFolderOperation.java#L100-L102

@mario @AndyScherzinger your opinions?
If we all agree on this, then @tanvidadu can start :-)

I also agree with you @tanvidadu @tobiasKaminsky -> FileNotFoundException is the appropriate one :+1:

@tanvidadu if you want to start with this, I am happy to help you.
As you said it is the best to have one commit for each case, so in doubt we can just kill one commit.

So this is happening quite some time according to google play console:

https://github.com/nextcloud/android/blob/c1fe52bbf81f374803fd94fe071c3b905b67167f/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java#L358-L364

mLocalFolder is null and we did not check this...

I will start working on the issue. Is there specific action that needs to be taken while handling exception ?

I think it depends on the case

  • some, like creating folder, are needed checks, so they are rather easy
  • some are NPE can be silently catched and ignored
  • some NPE cannot be resolved and should be presented somehow to the user, e.g. if you receive a shared file and a NPE happens (just an example, not sure if this could happen)

If you are unsure, just ask here :+1:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JSoko picture JSoko  路  3Comments

eppfel picture eppfel  路  3Comments

tobiasKaminsky picture tobiasKaminsky  路  3Comments

tobiasKaminsky picture tobiasKaminsky  路  3Comments

toobie83 picture toobie83  路  3Comments