-1

I'm trying to add a string to a listview, the string is being received via the broadcast receiver. whenever I try to do something on my arraylist or an adapter to my listview I get a null pointer exception.

public class DevScan extends AppCompatActivity {


MyReceiver rcvr;
String test;
ArrayList<String> devlist;
ArrayAdapter<String> Adapter;
ListView devLV;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dev_scan);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    rcvr=new MyReceiver();
    this.registerReceiver(rcvr, new    IntentFilter("CUSTOM_INTENT"));





    final UDP_service srv = new UDP_service(this);
    srv.start();


    final ArrayList<String> devlist = new ArrayList<>();
    ListView devLV = (ListView) findViewById(R.id.devlistview);

    devlist.add("elloooo");

    final ArrayAdapter<String> Adapter = new ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_1,
            devlist );

    devLV.setAdapter(Adapter);


    Button scanb1 = (Button) findViewById(R.id.scanbtn1);
    scanb1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            devlist.clear();
            Adapter.notifyDataSetChanged();


            srv.Message="eloooo!";
            srv.send();



        }
    });

}

private class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent){
    test = intent.getExtras().getString("packet");
    //it works fine when I put the Extra in a string variable

      //these 3 cause NPE
     // devlist.add(intent.getExtras().getString("packet"));
     //   devlist.add("foo");
     //   Adapter.notifyDataSetChanged();


    Log.i("SOCK broadcast intent", test);

}
}


}

what I don't undsetand is why string variable 'test' is works fine with MyReceiver, but arraylist defined in the same way doesn't

soluton:

because of my lack of knowlage I redeclared my objects as local, by accident..

  • 1
    Do you know such java's basics as variable's scope? http://ideone.com/HZ05SY – Selvin May 18 '16 at 15:13
  • Add this to DevScan `onCreate()`: `devlist=new ArrayList();` – NezSpencer May 18 '16 at 15:26
  • I do have 'devlist=new ArrayList();' in 'onCreate()' – Barricadexx May 18 '16 at 16:31
  • You are hiding the member variable 'devlist' by redefining it inside of onCreate() so yes, you have an instantiated object in that class just as a simple local variable, but when you get to onReceive() it has the scope of the class itself so when it tries to make the call to add() it finds the devlist variable is still null. If you wanted to initialize the member variable 'devlist' inside of onCreate() you should do this: – Kelly S. French May 18 '16 at 18:38
  • You are hiding the member variable 'devlist' by redefining it inside of onCreate() so yes, you have an instantiated object in that class just as a simple local variable, but when you get to onReceive() it has the scope of the class itself so when it tries to make the call to add() it finds the devlist variable is still null. If instead you wanted to initialize the member variable 'devlist' inside of onCreate() you should do this: " devlist = new ArrayList<>();". – Kelly S. French May 18 '16 at 18:40

1 Answers1

0
There are several possible errors in your code

1) The variables MyReceiver rcvr; String test; ArrayList<String> devlist;
ArrayAdapter<String> Adapter; and ListView devLV; are decleared but not initialize.

In your onCreate the above variable was initialize but with final constrains. You should initialize your variables on 
before rcvr=new MyReceiver(); that is

final UDP_service srv = new UDP_service(this);
srv.start();


devlist = new ArrayList<>();
devLV = (ListView) findViewById(R.id.devlistview);

devlist.add("elloooo");

Adapter = new ArrayAdapter<String>(
        this,
        android.R.layout.simple_list_item_1,
        devlist );

devLV.setAdapter(Adapter);

new MyReceiver().onReceive(getApplicationContext(), new Intent().putExtra("packet", "expectedString"));
//it should work
2)Try 1 let see what happens
Franklyn
  • 315
  • 2
  • 8