Jump to content

Soooo I feel quite dumb. I used to be good at this, but there's a simple bug that's bothering me.

 

One of the apps I'm coding gets input from an AlertDialog (3 EditTexts) and saves it into 3 variables. Dialog loads without problems (using custom view), but when pressing "Add", logcat gives me an error:

 

java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference

 

Thing is, it shouldn't be null as I declared it and assigned it to a variable. What am I missing here? (IDs are correct, if you need to know)

 

The Activity:

Spoiler

public class itemList extends AppCompatActivity{

    String name = "";
    int price = 0;
    int qty = 0;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_list);

        FloatingActionButton btn1 = (FloatingActionButton) findViewById(R.id.button);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                popItUp();
            }
        });

    }
    
    void popItUp(){

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Enter Order");
        builder.setView(getLayoutInflater().inflate(R.layout.popup, null));

        final EditText a = (EditText) findViewById(R.id.popEdit1);
        final EditText b = (EditText) findViewById(R.id.popEdit2);
        final EditText c = (EditText) findViewById(R.id.popEdit3);

        builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                name = a.getText().toString();							// Logcat gives the null object reference error on this line
                price = Integer.parseInt(b.getText().toString());
                qty = Integer.parseInt(c.getText().toString());
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        builder.show();
        
    }

}

 

 

Computer Case: NZXT S340 || CPU: AMD Ryzen 5 1600 || Cooler: CM Hyper212 Evo || MoBo: MSI B350 Mortar || RAM Vengeance LPX 2x8GB 3200MHz || PSU: Corsair CX600 || SSD: HyperX Fury 120GB & 240GB || HDD: WD Blue 1TB + 1TB 2.5'' backup drive || GPU: Sapphire Nitro+ RX 580 4GB

Laptop 1 HP x360 13-u113nl

Laptop Lenovo z50-75 with AMD FX-7500 || OS: Windows 10 / Ubuntu 17.04

DSLR Nikon D5300 w/ 18-105mm lens

Link to comment
https://linustechtips.com/topic/590806-cant-fix-a-bug-in-java/
Share on other sites

Link to post
Share on other sites

16 minutes ago, Mr_KoKa said:

If you have a way to debug it and write out some informations I would check if any a, b or c isn't null, and if getText function isn't returning null (maybe it does it when edit is empty? idk). Something in the chain is null.

I tried placing the "name = a.getText().toString();" outside the onClick funcion, same error. I think there is a problem with the EditText variable 'a' (also 'b' and 'c').

 

Can't get it to log the value (with Log.d() ), as it gives me the same error.

 

Also tried commenting out each of the getText() functions, to see if this happens also with b and c. it does.

Computer Case: NZXT S340 || CPU: AMD Ryzen 5 1600 || Cooler: CM Hyper212 Evo || MoBo: MSI B350 Mortar || RAM Vengeance LPX 2x8GB 3200MHz || PSU: Corsair CX600 || SSD: HyperX Fury 120GB & 240GB || HDD: WD Blue 1TB + 1TB 2.5'' backup drive || GPU: Sapphire Nitro+ RX 580 4GB

Laptop 1 HP x360 13-u113nl

Laptop Lenovo z50-75 with AMD FX-7500 || OS: Windows 10 / Ubuntu 17.04

DSLR Nikon D5300 w/ 18-105mm lens

Link to comment
https://linustechtips.com/topic/590806-cant-fix-a-bug-in-java/#findComment-7690007
Share on other sites

Link to post
Share on other sites

Most likely IMO:

final EditText a = (EditText) findViewById(R.id.popEdit1);
final EditText b = (EditText) findViewById(R.id.popEdit2);
final EditText c = (EditText) findViewById(R.id.popEdit3);

One of these does not return the view, instead returning null. Check if your id's are right (like case sensitive things and whatnot), maybe you need to rebuild the entire project, because some files that are meant to be auto-regenerated are not.

Link to comment
https://linustechtips.com/topic/590806-cant-fix-a-bug-in-java/#findComment-7690018
Share on other sites

