jung6717 / arduino

Automatically exported from code.google.com/p/arduino
0 stars 0 forks source link

NullPointerException when board not selected #235

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Open Arduino with no default board selected
2. Try to Compile or Upload

What version of the Arduino software are you using? On what operating
system?  Which Arduino board are you using?
0018 on Linux (ubuntu karmic) with any (or no) arduino board

Please provide any additional information below.

I've attached a patch which tries to catch this error and either pass it 
more gently or open a warning dialog; I strongly recommend refactoring 
around this problem. 
The contents of this patch are released to the public domain and can be 
included under any license blah blah.

Original issue reported on code.google.com by bnewbold on 28 Apr 2010 at 4:13

Attachments:

GoogleCodeExporter commented 9 years ago
Many of these null pointer exceptions happen because getBoardPreferences isn't
checking if getTarget returns null before using it, and even if target exists,
returning null isn't an option since almost every use is a pointer to call 
get() for
a specific board pref.

Just adding a couple checks in getBoardPreferences() eliminates the need for 
many of
those try/catch blocks.  It's only a few lines, changing from this:

  static public Map<String, String> getBoardPreferences() {
    return getTarget().getBoards().get(Preferences.get("board"));
  }

to something like this:

  static public Map<String, String> getBoardPreferences() {
    Target target = getTarget();
    if (target == null) return new LinkedHashMap();
    Map map = getTarget().getBoards().get(Preferences.get("board"));
    if (map == null) return new LinkedHashMap();
    return map;
  }

Of course, there are still some places not checking if
boardPreferences.get("something") is null, when they really should.  For 
example, in
Compiler, core should be checked, like this:

    String core = boardPreferences.get("build.core");
    if (core == null) {
      Base.showWarning("No board selected", "Please choose a Board from the Tools
menu", null);
      return false;
    }

Original comment by paul.sto...@gmail.com on 30 Apr 2010 at 7:41

GoogleCodeExporter commented 9 years ago
Here is a patch

Original comment by paul.sto...@gmail.com on 12 Jun 2010 at 5:59

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks for the patch.  I modified it a bit and applied it.

Original comment by dmel...@gmail.com on 12 Jun 2010 at 6:32