Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPE Using AlarmManager in v3.1.2 #2669

Closed
ScottPierce opened this issue Oct 5, 2016 · 12 comments
Closed

NPE Using AlarmManager in v3.1.2 #2669

ScottPierce opened this issue Oct 5, 2016 · 12 comments

Comments

@ScottPierce
Copy link

Description

Getting a NPE while using AlarmManager.

AlarmManager alarmManager = (AlarmManager) RuntimeEnvironment.application.getSystemService(Context.ALARM_SERVICE);

// Later on a background thread I call the below statement. The call results in the below error.
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nextAlarmTimestamp, pendingIntent);
java.lang.NullPointerException
    at android.app.AlarmManager.setImpl(AlarmManager.java:464)
    at android.app.AlarmManager.setExactAndAllowWhileIdle(AlarmManager.java:668)
...

Robolectric & Android Version

Roblectric version 3.1.2
Using android sdk 23 with robolectric, but using 24 to compile.

@anitawoodruff
Copy link
Contributor

I get the same NPE when testing code that calls alarmManager.setWindow() - is it simply because these methods aren't implemented in Robolectric's ShadowAlarmManager?

I don't really know how Robolectric works but I note that if I change the code to call alarmManager.set() or setAndAllowWhileIdle() instead (which are implemented) it does not throw the NPE.

@jongerrish
Copy link
Contributor

Yes, it is missing the shadow for this relatively new method that was added
in API 23

You can just add a the implementation similar to all the other set*()
methods:-

https://github.com/robolectric/robolectric/blob/master/robolectric-shadows/shadows-core/src/main/java/org/robolectric/shadows/ShadowAlarmManager.java

Want to send a PR with a test?

On 14 November 2016 at 15:12, anitawoodruff notifications@github.com
wrote:

I get the same NPE when testing code that calls alarmManager.setWindow()

I don't really know how Robolectric works but I note that if I change the
code to call alarmManager.set() or setAndAllowWhileIdle() instead (which
are implemented) it does not throw the NPE.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#2669 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AGBaeK_velJyMVdNAFdEMZwva1o18hX0ks5q-OrfgaJpZM4KOh9z
.

@anitawoodruff
Copy link
Contributor

Sure, I'll give it a go!

@jongerrish
Copy link
Contributor

You can check the project out from github, it builds with Gradle now,
instructions are at the bottom here, let me know if you get stuck...

https://github.com/robolectric/robolectric

On 14 November 2016 at 15:42, anitawoodruff notifications@github.com
wrote:

Sure, I'll give it a go!


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#2669 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AGBaeFwq6D47M-dVgKhKKDM-odH2NaQyks5q-PH9gaJpZM4KOh9z
.

@anitawoodruff
Copy link
Contributor

I've checked it out and installed dependencies but not sure how to run the right tests, without running all of them.. I tried./gradlew test --tests "*ShadowAlarmManagerTest*" but it said 'No tests found'

(the instructions to run Robolectric's tests with maven at http://robolectric.org/contributing/ are out of date, right?)

@jongerrish
Copy link
Contributor

Ya, they're out of date, I've created the following tracker entry to fix
this:- https://www.pivotaltracker.com/story/show/134374649

You should be able to just run ./gradlew test to run all tests, not sure
how to specify test filters, but if you find out let me know :-)

You can also just run a single test from within IntelliJ / Android Studio
directly.

On 15 November 2016 at 01:45, anitawoodruff notifications@github.com
wrote:

I've checked it out and installed dependencies but not sure how to run the
right tests, without running all of them.. I tried./gradlew test --tests
"ShadowAlarmManagerTest" but it said 'No tests found'

(the instructions to run Robolectric's tests with maven at
http://robolectric.org/contributing/ are out of date, right?)


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#2669 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AGBaePvn-uCneZyw7EIV7XCFS9X5DtyOks5q-X9LgaJpZM4KOh9z
.

@anitawoodruff
Copy link
Contributor

anitawoodruff commented Nov 15, 2016

I tried to import the project into Android Studio, but I'm having issues:

  • After import I get the message "Error:The modules 'parent', 'buildSrc' point to same directory in the file system.
    Each module has to have a unique path."
  • All the @RunWith(TestRunners.MultiApiWithDefaults.class) annotations are not found, underlined in red. I think maybe because the org.robolectric.annotations.Config file cannot resolve org.jetbrains.annotations.NotNull - the import and usages are underlined and if I try to run tests it fails with "Error:(5, 33) java: package org.jetbrains.annotations does not exist"

For the record I found out how to run a single test class from command line - you have to specify the submodule too:

./gradlew robolectric:test --tests "*ShadowAlarmManagerTest"

But it would be nice to be able to edit and run the tests using Android Studio if I can get it configured correctly! Any help appreciated.

(Edit: simplified the test command.)

@jongerrish
Copy link
Contributor

Are you importing the gradle project? robolectric/build.gradle - this works
for me

On 15 November 2016 at 14:14, anitawoodruff notifications@github.com
wrote:

I tried to import the project into Android Studio, but I'm having issues:

After import I get the message "Error:The modules 'parent', 'buildSrc'
point to same directory in the file system.
Each module has to have a unique path."

All the @RunWith(TestRunners.MultiApiWithDefaults.class) annotations
are not found, underlined in red. I think maybe because the
org.robolectric.annotations.Config file cannot resolve
org.jetbrains.annotations.NotNull - the import and usages are
underlined and if I try to run tests it fails with "Error:(5, 33) java:
package org.jetbrains.annotations does not exist"

For the record I found out how to run a single test class from command
line - you have to specify the submodule too:

./gradlew -D:robolectric:test.single=ShadowAlarmManagerTest robolectric:test

But it would be very nice to be able to edit the code using Android Studio
if I can get it configured correctly! Any help appreciated.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#2669 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AGBaeK8-ukRx_L0I5QrZLm4gzznqvCEYks5q-i7IgaJpZM4KOh9z
.

@anitawoodruff
Copy link
Contributor

Yup, that's exactly what I'm doing (Android Studio > Import Project > select top level build.gradle ) .

Not the end of the world though since I can run tests from command line just fine with that command. PR created!

@xian xian closed this as completed in b68494e Nov 15, 2016
xian added a commit that referenced this issue Nov 15, 2016
Add unimplemented AlarmManager methods - fixes #2669
@anitawoodruff
Copy link
Contributor

@jongerrish is there any chance of a new release of Robotica happening soon with this fix in? It would be really great to get the Habitica tests passing again! All the recent pull requests have been in the red due to this bug :( Thanks!

@jongerrish
Copy link
Contributor

jongerrish commented Dec 6, 2016 via email

@seadowg
Copy link
Collaborator

seadowg commented Jun 26, 2017

@anitawoodruff @jongerrish I'm having exactly the same issue importing the project into Android Studio (Error:The modules 'parent', 'buildSrc' point to same directory in the file system). Did you ever solve this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants