From 56b9f1cd6fc24e09f56cbd234fd1c35d0a74c0c4 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 1 May 2014 16:17:43 +0200 Subject: [PATCH] Fixed NPE when currently selected platform is no more installed. BaseNoGui.getTargetBoard() now handles null TargetBoard. Removed unused method Base.getTargetBoard() --- app/src/processing/app/Base.java | 9 ++--- app/src/processing/app/EditorLineStatus.java | 33 +++++++++------- .../src/processing/app/BaseNoGui.java | 38 ++++++++++--------- 3 files changed, 44 insertions(+), 36 deletions(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index e5f593997..61752543c 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -1077,6 +1077,10 @@ public class Base { } public void rebuildBoardsMenu(JMenu toolsMenu, Editor editor) throws Exception { + // If there are no platforms installed skip menu creation + if (BaseNoGui.packages.size() == 0) + return; + JMenu boardsMenu = getBoardCustomMenu(); boolean first = true; @@ -1698,11 +1702,6 @@ public class Base { return BaseNoGui.getBoardPreferences(); } - public static TargetBoard getTargetBoard() { - String boardId = Preferences.get("board"); - return getTargetPlatform().getBoard(boardId); - } - static public File getPortableFolder() { return BaseNoGui.getPortableFolder(); } diff --git a/app/src/processing/app/EditorLineStatus.java b/app/src/processing/app/EditorLineStatus.java index 2ef4e8edb..8420ddd4c 100644 --- a/app/src/processing/app/EditorLineStatus.java +++ b/app/src/processing/app/EditorLineStatus.java @@ -1,5 +1,3 @@ -/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ - /* Part of the Processing project - http://processing.org @@ -23,18 +21,23 @@ package processing.app; import processing.app.helpers.OSUtils; -import processing.app.syntax.*; -import java.awt.*; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.Graphics; +import java.awt.Image; import java.awt.geom.Rectangle2D; -import java.util.Map; -import javax.swing.*; +import javax.swing.JComponent; +import processing.app.helpers.PreferencesMap; +import processing.app.syntax.JEditTextArea; /** * Li'l status bar fella that shows the line number. */ +@SuppressWarnings("serial") public class EditorLineStatus extends JComponent { JEditTextArea textarea; int start = -1, stop; @@ -52,7 +55,6 @@ public class EditorLineStatus extends JComponent { String name = ""; String serialport = ""; - public EditorLineStatus(JEditTextArea textarea) { this.textarea = textarea; textarea.editorLineStatus = this; @@ -70,7 +72,6 @@ public class EditorLineStatus extends JComponent { //linestatus.color = #FFFFFF } - public void set(int newStart, int newStop) { if ((newStart == start) && (newStop == stop)) return; @@ -93,11 +94,10 @@ public class EditorLineStatus extends JComponent { repaint(); } - public void paintComponent(Graphics g) { - if (name=="" && serialport=="") { - Map boardPreferences = Base.getBoardPreferences(); - if (boardPreferences!=null) + if (name == "" && serialport == "") { + PreferencesMap boardPreferences = Base.getBoardPreferences(); + if (boardPreferences != null) setBoardName(boardPreferences.get("name")); else setBoardName("-"); @@ -124,8 +124,13 @@ public class EditorLineStatus extends JComponent { } } - public void setBoardName(String name) { this.name = name; } - public void setSerialPort(String serialport) { this.serialport = serialport; } + public void setBoardName(String name) { + this.name = name; + } + + public void setSerialPort(String serialport) { + this.serialport = serialport; + } public Dimension getPreferredSize() { return new Dimension(300, high); diff --git a/arduino-core/src/processing/app/BaseNoGui.java b/arduino-core/src/processing/app/BaseNoGui.java index 6bf376274..e922a9b01 100644 --- a/arduino-core/src/processing/app/BaseNoGui.java +++ b/arduino-core/src/processing/app/BaseNoGui.java @@ -152,6 +152,8 @@ public class BaseNoGui { static public PreferencesMap getBoardPreferences() { TargetBoard board = getTargetBoard(); + if (board == null) + return null; PreferencesMap prefs = new PreferencesMap(board.getPreferences()); for (String menuId : board.getMenuIds()) { @@ -343,8 +345,11 @@ public class BaseNoGui { } public static TargetBoard getTargetBoard() { + TargetPlatform targetPlatform = getTargetPlatform(); + if (targetPlatform == null) + return null; String boardId = PreferencesData.get("board"); - return getTargetPlatform().getBoard(boardId); + return targetPlatform.getBoard(boardId); } /** @@ -669,28 +674,27 @@ public class BaseNoGui { } static public void onBoardOrPortChange() { - TargetPlatform targetPlatform = getTargetPlatform(); - if (targetPlatform == null) - return; - - // Calculate paths for libraries and examples examplesFolder = getContentFile("examples"); toolsFolder = getContentFile("tools"); - - File platformFolder = targetPlatform.getFolder(); librariesFolders = new ArrayList(); librariesFolders.add(getContentFile("libraries")); - String core = getBoardPreferences().get("build.core"); - if (core.contains(":")) { - String referencedCore = core.split(":")[0]; - TargetPlatform referencedPlatform = getTargetPlatform(referencedCore, targetPlatform.getId()); - if (referencedPlatform != null) { - File referencedPlatformFolder = referencedPlatform.getFolder(); - librariesFolders.add(new File(referencedPlatformFolder, "libraries")); + + // Add library folder for the current selected platform + TargetPlatform targetPlatform = getTargetPlatform(); + if (targetPlatform != null) { + String core = getBoardPreferences().get("build.core"); + if (core.contains(":")) { + String referencedCore = core.split(":")[0]; + TargetPlatform referencedPlatform = getTargetPlatform(referencedCore, targetPlatform.getId()); + if (referencedPlatform != null) { + File referencedPlatformFolder = referencedPlatform.getFolder(); + librariesFolders.add(new File(referencedPlatformFolder, "libraries")); + } } + File platformFolder = targetPlatform.getFolder(); + librariesFolders.add(new File(platformFolder, "libraries")); + librariesFolders.add(getSketchbookLibrariesFolder()); } - librariesFolders.add(new File(platformFolder, "libraries")); - librariesFolders.add(getSketchbookLibrariesFolder()); // Scan for libraries in each library folder. // Libraries located in the latest folders on the list can override