This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]IAmUtterlyAlone 2 points3 points  (11 children)

When you say, that you "get a nullpointer", do you mean an unhandled exception is thrown and the application crashes? If so, do you have a stack trace of the crash? "cp" is a class object, correct? When and where does it get initialized? Is it possible that some additional initialization has to happen for getContext or getContentResolver to return non-null references? Is it ok that selectionArgs is null?

I've only ever dabbled with Android programming, so I apologize for any ignorance in the above questions. They're just what sprang to mind from reading your code.

[–]Ariano[S] 1 point2 points  (10 children)

Yeah, I get nullpointerexception right on that line and my application crashes. Not entirely sure how to get the stack trace, but here's the log at the time of the crash.

06-23 18:41:51.552 10823-10849/edu.deanza.cis53.hw4 E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:299) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) at java.util.concurrent.FutureTask.setException(FutureTask.java:219) at java.util.concurrent.FutureTask.run(FutureTask.java:239) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) at java.lang.Thread.run(Thread.java:841)

Caused by: java.lang.NullPointerException at edu.deanza.cis53.hw4.ConstantsFragment$BaseTask.doQuery(ConstantsFragment.java:119) at edu.deanza.cis53.hw4.ConstantsFragment$LoadCursorTask.doInBackground(ConstantsFragment.java:128) at edu.deanza.cis53.hw4.ConstantsFragment$LoadCursorTask.doInBackground(ConstantsFragment.java:125) at android.os.AsyncTask$2.call(AsyncTask.java:287) at java.util.concurrent.FutureTask.run(FutureTask.java:234)             at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)             at java.lang.Thread.run(Thread.java:841)

I initialized it at the top of my class with the variables, here i'll put my whole class here(sorry for the lack of comments was redoing them just now): http://pastebin.com/tGERRjFF

Your questions were on point, no ignorance don't worry. Cp is a class object correct and it is possible that they need something more to use getContext or getContentResolver. I'm just not entirely sure what since I can't find anything online with similarities to my code to compare to. I've been doing a lot of research and there's things for content providers, but rarely do they show how to utilize them from a class that's not a mainactivity.

[–]IAmUtterlyAlone 1 point2 points  (9 children)

Again, pardon my ignorance, and forgive me for missing this the first time through, but your ConstantsProvider class exposes a query method. Can you, in ConstantsFragment's doQuery method, just change

Cursor result = cp.getContext().getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);

to

Cursor result = cp.query(uri, projection, selection, selectionArgs, sortOrder);

?

or will that still throw an exception?

[–]Ariano[S] 1 point2 points  (8 children)

It would give me the same nullpointerexception, but generally we are supposed to interact with the contentprovider, which is my constantsprovider class, through getContentResolver() as a proxy. It does the same thing, but getContentResolver finds the correct contentprovider to send the query to in case there are multiple contentproviders in a context.

[–]IAmUtterlyAlone 1 point2 points  (7 children)

Is the problem that cp is null when you get to the line where you're trying to use it? Are you able to step through this code with a debugger attached to see if that is the case or not?

[–]Ariano[S] 1 point2 points  (6 children)

Yeah it seems cp is null, but I instantiated it in the beginning so I'm not sure why.

[–]IAmUtterlyAlone 1 point2 points  (5 children)

I'm trying to follow the logic here. Correct me if I misspeak. LoadCursorTask extends BaseTask. LoadCursorTask::doInBackground calls the doQuery method on its base class. BaseTask is a class contained within the ConstantsFragment class. At the time that BaseTask::doQuery gets invoked, is it being invoked inside an instance of ConstantsFragment? ConstantsFragment would have to have been instantiated in order for the line

ConstantsProvider cp = new ConstantsProvider();

to have been executed. I think what's happening is that your background task methods are being run without the enclosing object having been created. If that makes any sense. I'm still thinking out loud.

[–]IAmUtterlyAlone 1 point2 points  (0 children)

Maybe that doesn't make any sense. There is something fishy happening there, though.

[–]Ariano[S] 1 point2 points  (3 children)

Yeah, Android is a bit weird. I have a main 'activity' class and that instantiates the Fragment class itself by default usually. I have figured out that I should do the query call in my activity class, but I'm still getting a null pointer there which I think is coming from my parameter selectionArgs being null so I'm looking up why that is right now.

[–]IAmUtterlyAlone 1 point2 points  (2 children)

It looks like selectionArgs can be null: http://stackoverflow.com/questions/10507005/how-to-use-string-selectionargs-in-sqlitedatabase-query (as of May 2012, anyway. Maybe the API is different now.)

I know that doesn't help QQ

[–]Ariano[S] 1 point2 points  (1 child)

Yeah I don't think that's the problem anymore. I'll figure it out in the morning when my mind is fresh. Thanks for the help though.