Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot Access LinkedHashMap in inner class
    primarykey
    data
    text
    <p>I have the following code:</p> <pre><code>final LinkedHashMap&lt;String, Line&gt; trainLinesMap = MetraRail.myDbHelper.getTrainLinesHashMap(); // Create an array of proper line names for the listview adapter. String[] train_lines_long_names = new String[trainLinesMap.size()]; Iterator&lt;Entry&lt;String, Line&gt;&gt; it = trainLinesMap.entrySet().iterator(); for (int i = 0; it.hasNext(); i++) { Map.Entry&lt;String, Line&gt; pairs = (Map.Entry&lt;String, Line&gt;) it.next(); train_lines_long_names[i] = (String) pairs.getKey(); } // Override the default list adapter so we can do 2 things: 1) set custom background colors // on each item, and 2) so we can more easily add onclick handlers to each item. listview.setAdapter( new ArrayAdapter&lt;String&gt;(this, R.layout.select_line_row_layout, R.id.select_line_line_label, train_lines_long_names) { @Override public View getView(int position, View convertView, ViewGroup parent) { TextView textView = (TextView) super.getView(position, convertView, parent); // Change the background color of each item in the list. final String line_label_long = textView.getText().toString(); final int line_color = trainLinesMap.get(line_label_long).getColorInt(); textView.setBackgroundColor(line_color); // Add onclick handlers to each item. textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(); i.setClassName("garrettp.metrarail", "garrettp.metrarail.screens.SelectStations"); i.putExtra("garrettp.metrarail.line.short", trainLinesMap.get(line_label_long).getShortName()); i.putExtra("garrettp.metrarail.line.long", line_label_long); i.putExtra("garrettp.metrarail.line.color", line_color); startActivity(i); } }); return textView; } }); </code></pre> <p>At the line:</p> <pre><code>final int line_color = trainLinesMap.get(line_label_long).getColorInt(); </code></pre> <p>I get a NullPointerException:</p> <pre><code>10-26 16:10:35.922: E/AndroidRuntime(1785): java.lang.NullPointerException 10-26 16:10:35.922: E/AndroidRuntime(1785): at garrettp.metrarail.screens.SelectLine$1.getView(SelectLine.java:74) </code></pre> <p>Why is this? In a debugger, I've verified that trainLinesMap is correctly initialized and filled with values. It is successfully iterated over in the first for loop, so I know there are values there. But, when accessing the LinkedHashMap from my anonymous inner class, it is always null.</p> <p>I was able to access a String array from this inner class, why can't I access a LinkedHashMap?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload