Skip to content

Commit 518933d

Browse files
authored
Refactor onActivityResult() method to handle NullPointerExceptions (#1117)
This commit rewrites the onActivityResult() method in order to handle possible NullPointerExceptions that can occur when accessing data.getData(). The changes include adding try-catch blocks to handle exceptions gracefully and prevent app crashes. Additionally, comments have been added to the method to explain what each block of code is doing.
1 parent 771b804 commit 518933d

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

app/src/main/java/swati4star/createpdf/fragment/RemoveDuplicatePagesFragment.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,27 @@ public void showFileChooser() {
105105
startActivityForResult(mFileUtils.getFileChooser(),
106106
INTENT_REQUEST_PICKFILE_CODE);
107107
}
108-
109-
public void onActivityResult(int requestCode, int resultCode, Intent data) throws NullPointerException {
110-
if (data == null || resultCode != RESULT_OK || data.getData() == null)
111-
return;
112-
if (requestCode == INTENT_REQUEST_PICKFILE_CODE) {
113-
//Getting Absolute Path
108+
109+
// Refactor onActivityResult() method to handle possible NullPointerExceptions
110+
// when accessing data.getData(). Added try-catch blocks to handle exceptions
111+
// in a better way(imo) to prevent app crashes.
112+
@Override
113+
public void onActivityResult(int requestCode, int resultCode, Intent data) {
114+
if (resultCode != RESULT_OK || data == null || data.getData() == null) {
115+
return;
116+
}
117+
118+
if (requestCode == INTENT_REQUEST_PICKFILE_CODE) {
119+
try {
120+
// Attempt to get the real path of the selected file and update the UI
114121
String path = RealPathUtil.getInstance().getRealPath(getContext(), data.getData());
115122
setTextAndActivateButtons(path);
123+
} catch (NullPointerException e) {
124+
// If a NullPointerException occurs, log it to the console and continue
125+
e.printStackTrace();
116126
}
117127
}
128+
}
118129

119130
//On click remove duplicate button
120131
@OnClick(R.id.remove)
@@ -205,4 +216,4 @@ private void getRuntimePermissions() {
205216
WRITE_PERMISSIONS,
206217
REQUEST_CODE_FOR_WRITE_PERMISSION);
207218
}
208-
}
219+
}

0 commit comments

Comments
 (0)