Click here to Skip to main content
15,945,603 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi android enthusiast
I am trying to build a login/registration app but the below code has been posing a challenge which is the async task,
whenever i test it on my device pressing the register button it always crash and shows the below error in the logcat

Logcat error below

780 7570-7676/com.myfonetv.a3wc.myfonetv D/libc-netbsd: getaddrinfo: staging.streamhash.info NO result from proxy
08-17 16:20:54.784 7570-7676/com.myfonetv.a3wc.myfonetv I/System.out: [CDS][DNS]Unable to resolve host "staging.streamhash.info": No address associated with hostname
08-17 16:20:54.784 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: java.net.UnknownHostException: Unable to resolve host "staging.streamhash.info": No address associated with hostname
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.openConnection(DefaultClientConnectionOperator.java: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: Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
08-17 16:20:54.794 7570-7676/com.myfonetv.a3wc.myfonetv W/System.err: at libcore.io.Posix.android_getaddrinfo(Native Method)
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 more
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: Shutting down VM
08-17 16:20:54.812 7570-7570/com.myfonetv.a3wc.myfonetv E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myfonetv.a3wc.myfonetv, PID: 7570
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
at org.json.JSONTokener.nextValue(JSONTokener.java:94)
at org.json.JSONObject.<init>(JSONObject.java:156)
at org.json.JSONObject.<init>(JSONObject.java:173)
at com.myfonetv.a3wc.myfonetv.activities.Register$6.onPostExecute(Register.java:582)
at com.myfonetv.a3wc.myfonetv.activities.Register$6.onPostExecute(Register.java:516)
at android.os.AsyncTask.finish(AsyncTask.java:636)
at android.os.AsyncTask.access$500(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5643)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

See my code below

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;
        }


Kindly help

What I have tried:

I have google stackoverflow for solution but to no avail
Posted
Updated 17-Aug-18 22:03pm

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
Member 10627743 19-Aug-18 18:49pm    
Thank you originalgriff
OriginalGriff 20-Aug-18 2:07am    
You're welcome!
Member 14645741 5-Nov-19 15:00pm    
best explanation that I ever came through.
This is the issue you need to fix first:
08-17 16:20:54.784 7570-7676/com.myfonetv.a3wc.myfonetv I/System.out: [CDS][DNS]Unable to resolve host "staging.streamhash.info": No address associated with hostname
 
Share this answer
 
Comments
Member 10627743 19-Aug-18 18:49pm    
Thanks Richard

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900