-1

I am trying to build a feature within a ROM, the build completes without any isseu and the feature is working, however when i want to open it's settings it force closes.

I have made a logcat:

E/AndroidRuntime( 3440): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 3440):    at com.android.settings.mahdi.aokp.ProgressBar.onCreate(ProgressBar.java:86)

And this is the corresponding lines in ProgressBar.java:

    mprogressbar_speed = (SeekBarPreference) findPreference(PROGRESSBAR_SPEED);
    mprogressbar_speed.setValue(Settings.System.getInt(getContentResolver(), <---- this is line 86
                                Settings.System.PROGRESSBAR_SPEED, 4));
    mprogressbar_speed.setOnPreferenceChangeListener(this);

I dont know how to resolve this isseu and i can use some help...

Thanks in advance...

OwnDroid
  • 11
  • 4
  • As excepted mprogressbar_speed may be null. It's better to debug and check that object. – Janny Sep 18 '14 at 15:17
  • Debug `mprogressbar_speed = (SeekBarPreference) findPreference(PROGRESSBAR_SPEED);` and check if its null. Did you read [this question](http://stackoverflow.com/q/218384/3735079) before asking yours? – Narmer Sep 18 '14 at 15:19
  • what do you mean? i am trying to learn here about this, so please help me and be more spsecific – OwnDroid Sep 18 '14 at 15:20
  • You're trying to learn so please make some effort in that direction. I doubt you could read the question I linked you in a minute. – Narmer Sep 18 '14 at 15:22
  • 1
    @Narmer, how can i learn if i dont know i which direction of the code i have to look? not being rude but just want some help – OwnDroid Sep 18 '14 at 15:26
  • I, @Jani and @Orin already told you the direction you have to look: `mprogressbar_speed` gives you a [`NullPointerException`](http://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html) meaning that the program can't find the progress bar. Read the linked question to understand what it means and how to fix it. We didn't write your code, you're not going to receive an answer like _write 'abc' instead of 'bca'_. This is the best way for you to learn. – Narmer Sep 18 '14 at 15:30
  • okay just another question, how do i debug? – OwnDroid Sep 18 '14 at 15:35
  • Depends on your IDE: [Debug with Android Studio](https://developer.android.com/sdk/installing/studio-debug.html), [Debug with Eclipse](http://www.vogella.com/tutorials/EclipseDebugging/article.html). In this way you can monitor the values of all of your variables at runtime and check for null pointers. – Narmer Sep 18 '14 at 15:38

1 Answers1

0

java.lang.NullPointerException means that you are trying to call a function on a null value. With the line in question, it is very likely that mprogressbar_speed is null.

Check that (SeekBarPreference) findPreference(PROGRESSBAR_SPEED); returns an actual value. If it is returning null, then you are setting your mprogressbar_speed to null, and then on the next line, trying to call .setValue(...) on it, which would cause the NullPointerException

Niro
  • 776
  • 8
  • 15
  • okay, i am a beginner at this, so please can you help me with this a little more? – OwnDroid Sep 18 '14 at 15:23
  • Have you added your preferences from resources, prior to calling `findPreference()`? Like this: http://stackoverflow.com/questions/14339550/preferencefragment-findpreference-always-returns-null – Niro Sep 18 '14 at 15:27
  • i thaught i did, but this isnt my code, this is cherry-picked, but i am just interested on how to fix this, and need someone that can tell me what to do and explains why, thats all – OwnDroid Sep 18 '14 at 15:32
  • The most likely answer is that this line: `mprogressbar_speed = (SeekBarPreference) findPreference(PROGRESSBAR_SPEED);` is setting `mprogressbar_speed` to `null`. So when you try to call `mprogressbar_speed.setValue(...)` on the next line, you are getting the `NullPointerException` because in Java, if a variable (mprogressbar_speed) is `null`, you cannot call any functions on it. – Niro Sep 18 '14 at 15:40
  • So if i do this: mprogressbar_speed = (SeekBarPreference) findPreference(PROGRESSBAR_SPEED); mprogressbar_speed.setValue(Settings.System.PROGRESSBAR_SPEED, 4); mprogressbar_speed.setOnPreferenceChangeListener(this); it should not be a NPE? – OwnDroid Sep 18 '14 at 16:00
  • If `(SeekBarPreference) findPreference(PROGRESSBAR_SPEED);` fails to return a value (because it can't find a preference named PROGRESSBAR_SPEED or because the preferences haven't been loaded yet or for any reason), then `mprogressbar_speed` will be set to `null`. `null` just means that that variable doesn't have any value. The program doesn't know what it is. So when you try to do any functions on it (like `mprogressbar_speed.setValue(...)` or `mprogressbar_speed.setOnPreferenceChangeListener(this)`, you will get a `NullPointerException`, meaning that `mprogressbar_speed` doesnt have a value – Niro Sep 18 '14 at 16:10