• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Caused by: java.lang.NullPointerException . Can someone help

german278

Lurker
Hey guys whatsup . I´m new here and i want to ask if you could help me with this error message ...


Code:
at android.app.ActivityThread.main(ActivityThread.java:7073)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.myapplication2.MainActivity.onCreate(MainActivity.java:47)

Yeah you know i´m already sit over 2 days on this error message cause i can`t find any problem in my Code you know ...
Here are my Files , what I´m doing wrong ? :

Java:
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       Button button = findViewById(R.id.button1);
       button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                openanmeldung();
            }
        });







        BottomNavigationView navigation = findViewById(R.id.bottomnav);
        navigation.setOnNavigationItemSelectedListener(this);


        getSupportFragmentManager().beginTransaction().replace(fragment_container, new Home_Screen()).commit();


    }

    private void openanmeldung(){
        Intent intent = new Intent(MainActivity.this,AnmeldungButton.class);
        startActivity(intent);
    }


Thats not my whole code but here is a picture too from the debug tool :
I uploaded it .

and here is the file where I have my Button ... :

Code:
 <android.support.v7.widget.CardView

            android:layout_width="match_parent"
            android:layout_height="173dp"
            app:cardCornerRadius="8dp"
            android:layout_marginTop="80dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            >

        <Button
                android:text="Mehr infos"
                android:layout_width="120dp"
                android:layout_height="35dp"
                android:id="@+id/button1"
                android:layout_weight="1"
                android:layout_marginTop="130dp"
                android:layout_marginRight="170dp"

                />


        <LinearLayout android:layout_height="match_parent"
                      android:layout_width="match_parent"
                      android:background="@color/maincolor"

                >

        </LinearLayout>

        <TextView android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:textColor="@color/white"
                  android:text="HPI - 29.06.19"
                  android:textSize="24dp"
                />

        <TextView android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:textColor="@color/white"
                  android:text="Sitzung: H-E.51"
                  android:textSize="16dp"
                  android:layout_marginTop="40dp"

                />
        <TextView android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:textColor="@color/white"
                  android:text="Juni22, 2019"
                  android:textSize="16dp"
                  android:layout_marginTop="40dp"
                  android:layout_marginRight="20dp"
                  android:layout_marginLeft="270dp"






                />
            </android.support.v7.widget.CardView>


Here is the file that should open when I click the Button :


Code:
package com.example.myapplication2;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class AnmeldungButton extends Fragment {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.moreinffos, container, false);
    }
}

Thank you for everyone who help me ....
 

Attachments

  • Button = null ;.JPG
    Button = null ;.JPG
    103.3 KB · Views: 280
Your problem is happening because this statement returns null

Code:
Button button = findViewById(R.id.button1);

therefore attempting to call setOnClickListener on a null object causes a NPE.

From the code you've shown, I'm guessing that you're inflating the wrong layout for your code. So findViewById fails to locate button1.
As you've omitted file names, it's hard to know where these code fragments live, but the crucial bit is this

Code:
setContentView(R.layout.activity_main);

Is your button1 definition contained in the activity_main layout XML file?
 
Yo first , thank you for your answer man . And yes in the openanmeldung() statement i define that the button is in the moreinfos layout ... is this incorect ?
But you`re right , my button1 is not in the activity_main xml but in the moreinfos.xml and I defined this so I don`t know whats the problem ... Here is the Code the openanmeldung() statement

Code:
 private void openanmeldung(){
        Intent intent = new Intent(MainActivity.this,AnmeldungButton.class);
        startActivity(intent);
    }

Isn`t this true ?
 
But you`re right , my button1 is not in the activity_main xml but in the moreinfos.xml and I defined this so I don`t know whats the problem

That is your problem. You can't use a Button which is defined in another layout. You need to rethink how your layouts are structured.
 
Yeah you are right but if I make my layout to activity_3.xml then my BottomNav bar wouldn`t work cause I define "my whole layout " in this Content view you know ?
So what I can do that these things are working together ... maybe I need write this Code somewhere else ... Pls help ...
 
Back
Top Bottom