Skip to content

Commit dbf8477

Browse files
Fix potential null exceptions
1 parent 324a69f commit dbf8477

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

app/src/main/java/com/termux/app/TermuxActivity.java

+19-8
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ public final class TermuxActivity extends Activity implements ServiceConnection
105105
private static final String BROADCAST_TERMUX_OPENED = TermuxConstants.TERMUX_PACKAGE_NAME + ".app.OPENED";
106106

107107
/** The main view of the activity showing the terminal. Initialized in onCreate(). */
108-
@SuppressWarnings("NullableProblems")
109-
@NonNull
110108
TerminalView mTerminalView;
111109

112110
ExtraKeysView mExtraKeysView;
@@ -366,7 +364,7 @@ void sendOpenedBroadcast() {
366364
for (ResolveInfo info : matches) {
367365
Intent explicitBroadcast = new Intent(broadcast);
368366
ComponentName cname = new ComponentName(info.activityInfo.applicationInfo.packageName,
369-
info.activityInfo.name);
367+
info.activityInfo.name);
370368
explicitBroadcast.setComponent(cname);
371369
sendBroadcast(explicitBroadcast);
372370
}
@@ -488,7 +486,10 @@ public View getView(int position, View convertView, @NonNull ViewGroup parent) {
488486
}
489487

490488
TerminalSession sessionAtRow = getItem(position);
491-
boolean sessionRunning = sessionAtRow.isRunning();
489+
if (sessionAtRow == null) return row;
490+
491+
boolean sessionRunning = false;
492+
sessionRunning = sessionAtRow.isRunning();
492493

493494
TextView firstLineView = row.findViewById(R.id.row_line);
494495
if (mIsUsingBlackUI) {
@@ -577,6 +578,7 @@ public void switchToSession(boolean forward) {
577578

578579
@SuppressLint("InflateParams")
579580
void renameSession(final TerminalSession sessionToRename) {
581+
if (sessionToRename == null) return;
580582
DialogUtils.textInput(this, R.string.session_rename_title, sessionToRename.mSessionName, R.string.session_rename_positive_button, text -> {
581583
sessionToRename.mSessionName = text;
582584
mListViewAdapter.notifyDataSetChanged();
@@ -629,6 +631,7 @@ protected void onStop() {
629631
getDrawer().closeDrawers();
630632
}
631633

634+
@SuppressLint("RtlHardcoded")
632635
@Override
633636
public void onBackPressed() {
634637
if (getDrawer().isDrawerOpen(Gravity.LEFT)) {
@@ -825,7 +828,10 @@ static LinkedHashSet<CharSequence> extractUrls(String text) {
825828
}
826829

827830
void showUrlSelection() {
828-
String text = getCurrentTermSession().getEmulator().getScreen().getTranscriptTextWithFullLinesJoined();
831+
String text = null;
832+
if (getCurrentTermSession() != null) {
833+
text = getCurrentTermSession().getEmulator().getScreen().getTranscriptTextWithFullLinesJoined();
834+
}
829835
LinkedHashSet<CharSequence> urlSet = extractUrls(text);
830836
if (urlSet.isEmpty()) {
831837
new AlertDialog.Builder(this).setMessage(R.string.select_url_no_found).show();
@@ -896,11 +902,14 @@ public boolean onContextItemSelected(MenuItem item) {
896902
return true;
897903
case CONTEXTMENU_KILL_PROCESS_ID:
898904
final AlertDialog.Builder b = new AlertDialog.Builder(this);
905+
final TerminalSession terminalSession = getCurrentTermSession();
906+
if (terminalSession == null) return true;
907+
899908
b.setIcon(android.R.drawable.ic_dialog_alert);
900909
b.setMessage(R.string.confirm_kill_process);
901910
b.setPositiveButton(android.R.string.yes, (dialog, id) -> {
902911
dialog.dismiss();
903-
getCurrentTermSession().finishIfRunning();
912+
terminalSession.finishIfRunning();
904913
});
905914
b.setNegativeButton(android.R.string.no, null);
906915
b.show();
@@ -952,7 +961,7 @@ public boolean onContextItemSelected(MenuItem item) {
952961
}
953962

954963
@Override
955-
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
964+
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
956965
if (requestCode == REQUESTCODE_PERMISSION_STORAGE && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
957966
TermuxInstaller.setupStorageSymlinks(this);
958967
}
@@ -969,7 +978,9 @@ void doPaste() {
969978
if (clipData == null) return;
970979
CharSequence paste = clipData.getItemAt(0).coerceToText(this);
971980
if (!TextUtils.isEmpty(paste))
972-
getCurrentTermSession().getEmulator().paste(paste.toString());
981+
if (getCurrentTermSession() != null) {
982+
getCurrentTermSession().getEmulator().paste(paste.toString());
983+
}
973984
}
974985

975986
/** The current session as stored or the last one if that does not exist. */

terminal-emulator/src/main/java/com/termux/terminal/TerminalOutput.java

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public abstract class TerminalOutput {
77

88
/** Write a string using the UTF-8 encoding to the terminal client. */
99
public final void write(String data) {
10+
if (data == null) return;
1011
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
1112
write(bytes, 0, bytes.length);
1213
}

0 commit comments

Comments
 (0)