Skip to content

Commit 12ac3f3

Browse files
committed
Fixed NPE when unknown platform/board are selected in preference
1 parent 69b31ba commit 12ac3f3

File tree

2 files changed

+49
-29
lines changed

2 files changed

+49
-29
lines changed

app/src/processing/app/Base.java

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,8 @@ protected void rebuildSketchbookMenu(JMenu menu) {
970970
}
971971

972972
public Map<String, File> getIDELibs() {
973+
if (libraries == null)
974+
return new HashMap<String, File>();
973975
Map<String, File> ideLibs = new HashMap<String, File>(libraries);
974976
for (String lib : libraries.keySet()) {
975977
if (FileUtils.isSubDirectory(getSketchbookFolder(), libraries.get(lib)))
@@ -979,6 +981,8 @@ public Map<String, File> getIDELibs() {
979981
}
980982

981983
public Map<String, File> getUserLibs() {
984+
if (libraries == null)
985+
return new HashMap<String, File>();
982986
Map<String, File> userLibs = new HashMap<String, File>(libraries);
983987
for (String lib : libraries.keySet()) {
984988
if (!FileUtils.isSubDirectory(getSketchbookFolder(), libraries.get(lib)))
@@ -1002,34 +1006,37 @@ public void actionPerformed(ActionEvent e) {
10021006
importMenu.add(addLibraryMenuItem);
10031007

10041008
// Split between user supplied libraries and IDE libraries
1005-
Map<String, File> ideLibs = getIDELibs();
1006-
Map<String, File> userLibs = getUserLibs();
1007-
try {
1008-
// Find the current target. Get the platform, and then select the
1009-
// correct name and core path.
1010-
PreferencesMap prefs = getTargetPlatform().getPreferences();
1011-
String targetname = prefs.get("name");
1012-
1013-
if (false) {
1014-
// Hack to extract these words by gettext tool.
1015-
// These phrases are actually defined in the "platform.txt".
1016-
String notused = _("Arduino AVR Boards");
1017-
notused = _("Arduino ARM (32-bits) Boards");
1018-
}
1009+
TargetPlatform targetPlatform = getTargetPlatform();
1010+
if (targetPlatform != null) {
1011+
Map<String, File> ideLibs = getIDELibs();
1012+
Map<String, File> userLibs = getUserLibs();
1013+
try {
1014+
// Find the current target. Get the platform, and then select the
1015+
// correct name and core path.
1016+
PreferencesMap prefs = targetPlatform.getPreferences();
1017+
String targetname = prefs.get("name");
1018+
1019+
if (false) {
1020+
// Hack to extract these words by gettext tool.
1021+
// These phrases are actually defined in the "platform.txt".
1022+
String notused = _("Arduino AVR Boards");
1023+
notused = _("Arduino ARM (32-bits) Boards");
1024+
}
10191025

1020-
JMenuItem platformItem = new JMenuItem(_(targetname));
1021-
platformItem.setEnabled(false);
1022-
importMenu.add(platformItem);
1023-
if (ideLibs.size()>0) {
1024-
importMenu.addSeparator();
1025-
addLibraries(importMenu, ideLibs);
1026-
}
1027-
if (userLibs.size()>0) {
1028-
importMenu.addSeparator();
1029-
addLibraries(importMenu, userLibs);
1026+
JMenuItem platformItem = new JMenuItem(_(targetname));
1027+
platformItem.setEnabled(false);
1028+
importMenu.add(platformItem);
1029+
if (ideLibs.size() > 0) {
1030+
importMenu.addSeparator();
1031+
addLibraries(importMenu, ideLibs);
1032+
}
1033+
if (userLibs.size() > 0) {
1034+
importMenu.addSeparator();
1035+
addLibraries(importMenu, userLibs);
1036+
}
1037+
} catch (IOException e) {
1038+
e.printStackTrace();
10301039
}
1031-
} catch (IOException e) {
1032-
e.printStackTrace();
10331040
}
10341041
}
10351042

@@ -1132,11 +1139,15 @@ public File scanFatLibrary(File libFolder) {
11321139
}
11331140

11341141
public void onBoardOrPortChange() {
1142+
TargetPlatform targetPlatform = getTargetPlatform();
1143+
if (targetPlatform == null)
1144+
return;
1145+
11351146
// Calculate paths for libraries and examples
11361147
examplesFolder = getContentFile("examples");
11371148
toolsFolder = getContentFile("tools");
11381149

1139-
File platformFolder = getTargetPlatform().getFolder();
1150+
File platformFolder = targetPlatform.getFolder();
11401151
librariesFolders = new ArrayList<File>();
11411152
librariesFolders.add(getContentFile("libraries"));
11421153
librariesFolders.add(new File(platformFolder, "libraries"));
@@ -1400,6 +1411,9 @@ public void actionPerformed(ActionEvent actionevent) {
14001411
*/
14011412
protected boolean addSketches(JMenu menu, File folder,
14021413
final boolean replaceExisting) throws IOException {
1414+
if (folder == null)
1415+
return false;
1416+
14031417
// skip .DS_Store files, etc (this shouldn't actually be necessary)
14041418
if (!folder.isDirectory()) return false;
14051419

@@ -1846,7 +1860,10 @@ static public TargetPlatform getTargetPlatform() {
18461860
*/
18471861
static public TargetPlatform getTargetPlatform(String packageName,
18481862
String platformName) {
1849-
return packages.get(packageName).get(platformName);
1863+
TargetPackage p = packages.get(packageName);
1864+
if (p == null)
1865+
return null;
1866+
return p.get(platformName);
18501867
}
18511868

18521869
static public TargetPlatform getCurrentTargetPlatformFromPackage(String pack) {

app/src/processing/app/Editor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,10 @@ public void menuSelected(MenuEvent e) {
727727

728728

729729
protected void addTools(JMenu menu, File sourceFolder) {
730-
HashMap<String, JMenuItem> toolItems = new HashMap<String, JMenuItem>();
730+
if (sourceFolder == null)
731+
return;
732+
733+
Map<String, JMenuItem> toolItems = new HashMap<String, JMenuItem>();
731734

732735
File[] folders = sourceFolder.listFiles(new FileFilter() {
733736
public boolean accept(File folder) {

0 commit comments

Comments
 (0)