Search code examples
javaandroidarraylistandroid-listviewandroid-alertdialog

Not able to shop name based on location I given in AlertDialog


   AllArea.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    //get the place values as popup window
    String[] items = {"All-Area","Cheruvathur", "Kanhangad","Nileswaram","Panathur","Periya","Thrikkaripur","Vellarikkundu"};
    AlertDialog.Builder  builder=new AlertDialog.Builder(context);
    builder.setTitle("Choose Area: ");
    builder.setItems(items, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int item)
    {
    //AllArea.setText(items[item]);
    if(items[item].equals("All-Area"))
    {
    AllArea.setText(items[item].toString());
    //System.out.println(AllArea);
    String getAllArea=AllArea.getText().toString();
    Intent i=new Intent(SecondPage.this,Order.class);
    i.putExtra("getAllArea", getAllArea);
    startActivity(i);
    Toast.makeText(getApplicationContext(),"All-Area",Toast.LENGTH_SHORT).show();
    }
    else if(items[item].equals("Cheruvathur"))
    {
    AllArea.setText(items[item].toString());
    Intent j=new Intent(SecondPage.this,Order.class);
    String getCheruvathur=AllArea.getText().toString();
    j.putExtra("getCheruvathur", getCheruvathur);
    startActivity(j);
    //System.out.println(AllArea);
    Toast.makeText(getApplicationContext(),"Cheruvathur",Toast.LENGTH_SHORT).show();
    }
    }
    }
    });

Not able to view the shop name based on the given location and display its name in another activity,

Please help me to figure out what is the problem

code for the selecting value of shop based on location

Intent All=getIntent();
String Allnew=All.getStringExtra("getAllArea");
            if (Allnew.equals("All-Area")) {

                list = new ArrayList<>();
                list.add("3G mobile : Chevtr");
                list.add("Spice World : kngd");
                list.add("Dotcom : nslrm");
                list.add("Pv Stores: Pya");
                list.add("Sky World : Pntur");
                list.add("Mobile Galaxy : trkppr");
                list.add("Dream Mobile : vlkdu");
                list.add("Tele World : Chevtr");
                list.add("Info Tech Solutions : Pya");
                list.add("MYG Mobile : trkppr");
              // adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
             // listView.setAdapter(adapter);
            }




 Intent Cheru=getIntent();
        String Cherunew=Cheru.getStringExtra("getCheruvathur");
       if (Cherunew.equals("Cheruvathur")) {

          list1 = new ArrayList<>();
          list1.add("3G mobile : Chevtr");
          list1.add("Tele World : Chevtr");

         //  adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
          // listView.setAdapter(adapter);
       }

please help me to figure out the problem log cat showing this is the error

NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference

i


Solution

  • The problem here is that in your Order class, your code is trying to get StringExtras of both getAllArea and Cheruvathur and this will lead to crash because only one of them exist since you are calling one Intent at a time. So one solution for this is that you can simply check if the Intent's StringExtras are not null and with little modification to your code it will work fine, try this.

    Intent intent=getIntent();
    if(intent.getStringExtra("getAllArea")!= null)
                    list = new ArrayList<>();
                    list.add("3G mobile : Chevtr");
                    list.add("Spice World : kngd");
                    list.add("Dotcom : nslrm");
                    list.add("Pv Stores: Pya");
                    list.add("Sky World : Pntur");
                    list.add("Mobile Galaxy : trkppr");
                    list.add("Dream Mobile : vlkdu");
                    list.add("Tele World : Chevtr");
                    list.add("Info Tech Solutions : Pya");
                    list.add("MYG Mobile : trkppr");
                  // adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
                 // listView.setAdapter(adapter);
                } 
    
    else if(intent.getStringExtra("Cheruvathur")!= null)
              list1 = new ArrayList<>();
              list1.add("3G mobile : Chevtr");
              list1.add("Tele World : Chevtr"); 
    
             //  adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
              // listView.setAdapter(adapter);
           }