Java.lang.nullpointerexception: попытка вызвать виртуальный метод 'int java.lang.string.длина()'
Привет android энтузиаст
Я пытаюсь создать приложение для входа/регистрации, но приведенный ниже код создает проблему, которая является асинхронной задачей,
всякий раз, когда я тестирую его на своем устройстве, нажимая кнопку register, он всегда падает и показывает следующую ошибку в logcat
Чтение logcat ошибки
780 7570-7676/com.myfonetv.a3wc.myfonetv D/libc-netbsd: getaddrinfo: staging.streamhash.info нет результата от прокси-сервера
08-17 16:20:54.784 7570-7676/com.myfonetv.a3wc.myfonetv I/System.out: [CDS][DNS]не удается разрешить хост "staging.streamhash.info": нет адреса, связанного с именем хоста
08-17 16:20:54.784 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: java.net-да.UnknownHostException: не удается разрешить хост "staging.streamhash.info": нет адреса, связанного с именем хоста
08-17 16:20:54.787 7570-7729/com.myfonetv.a3wc.myfonetv D/Surface: Surface::setBuffersDimensions(this=0x7f8060c600,w=480,h=800)
08-17 16:20:54.793 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at java.net-да.InetAddress.lookupHostByName(InetAddress.java:479)
08-17 16:20:54.793 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at java.net-да.InetAddress.getAllByNameImpl(InetAddress.java:259)
08-17 16:20:54.793 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at java.net-да.InetAddress.getAllByName(InetAddress.java:221)
08-17 16:20:54.793 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at org.apache.http.impl.conn.DefaultClientConnectionOperator.содержащие вредоносные элементы(DefaultClientConnectionOperator.Ява:147)
08-17 16:20:54.793 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
08-17 16:20:54.793 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
08-17 16:20:54.793 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
08-17 16:20:54.793 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:602)
08-17 16:20:54.793 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:522)
08-17 16:20:54.793 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:500)
08-17 16:20:54.793 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at com.myfonetv.a3wc.myfonetv.activities.Register$6.doInBackground(Register.java:555)
08-17 16:20:54.793 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at com.myfonetv.a3wc.myfonetv.activities.Register$6.doInBackground(Register.java:516)
08-17 16:20:54.793 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:292)
08-17 16:20:54.793 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-17 16:20:54.793 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-17 16:20:54.793 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-17 16:20:54.793 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-17 16:20:54.793 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at java.lang.Thread.run(Thread.java:818)
08-17 16:20:54.793 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: вызвано: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (нет адреса, связанного с именем хоста)
08-17 16:20:54.794 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at libcore.io.Posix.android_getaddrinfo(родной метод)
08-17 16:20:54.794 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
08-17 16:20:54.794 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at java.net-да.InetAddress.lookupHostByName(InetAddress.java:445)
08-17 16:20:54.794 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: ... 17 подробнее
08-17 16:20:54.801 7570-7729/com.myfonetv.a3wc.myfonetv D/Surface: Surface::setBuffersDimensions(this=0x7f8060c600,w=480,h=800)
08-17 16:20:54.804 7570-7570/com.myfonetv.a3wc.myfonetv D/AndroidRuntime: выключение виртуальной машины
08-17 16:20:54.812 7570-7570/com.myfonetv.a3wc.myfonetv E/AndroidRuntime: фатальное исключение: main
Процесс: com.myfonetv.a3wc.myfonetv, PID: 7570
Ява.яз.NullPointerException: попытка вызвать виртуальный метод 'int java.lang.Строка.length()' для нулевой ссылки на объект
at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
в org.json.JSONTokener.nextValue(JSONTokener.java:94)
в org.json.JSONObject.<init>(JSONObject.java:156)
в org.json.JSONObject.<init>(JSONObject.java:173)
на com.myfonetv.a3wc.myfonetv.activities.Register$6.onPostExecute(Register.java:582)
на com.myfonetv.a3wc.myfonetv.activities.Register$6.onPostExecute(Register.java:516)
на android.os.AsyncTask.finish(AsyncTask.java:636)
на android.os.AsyncTask.access$500(AsyncTask.java:177)
на android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
в android.os.Handler.dispatchMessage(Handler.java:111)
на Андроид.ОС."Петля времени".петли(лупер.Ява:194)
в android.app.ActivityThread.main(ActivityThread.java:5643)
в java.lang.reflect.Method.invoke(собственный метод)
в java.lang.reflect.Method.invoke(Method.java:372)
в com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Смотрите мой код ниже
case R.id.register: if (ConnectionHelper.isConnectingToInternet(Register.this)) { Boolean flag = false; final String str_email, str_pass, str_fName, str_phone; str_email = edt_email.getText().toString(); str_pass = edt_password.getText().toString(); str_fName = edt_f_name.getText().toString(); str_phone = edt_phone.getText().toString(); if ((!isValidEmail(str_email))) { assert lay_email != null; lay_email.setError("Required Field"); flag = true; } if (str_pass.length() <= 0) { lay_password.setError("Required Field"); flag = true; } if (str_fName.length() <= 0) { lay_f_name.setError("Required Field"); flag = true; } if (str_phone.length() <= 0) { lay_phone.setError("Required Field"); flag = true; } if (!flag) { //The below line is 517 new AsyncTask<String, Void, String>() { @Override protected void onPreExecute() { super.onPreExecute(); progressBar.setVisibility(View.VISIBLE); } @SuppressWarnings("deprecation") @Override protected String doInBackground(String... params) { try { HttpClient client = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(URLUtils.register); entityBuilder = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); if (path.isEmpty()) { Log.e("doInBack_file", ": is empty"); } else { File file = new File(path); Log.e("doInBack_file", "" + file); entityBuilder.addPart("picture", new FileBody(file, "application/octet")); Log.d("EDIT USER PROFILE", "UPLOAD: file length = " + file.length()); Log.d("EDIT USER PROFILE", "UPLOAD: file exist = " + file.exists()); } entityBuilder.addPart("device_token", new StringBody(SingleTon.getInstance().GCMKey)); entityBuilder.addPart("device_type", new StringBody("android")); entityBuilder.addPart("login_by", new StringBody("manual")); entityBuilder.addPart("name", new StringBody(str_fName)); entityBuilder.addPart("email", new StringBody(str_email)); entityBuilder.addPart("mobile", new StringBody(str_phone)); entityBuilder.addPart("password", new StringBody(str_pass)); entityBuilder.addPart("social_unique_id", new StringBody("")); httpPost.setEntity(entityBuilder); //The below line is 556 HttpResponse response = client.execute(httpPost); HttpEntity httpEntity = response.getEntity(); if (response.getStatusLine().getStatusCode() == 200) { String res = EntityUtils.toString(httpEntity); Log.e("res", "" + res); return res; } else { return "Error"; } } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); progressBar.setVisibility(View.GONE); //Line 579 has been edited if (s != null && s.equalsIgnoreCase("Error")) { UIUtils.showToast(Register.this, R.string.con_timeout); } else { try { //The below line is 584 JSONObject object = new JSONObject(s); Log.i("object", "" + object); String result = object.optString("success"); if (result.equalsIgnoreCase("true")) { Intent mainIntent = new Intent(Register.this, MainActivity.class); mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); SharedPref.putKey(Register.this, "login_type", "Logged"); SharedPref.putKey(Register.this, "ID", object.optString("id")); SharedPref.putKey(Register.this, "PROFILE_IMAGE", object.optString("picture")); SharedPref.putKey(Register.this, "NAME", object.optString("name")); SharedPref.putKey(Register.this, "TOKEN", object.optString("token")); SharedPref.putKey(Register.this, "SUB_PROFILE", object.optString("sub_profile_id")); SharedPref.putKey(Register.this, "user_type", object.optString("user_type")); SharedPref.putKey(Register.this, "push_status", object.optString("push_status")); SharedPref.putKey(Register.this, "CHANGE_PASS", ""); startActivity(mainIntent); Register.this.finish(); } else { UIUtils.showToastMsg(Register.this, object.optString("error_messages")); } } catch (JSONException e) { e.printStackTrace(); } } } }.execute(); } } else { UIUtils.showNetworkAlert(Register.this, R.string.check_network); } break; }
Пожалуйста помогите
Что я уже пробовал:
У меня есть google stackoverflow для решения, но безрезультатно