Nullpointerexception on Button.Findviewbyid()

NullPointerException on Button.findViewById()

You are invoking the findViewById() method in the wrong way. It should be invoked on the Activity itself, not on the Button reference. Replace

b1=(Button) b1.findViewById(R.id.button);

with

b1=(Button) findViewById(R.id.button);

The b1 reference is null at that point, and is the reason for the NullPointerException.

FindViewByID - Null Pointer Exception! [ANDROID STUDIO]

So far I understand the button with id calc_kreis doesn't exist on the "current" view and that's why findViewById returns null.

Method findViewById will only return widgets contained by the view you set on setContentView. If the button is placed on other view, your code will never work.

Android findViewById null pointer

You're calling findViewById() on the view that was clicked. Your button does not have the edittext as its child. You should query the activity view hierarchy instead: replace

view.findViewById(R.id.editText)

with

findViewById(R.id.editText)

Android NullPointerException when findViewById

This is because findViewById() searches in the activity_main layout, while the button is located in the fragment's layout fragment_main.

Move that piece of code in the onCreateView() method of the fragment:

//...

View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button buttonClick = (Button)rootView.findViewById(R.id.button);
buttonClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onButtonClick((Button) view);
}
});

Notice that now you access it through rootView view:

Button buttonClick = (Button)rootView.findViewById(R.id.button);

otherwise you would get again NullPointerException.

findViewById throws null exception when it is used on Alert Dialog widgets

Replace this:

tv7 = findViewById(R.id.tv7)
bt4 = findViewById(R.id.bt4)

With this:

tv7 = messageBoxView.findViewById(R.id.tv7)
bt4 = messageBoxView.findViewById(R.id.bt4)

You are inflating a view (the dialog) into your activity with:

val messageBoxView = LayoutInflater.from(this).inflate(R.layout.layout_dialog, null)

You then need to define tv7 and bt4 views, but they are in the view that you inflated previously. So you need to define them based on the inflated view.

Null pointer Exception - findViewById()

findViewById() returns a View if it exists in the layout you provided in setContentView(), otherwise it returns null and that's what happening to you. Note that if you don't setContentView(), and don't have a valid view to findViewById() on, findViewById() will always return null until you call setContentView().

This also means variables in the top-level trigger an NPE, because they're called before onCreate(), and by extension, before setContentView(). See also the activity lifecycle

Example if you setContentView(R.layout.activity_first); and then call findViewById(R.id.first_View); it will return a View which is your layout.

But if you call findViewById(R.id.second_View); before setContentView(), it will return null since there is not a view in your activity_first.xml layout called @+id/second_View.

How to solve NullPointerException in the following code?

at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:214)
at com.example.shrine.MainActivity.<init>(MainActivity.java:18)

This tells you're calling findViewById() too early in MainActivity's init phase e.g. when initialising its fields. Move the findViewById() calls to onCreate() after setContentView().



Related Topics



Leave a reply



Submit