-2

I want to use jsonObject in my class. So I create a private jsonObject. I created it. But with getting NullPointer. Why is this happening?

My code
http://pastebin.com/NKm372LV

My Error Exeption
http://pastebin.com/cscAE1Q8

Caused by: java.lang.NullPointerException
        at com.EraNewGames.Elbase.Schedule.onCreate(Schedule.java:32)
Pupkovisk
  • 1
  • 2
  • include the code on SO, not just in a link. – luk2302 Apr 11 '15 at 14:57
  • Put your code and error in your question. Otherwise people with same problem will not be able to find answers which may appear in your post, which defeats purpose of Stack Overflow. If your code is too long create and post [SSCCE](http://sscce.org/) – Pshemo Apr 11 '15 at 14:57
  • what is the statement on Schedule.java:32? obviously there is something null value referrence – Amit K. Saha Apr 11 '15 at 17:05

1 Answers1

0

Because you are creating JSONObject in another thread - after you are trying to use it. You should read more about work with threads. You can put Log.e("asdadsadasda", jsonObject.toString()); after you were set jsonObject value in your other thread.

Ircover
  • 2,406
  • 2
  • 22
  • 42
  • but there is a new thread in the current class. And he sees JsonObject. This is Thread thread=new Thread(new Runnable() { @Override public void run() { try { jsonObject=new JSONObject(str); – Pupkovisk Apr 11 '15 at 15:01
  • Your `onCreate` code works parallel with another thread, so `jsonObject.toString()` calls earlier then `jsonObject=new JSONObject(str)`. – Ircover Apr 11 '15 at 15:03
  • @Incover, not. JsonCreate(); Log.e("asdadsadasda", jsonObject.toString()); JsonCreate(); This is Method – Pupkovisk Apr 11 '15 at 15:10
  • @Invocer. And if I create a onCreate jsonObject=new JSONObject();. And JsonCreate(){ jsonObject.opt(str); } So go? – Pupkovisk Apr 11 '15 at 15:15
  • `NullPointerException` will disappear, but it will just show `{}` in your log. So, your app would not crash. – Ircover Apr 11 '15 at 15:23
  • You just don't understand how separated threads works. Learn more about that, for example [here](http://stackoverflow.com/questions/3391272/how-to-use-separate-thread-to-perform-http-requests). – Ircover Apr 11 '15 at 16:01