Link to post
Share on other sites

2 minutes ago, DevBlox said:

Most likely IMO:


final EditText a = (EditText) findViewById(R.id.popEdit1);
final EditText b = (EditText) findViewById(R.id.popEdit2);
final EditText c = (EditText) findViewById(R.id.popEdit3);

One of these does not return the view, instead returning null. Check if your id's are right (like case sensitive things and whatnot), maybe you need to rebuild the entire project, because some files that are meant to be auto-regenerated are not.

Spelling is correct, also because Android Studio confirms it by highlighting the id in purple (meaning that it matches)

 

I rebuilt the whole project and it still gives me an error

Computer Case: NZXT S340 || CPU: AMD Ryzen 5 1600 || Cooler: CM Hyper212 Evo || MoBo: MSI B350 Mortar || RAM Vengeance LPX 2x8GB 3200MHz || PSU: Corsair CX600 || SSD: HyperX Fury 120GB & 240GB || HDD: WD Blue 1TB + 1TB 2.5'' backup drive || GPU: Sapphire Nitro+ RX 580 4GB

Laptop 1 HP x360 13-u113nl

Laptop Lenovo z50-75 with AMD FX-7500 || OS: Windows 10 / Ubuntu 17.04

DSLR Nikon D5300 w/ 18-105mm lens

Link to comment
https://linustechtips.com/topic/590806-cant-fix-a-bug-in-java/#findComment-7690036
Share on other sites

Link to post
Share on other sites

11 minutes ago, Cryosec said:

Spelling is correct, also because Android Studio confirms it by highlighting the id in purple (meaning that it matches)

 

I rebuilt the whole project and it still gives me an error

Hmm, another option would be that findViewById returns the view, but the cast is wrong, because what it returns is not EditText, but still a subclass of View, so the compiler is silent.

Link to comment
https://linustechtips.com/topic/590806-cant-fix-a-bug-in-java/#findComment-7690118
Share on other sites

Link to post
Share on other sites

9 minutes ago, Mr_KoKa said:

 

8 minutes ago, DevBlox said:

Hmm, another option would be that findViewById returns the view, but the cast is wrong, because what it returns is not EditText, but still a subclass of View, so the compiler is silent.

Ok so the StackOverflow thread @Mr_KoKa linked helped a lot. The findViewById was searching for a view within the activity hierarchy, so I needed to specify where to look:

 

Spoiler

void popItUp(){

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Enter Order");
        builder.setView(getLayoutInflater().inflate(R.layout.popup, null));
        

        builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            
            	// Here I added the following line and the view where the findViewById should look
                Dialog dialogView = (Dialog) dialog;
                final EditText a = (EditText) dialogView.findViewById(R.id.popEdit1);
                final EditText b = (EditText) dialogView.findViewById(R.id.popEdit2);
                final EditText c = (EditText) dialogView.findViewById(R.id.popEdit3);

                name = a.getText().toString();
                price = Integer.parseInt(b.getText().toString());
                qty = Integer.parseInt(c.getText().toString());
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        builder.show();

    }

 

Thanks a lot to Mr_Koka, it's the second time he helped me today :D 

Computer Case: NZXT S340 || CPU: AMD Ryzen 5 1600 || Cooler: CM Hyper212 Evo || MoBo: MSI B350 Mortar || RAM Vengeance LPX 2x8GB 3200MHz || PSU: Corsair CX600 || SSD: HyperX Fury 120GB & 240GB || HDD: WD Blue 1TB + 1TB 2.5'' backup drive || GPU: Sapphire Nitro+ RX 580 4GB

Laptop 1 HP x360 13-u113nl

Laptop Lenovo z50-75 with AMD FX-7500 || OS: Windows 10 / Ubuntu 17.04

DSLR Nikon D5300 w/ 18-105mm lens

Link to comment
https://linustechtips.com/topic/590806-cant-fix-a-bug-in-java/#findComment-7690167
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×