2

I'm new with android development. I'm currently developing an Android Application on Face Recognition. I'm trying to run an existing source code that I obtain from github. The codes seems does not have any error(red line beneath the code). When I tried to run, the application has stopped working and gave me a java.lang.NullPointerException error.

Caused by: java.lang.NullPointerException
           at java.util.Objects.requireNonNull(Objects.java:98)
           at com.example.syafiq.testing.FdActivity.onCreate(FdActivity.java:179)

on the highlighted error (FdActivity.java:179)

(line 178) ActionBar actionBar = getActionBar();
(line 179) actionBar.setDisplayHomeAsUpEnabled(true);

I've tried solution that I've found on the Internet

First try:

ActionBar actionBar = getActionBar();
assert actionBar != null;
actionBar.setDisplayHomeAsUpEnabled(true);

And yet still the same error, so i tried another one that I found on the Internet.

Second try:

ActionBar actionBar = getActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

The result still the same
I've tried the third one

Third try:

ActionBar actionBar = getActionBar();
Objects.requireNonNull(actionBar).setDisplayHomeAsUpEnabled(true);

All the three solution that I've tried still giving the same error, point to line 179

For android manifest.xml(obtain from source code on github):

android:theme="@android:style/Theme.Holo"

But the source code does not have style.xml file under the values folder. But my android studio include with style.xml file and the code is:

<style name="Theme.Holo" parent="Theme.AppCompat.Light.DarkActionBar">

I did not use the import tools instead I've created manually (.java, .xml and other libraries that included)

bo2
  • 653
  • 1
  • 7
  • 12

4 Answers4

2

If your project build target is equal or below Android 4.4 problem may be of your Activity theme please check in manifest if u r using custom theme try using android:theme="@android:style/Theme.Holo.Light" as parent

and if project build target is equal or above Android 5.0 use :

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

instead of

actionBar.setDisplayHomeAsUpEnabled(true);

and also check Theme issue as above

Kapil Rajput
  • 11,429
  • 9
  • 50
  • 65
  • I tried and receive another error, Three classes contain, FdActivity, PersonRecognizer and AsyncServiceHelper :/ – bo2 Dec 23 '15 at 07:59
  • A code block alone does not provide a good answer. Please add explanations. – Louis Barranqueiro Dec 23 '15 at 09:16
  • Hi kapil, thank you for your new edit and help! I've tried using the solution that you gave me, it seems it gave me different error that consist an xml file Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity my project is Android 4.4.2 – bo2 Dec 23 '15 at 16:16
  • edited, I've included my manifest there :D – bo2 Dec 24 '15 at 07:03
2

Your logcat throws

Caused by: java.lang.NullPointerException at java.util.Objects.requireNonNull(Objects.java:98)

  1. Try with getSupportActionBar()

  2. Use ToolBar instead of ActionBar .

Since version 22.1.0, the class ActionBarActivity is deprecated. You should use AppCompatActivity.

https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
1

It's hard to guess without you posting your xml file and manifest. Please post them and we can help you. Also in your manifest make sure you have a theme as such

android:theme="@style/Theme.ActionBar"

You can always run into npe for so many reasons.

The_Martian
  • 3,684
  • 5
  • 33
  • 61
0

check this activity theme in AndroidManifest.xml or your code whether it is something like noactionbar,if yes,so getActionBar() in your activity will be null;

bozzmob
  • 12,364
  • 16
  • 50
  • 73
Scofield
  • 1
  • 1
  • Hi bozzmob, I've checked my AndroidManifest.xml and theres no "noactionbar" instead it use android:theme="@android:style/Theme.Holo" thanks for answering :) – bo2 Dec 23 '15 at 16:13
  • edited, I've included my manifest there :D – bo2 Dec 24 '15 at 07:04