tree hh
tree hh

Reputation: 269

Get value in HashMap Android

I have two activitys... Activity 1: have a edittext and two button:button1 & button2 Activity 2: have a autocompletetextview and a listview

I want to that when i enter string to edittext and click button1 ,i create a HashMap(static) to save value(set defaul="hello word") and key as string in edittext... Event when click button1:

button.setOnClickListener(new OnClickListener() {       
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
  hashMap.put(edittext.getText().toString(),"hello word");//I declared hashMap  
}
});

In activity2 :

public class Notification_App extends Activity {

    AutoCompleteTextView actv_notification;
    ListView lv_notification;
    Dictionary1 dictionary1;
    ArrayList<String> str_value;
    ArrayList<String> array_key;

@Override
public void onCreate(Bundle circle)
{
    super.onCreate(circle);
    setContentView(R.layout.notification);

    actv_notification=(AutoCompleteTextView)findViewById(R.id.actv_notification);
    lv_notification=(ListView)findViewById(R.id.listview_notification);

    array_key=new ArrayList<String>(dictionary1.hashMap.keySet());
    Iterator myVeryOwnIterator = dictionary1.hashMap.keySet().iterator();
    while(myVeryOwnIterator.hasNext())
    {
        String key=(String)myVeryOwnIterator.next();
        String value=(String)dictionary1.hashMap.get(key);
        str_value.add(value);
    }

    ArrayAdapter<String> adapter=new ArrayAdapter<String>(Notification_App.this, android.R.layout.simple_dropdown_item_1line,array_key);
    ArrayAdapter<String> adapter1=new ArrayAdapter<String>(Notification_App.this, android.R.layout.simple_list_item_1,array_key);


    actv_notification.setAdapter(adapter);
    lv_notification.setAdapter(adapter1);
    lv_notification.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
    long id) {
    // TODO Auto-generated method stub
    Toast.makeText(getBaseContext(), str_value.get(position), Toast.LENGTH_LONG).show();
    }
    });     
}

but error display: null poiter at line: str_value.add(value);...Can you help me..

Upvotes: 0

Views: 1260

Answers (3)

Skies
Skies

Reputation: 415

Try to add

str_value = new ArrayList<String>();

Before the first

str_value.add(value);

Upvotes: 0

Tony the Pony
Tony the Pony

Reputation: 41347

You're getting an exception because str_value is null.

You need to create the actual object:

    str_value = new ArrayList<String>();

Upvotes: 0

Aleks G
Aleks G

Reputation: 57306

I think you just forgot to initialise str_value variable. Put before your while loop:

str_value = new ArrayList<String>();

and it should do the trick.

P.S. I'm not checking the logic of your code, simply pointing out why you get NullPointerException.

Upvotes: 2

Related Questions