Home > OS >  java.lang.NullPointerException:Attempt to invoke virtual method 'void androidx.ap
java.lang.NullPointerException:Attempt to invoke virtual method 'void androidx.ap

Time:11-27

I am building an application with contains navigation drawer so while using action bar i am getting this error tried multiple ways but it is not getting resolved. theme used is

This is the runtime error E/AndroidRuntime: FATAL EXCEPTION: main Process: com.vrushali.hospital, PID: 32621 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vrushali.hospital/com.vrushali.hospital.MainActivity3}: java.lang.NullPointerException: Attempt to invoke virtual method 'voidandroidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference .....

 **My code of .java file**
            public class MainActivity3 extends AppCompatActivity {
                private DrawerLayout drawerLayout;
                private Toolbar toolbar;
                private NavigationView navigationView;
            private TextView nav_fullname,nav_email,nav_contact,nav_type;
            private DatabaseReference userRef;
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    drawerLayout = findViewById(R.id.drawer_layout);
                    toolbar = findViewById(R.id.toolbar);
                    setSupportActionBar(toolbar);
                    getSupportActionBar().setTitle("Patient Portal");
                    navigationView = findViewById(R.id.navigation_view);
                    ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(MainActivity3.this,drawerLayout,
                            toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_closed);
                    drawerLayout.addDrawerListener(toggle);
                    toggle.syncState();
                    nav_fullname =navigationView.getHeaderView(0).findViewById(R.id.nav_fullname);
                    nav_email =navigationView.getHeaderView(0).findViewById(R.id.nav_email);
                    nav_contact =navigationView.getHeaderView(0).findViewById(R.id.nav_contact);
                    nav_type =navigationView.getHeaderView(0).findViewById(R.id.nav_type);
                    userRef= FirebaseDatabase.getInstance().getReference().child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
                    userRef.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot snapshot) {
                           if (snapshot.exists()) {
                               String name= snapshot.child("name").getValue().toString();
                               nav_fullname.setText(name);
                               String email= snapshot.child("email").getValue().toString();
                               nav_fullname.setText(email);
                               String phonenumber= snapshot.child("phonenumber").getValue().toString();
                               nav_fullname.setText(phonenumber);
                               String type= snapshot.child("type").getValue().toString();
                               nav_fullname.setText(type);
            
                           }
                        }
            
                        @Override
                        public void onCancelled(@NonNull DatabaseError error) {
            
                        }
                    });
            
                }
            }

Please help resolve this error the error is related to themes but i am not getting it

CodePudding user response:

Your action bar is null and so you are trying to setTitle() on a null object.

  1. What is in the theme are you using ? "<style name="AppTheme.NoActionBar">" ?

  2. setSupportActionBar(toolbar); put a break point here, is toolbar null ?

CodePudding user response:

this is the theme used along with false true
  • Related