0

I add notifications property my android app and update in google play. My phone(Nexus 4) is working properly. But Some phones give this error

java.lang.NullPointerException
at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
at org.json.JSONTokener.nextValue(JSONTokener.java:94)
at org.json.JSONObject.<init>(JSONObject.java:154)
at org.json.JSONObject.<init>(JSONObject.java:171)
at com.medyasef.dernek.tjod.GetJson.json_to_last_id(GetJson.java:66)
at com.medyasef.dernek.tjod.PostService.onHandleIntent(PostService.java:42)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:213)
at android.os.HandlerThread.run(HandlerThread.java:60)

I dont understand why ?

PostService.java

public class PostService extends IntentService {

    private List<Categoryicerikler> get_last_id;
    private final String SON_ID_NUMARASI    = "son_id_numarasi";
    private final String UYARI              = "preferences_sonucu";
    private Context mContext                = this;

    public PostService() {
        super("PostService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.i(UYARI, "Service Başladı");

        if(checkInternetConnection()){
            InternetConnection internetcon  = new InternetConnection(34);
            String json_result              = internetcon.get_json_data();
            try {
                get_last_id = GetJson.json_to_last_id(json_result);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            /*
            Telefona kaydedilen son id numarasını kontrol ediyoruz.
            Eğer daha önce internete bir veri kaydedilmediyse boş veri döner.
             */
            SharedPreferences preferences   = getSharedPreferences("last_id_int", MODE_PRIVATE);
            int last_shared_id              = preferences.getInt(SON_ID_NUMARASI, 0);

            if(last_shared_id == 0) {
                SharedPreferences.Editor editor = preferences.edit();
                editor.putInt(SON_ID_NUMARASI,get_last_id.get(0).getCategory_post_ID());
                editor.apply();
                Log.i(UYARI,"Sonuc Bos Dondu Preferences kaydetti");
            }
            else if(last_shared_id != get_last_id.get(0).getCategory_post_ID()){
                Log.i(UYARI,"Sonuçlar Eşit Değil Notification Gosterilecek");

                notification_show(get_last_id.get(0).getCategory_posttitle(), get_last_id.get(0).getCategory_post_content());

                SharedPreferences.Editor editor = preferences.edit();
                editor.putInt(SON_ID_NUMARASI, get_last_id.get(0).getCategory_post_ID());
                editor.apply();
            }
            else {
                Log.i(UYARI,"Sonuclar Eşit Hiç Birşey Yapılmadı.");
            }

        }
    }

    private boolean checkInternetConnection() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm.getActiveNetworkInfo() != null
                && cm.getActiveNetworkInfo().isAvailable()
                && cm.getActiveNetworkInfo().isConnected()) {
            return true;
        }
        else {
            Log.v("Internet", "Internet Connection Not Present");
            return false;
        } }

    private void notification_show(String post_title,String post_content){

        int requestID = (int) System.currentTimeMillis();
        Intent reminder_intent = new Intent(mContext, SinglePage.class);
        reminder_intent.putExtra(Categories.EXTRA_CODE,post_content);
        PendingIntent intent_single = PendingIntent.getActivity(this,requestID,reminder_intent,0);

        /*
        Setticker bildirim ilk geldiğinde üstte gözükecek yazıdır.
        SetContentTitle Bildirim penceresi aşağı kaydırıldığında gösterilecek başlıktır.
        setContentText Bildirim penceresi aşağı kaydırıldığında gösterilecek başlığın altında ki içeriktir.
        setSmallIcon Gösterilecek resimdir.
         */
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification noti = new Notification.Builder(this)
                .setTicker("TJOD Yeni Duyuru")
                .setContentTitle(post_title)
                .setContentText("Duyuruyu Görmek İçin Tıklayınız.")
                .setSmallIcon(R.drawable.ic_launcher)
                .setShowWhen(false)
                .setContentIntent(intent_single)
                .build();
        noti.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, noti);
    }
}

json_to_last_id function (in GetJson.java)

public static List<Categoryicerikler> json_to_last_id(String data) throws JSONException {

        content_list = new ArrayList<Categoryicerikler>();

        /*
        Gelen String veriyi çekiyoruz ve dizilere girmeye başlıyoruz.
         */
        JSONObject jObj     = new JSONObject(data);
        JSONArray posts     = jObj.getJSONArray("posts");

        JSONObject post_id  = posts.getJSONObject(0);
        JSONObject title    = posts.getJSONObject(0);
        JSONObject content  = posts.getJSONObject(0);

        content_list.add(new Categoryicerikler(
                title.getString("title"),
                content.getString("content"),
                post_id.getInt("ID")
        ));
        return content_list;
    }
6
  • 1
    Can you indicate which lines the NPE are on please? Commented Nov 5, 2013 at 15:23
  • that seems to be this one : JSONObject jObj = new JSONObject(data); Commented Nov 5, 2013 at 15:30
  • can you post the json that causes the error ? Commented Nov 5, 2013 at 15:30
  • 1
    @ScottHelme 42. Line get_last_id = GetJson.json_to_last_id(json_result); Commented Nov 5, 2013 at 15:40
  • @njzk2 How to fixed this line ? this is json service link = tjod.org/androidWebService/?id=34 Commented Nov 5, 2013 at 15:41

1 Answer 1

1

JSONTokener.nextCleanInternal will throw when the json input string is null. Check for a null data argument to json_to_last_id.

Sign up to request clarification or add additional context in comments.

1 Comment

I think its true now ı trying again and again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.