1. Are you ready for the Galaxy S20? Here is everything we know so far!

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

Discussion in 'Android Development' started by german278, Jun 24, 2019.

  1. german278

    german278 Lurker
    Thread Starter

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


    Code (Text):
    1. at android.app.ActivityThread.main(ActivityThread.java:7073)
    2.         at java.lang.reflect.Method.invoke(Native Method)
    3.         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    4.         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
    5.      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    6.         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 ? :

    Code (Java):
    1.     @Override
    2.     protected void onCreate(Bundle savedInstanceState) {
    3.         super.onCreate(savedInstanceState);
    4.         setContentView(R.layout.activity_main);
    5.  
    6.        Button button = findViewById(R.id.button1);
    7.        button.setOnClickListener(new View.OnClickListener() {
    8.             @Override
    9.             public void onClick(View v) {
    10.  
    11.                 openanmeldung();
    12.             }
    13.         });
    14.  
    15.  
    16.  
    17.  
    18.  
    19.  
    20.  
    21.         BottomNavigationView navigation = findViewById(R.id.bottomnav);
    22.         navigation.setOnNavigationItemSelectedListener(this);
    23.  
    24.  
    25.         getSupportFragmentManager().beginTransaction().replace(fragment_container, new Home_Screen()).commit();
    26.  
    27.  
    28.     }
    29.  
    30.     private void openanmeldung(){
    31.         Intent intent = new Intent(MainActivity.this,AnmeldungButton.class);
    32.         startActivity(intent);
    33.     }

    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 (Text):
    1.  <android.support.v7.widget.CardView
    2.  
    3.             android:layout_width="match_parent"
    4.             android:layout_height="173dp"
    5.             app:cardCornerRadius="8dp"
    6.             android:layout_marginTop="80dp"
    7.             android:layout_marginLeft="30dp"
    8.             android:layout_marginRight="30dp"
    9.             >
    10.  
    11.         <Button
    12.                 android:text="Mehr infos"
    13.                 android:layout_width="120dp"
    14.                 android:layout_height="35dp"
    15.                 android:id="@+id/button1"
    16.                 android:layout_weight="1"
    17.                 android:layout_marginTop="130dp"
    18.                 android:layout_marginRight="170dp"
    19.  
    20.                 />
    21.  
    22.  
    23.         <LinearLayout android:layout_height="match_parent"
    24.                       android:layout_width="match_parent"
    25.                       android:background="@color/maincolor"
    26.  
    27.                 >
    28.  
    29.         </LinearLayout>
    30.  
    31.         <TextView android:layout_height="wrap_content"
    32.                   android:layout_width="wrap_content"
    33.                   android:textColor="@color/white"
    34.                   android:text="HPI - 29.06.19"
    35.                   android:textSize="24dp"
    36.                 />
    37.  
    38.         <TextView android:layout_height="wrap_content"
    39.                   android:layout_width="wrap_content"
    40.                   android:textColor="@color/white"
    41.                   android:text="Sitzung: H-E.51"
    42.                   android:textSize="16dp"
    43.                   android:layout_marginTop="40dp"
    44.  
    45.                 />
    46.         <TextView android:layout_height="wrap_content"
    47.                   android:layout_width="wrap_content"
    48.                   android:textColor="@color/white"
    49.                   android:text="Juni22, 2019"
    50.                   android:textSize="16dp"
    51.                   android:layout_marginTop="40dp"
    52.                   android:layout_marginRight="20dp"
    53.                   android:layout_marginLeft="270dp"
    54.  
    55.  
    56.  
    57.  
    58.  
    59.  
    60.                 />
    61.             </android.support.v7.widget.CardView>

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


    Code (Text):
    1. package com.example.myapplication2;
    2.  
    3.  
    4. import android.os.Bundle;
    5. import android.support.annotation.NonNull;
    6. import android.support.annotation.Nullable;
    7. import android.support.v4.app.Fragment;
    8. import android.view.LayoutInflater;
    9. import android.view.View;
    10. import android.view.ViewGroup;
    11.  
    12.  
    13. public class AnmeldungButton extends Fragment {
    14.  
    15.     @Override
    16.     public void onCreate(@Nullable Bundle savedInstanceState) {
    17.         super.onCreate(savedInstanceState);
    18.     }
    19.  
    20.     @Nullable
    21.     @Override
    22.     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    23.         return inflater.inflate(R.layout.moreinffos, container, false);
    24.     }
    25. }
    Thank you for everyone who help me ....
     

    Attached Files:



  2. Deleted User

    Deleted User Guest

    Your problem is happening because this statement returns null

    Code (Text):
    1.  
    2. Button button = findViewById(R.id.button1);
    3.  
    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 (Text):
    1.  
    2. setContentView(R.layout.activity_main);
    3.  
    Is your button1 definition contained in the activity_main layout XML file?
     
    german278 likes this.
  3. german278

    german278 Lurker
    Thread Starter

    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 (Text):
    1.  private void openanmeldung(){
    2.         Intent intent = new Intent(MainActivity.this,AnmeldungButton.class);
    3.         startActivity(intent);
    4.     }
    Isn`t this true ?
     
  4. Deleted User

    Deleted User Guest

    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.
     
  5. german278

    german278 Lurker
    Thread Starter

    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 ...
     
Loading...
Similar Threads - Caused java lang
  1. LubaArbel
    Replies:
    0
    Views:
    934
  2. kayakkielbasa
    Replies:
    3
    Views:
    439
  3. Brizky
    Replies:
    0
    Views:
    385
  4. Android News
    Replies:
    0
    Views:
    139
  5. james_uk
    Replies:
    6
    Views:
    368
  6. joshhumble
    Replies:
    69
    Views:
    1,965
  7. ShamusVW
    Replies:
    0
    Views:
    676
  8. ElWadster
    Replies:
    7
    Views:
    645
  9. willsp28
    Replies:
    9
    Views:
    614
  10. Sara222
    Replies:
    7
    Views:
    774

Share This Page

Loading...