1

I'm trying to insert values into mysql database using below code. For some reason the app crashes everytime I press the submit button. I added Internet permission too. I am unable to understand the logcat. Any help will be appreciated.

MainActivity.java

package com.example.testthis;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    EditText et;
    Button btn;
    String mname;
    String query;
    String mylink;
    Activity context;
    ProgressDialog pd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et = (EditText)findViewById(R.id.editText1);
        btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                mname = et.getText().toString();

                try {

                    query = URLEncoder.encode(mname,"utf-8");

                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                mylink = "http://necrecords.16mb.com/donordetails.php?fn="+query;

                HttpAsyncTask hat = new HttpAsyncTask();

                hat.execute(mylink);

            }



        });


    }

    private class HttpAsyncTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... urls) {

            return httpRequestResponse(urls[0]);
        }

        protected void onPreExecute(){
            super.onPreExecute();
            pd = new ProgressDialog(context);
            pd.setTitle("Reporting query");
            pd.setMessage("Please wait..");
            pd.setCancelable(true);
            pd.setIndeterminate(true);
            pd.show();

        }
        // onPostExecute displays the results of the AsyncTask.
        @Override
        protected void onPostExecute(String result) {

            if(pd!=null) pd.dismiss();
            Toast.makeText(getApplicationContext(),"Successfully Sent!", Toast.LENGTH_LONG).show();

        }
    }

//For HttpAsync Functions: sending requests and receiving responses
    public static String httpRequestResponse(String url){
        InputStream inputStream = null;
        String result = "";
        try {
            // create HttpClient
            HttpClient httpclient = new DefaultHttpClient();

            // make GET request to the given URL
            HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

            // receive response as inputStream
            inputStream = httpResponse.getEntity().getContent();

            // convert InputStream to string
            if(inputStream != null)
                result = convertInputStreamToString(inputStream);
            else
                result = "InputStream did not work";


        } catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());
        }

        return result;
    }

private static String convertInputStreamToString(InputStream inputStream) throws IOException{
    BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
    String line = "";
    String result = "";
    while((line = bufferedReader.readLine()) != null)
        result += line;

    inputStream.close();
    return result;
}


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Logcat

04-21 19:34:24.582: I/dalvikvm(5064): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
    04-21 19:34:24.582: W/dalvikvm(5064): VFY: unable to resolve virtual method 12215: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
    04-21 19:34:24.582: D/dalvikvm(5064): VFY: replacing opcode 0x6f at 0x0000
    04-21 19:34:24.582: I/dalvikvm(5064): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
    04-21 19:34:24.582: W/dalvikvm(5064): VFY: unable to resolve virtual method 12221: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
    04-21 19:34:24.582: D/dalvikvm(5064): VFY: replacing opcode 0x6f at 0x0000
    04-21 19:34:24.582: I/dalvikvm(5064): Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
    04-21 19:34:24.582: W/dalvikvm(5064): VFY: unable to resolve virtual method 9785: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
    04-21 19:34:24.582: D/dalvikvm(5064): VFY: replacing opcode 0x6e at 0x000e
    04-21 19:34:24.592: I/dalvikvm(5064): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
    04-21 19:34:24.592: W/dalvikvm(5064): VFY: unable to resolve virtual method 399: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
    04-21 19:34:24.592: D/dalvikvm(5064): VFY: replacing opcode 0x6e at 0x0002
    04-21 19:34:24.592: I/dalvikvm(5064): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
    04-21 19:34:24.592: W/dalvikvm(5064): VFY: unable to resolve virtual method 421: Landroid/content/res/TypedArray;.getType (I)I
    04-21 19:34:24.592: D/dalvikvm(5064): VFY: replacing opcode 0x6e at 0x0002
    04-21 19:34:24.592: I/dalvikvm(5064): Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
    04-21 19:34:24.592: W/dalvikvm(5064): VFY: unable to resolve virtual method 362: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
    04-21 19:34:24.592: D/dalvikvm(5064): VFY: replacing opcode 0x6e at 0x0002
    04-21 19:34:24.592: I/dalvikvm(5064): Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
    04-21 19:34:24.592: W/dalvikvm(5064): VFY: unable to resolve virtual method 364: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
    04-21 19:34:24.592: D/dalvikvm(5064): VFY: replacing opcode 0x6e at 0x0002
    04-21 19:34:24.622: D/TextView(5064): Constructor - Got Res id for appearance for textColorPrimaryInverse
    04-21 19:34:24.622: W/ResourceType(5064): Skipping entry 0x7f070035 in package table 0 because it is not complex!
    04-21 19:34:24.622: D/TextView(5064): Constructor - Got appearance for textColorPrimaryInverse
    04-21 19:34:24.622: D/TextView(5064): Constructor -- Got mEditTextBackgroundColor
    04-21 19:34:24.702: E/IMGSRV(5064): :0: PVRDRMOpen: TP3, ret = 46
    04-21 19:34:24.712: E/IMGSRV(5064): :0: PVRDRMOpen: TP3, ret = 49
    04-21 19:34:24.712: E/IMGSRV(5064): :0: PVRDRMOpen: TP3, ret = 50
    04-21 19:34:24.712: E/IMGSRV(5064): :0: PVRDRMOpen: TP3, ret = 50
    04-21 19:34:24.712: E/IMGSRV(5064): :0: PVRDRMOpen: TP3, ret = 50
    04-21 19:34:24.722: E/IMGSRV(5064): :0: PVRDRMOpen: TP3, ret = 52
    04-21 19:34:24.762: D/OpenGLRenderer(5064): Enabling debug mode 0
    04-21 19:34:30.742: D/AndroidRuntime(5064): Shutting down VM
    04-21 19:34:30.742: W/dalvikvm(5064): threadid=1: thread exiting with uncaught exception (group=0x430ef140)
    04-21 19:34:30.742: E/AndroidRuntime(5064): FATAL EXCEPTION: main
    04-21 19:34:30.742: E/AndroidRuntime(5064): Process: com.example.testthis, PID: 5064
    04-21 19:34:30.742: E/AndroidRuntime(5064): java.lang.NullPointerException
    04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
    04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.app.AlertDialog.<init>(AlertDialog.java:98)
    04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
    04-21 19:34:30.742: E/AndroidRuntime(5064):     at com.example.testthis.MainActivity$HttpAsyncTask.onPreExecute(MainActivity.java:88)
    04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
    04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.os.AsyncTask.execute(AsyncTask.java:535)
    04-21 19:34:30.742: E/AndroidRuntime(5064):     at com.example.testthis.MainActivity$1.onClick(MainActivity.java:67)
    04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.view.View.performClick(View.java:4478)
    04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.view.View$PerformClick.run(View.java:18698)
    04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.os.Handler.handleCallback(Handler.java:733)
    04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.os.Handler.dispatchMessage(Handler.java:95)
    04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.os.Looper.loop(Looper.java:149)
    04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.app.ActivityThread.main(ActivityThread.java:5257)
    04-21 19:34:30.742: E/AndroidRuntime(5064):     at java.lang.reflect.Method.invokeNative(Native Method)
    04-21 19:34:30.742: E/AndroidRuntime(5064):     at java.lang.reflect.Method.invoke(Method.java:515)
    04-21 19:34:30.742: E/AndroidRuntime(5064):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    04-21 19:34:30.742: E/AndroidRuntime(5064):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
    04-21 19:34:30.742: E/AndroidRuntime(5064):     at dalvik.system.NativeStart.main(Native Method)

1 Answer 1

1

Line 37:

Activity context;

Then you never initialize this variable and on line 88 you get a null pointer exception:

pd = new ProgressDialog(context);

That's what the log tells you: NullPointerException.

EDIT: Just to clarify for Popsta. Here is the fragment from the logcat that helped me figure out what's going on (just at the very end):

04-21 19:34:30.742: E/AndroidRuntime(5064): java.lang.NullPointerException
04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.app.AlertDialog.<init>(AlertDialog.java:98)
04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
04-21 19:34:30.742: E/AndroidRuntime(5064):     at com.example.testthis.MainActivity$HttpAsyncTask.onPreExecute(MainActivity.java:88)
04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.os.AsyncTask.execute(AsyncTask.java:535)
04-21 19:34:30.742: E/AndroidRuntime(5064):     at com.example.testthis.MainActivity$1.onClick(MainActivity.java:67)
04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.view.View.performClick(View.java:4478)
04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.view.View$PerformClick.run(View.java:18698)
04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.os.Handler.handleCallback(Handler.java:733)
04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.os.Looper.loop(Looper.java:149)
04-21 19:34:30.742: E/AndroidRuntime(5064):     at android.app.ActivityThread.main(ActivityThread.java:5257)
04-21 19:34:30.742: E/AndroidRuntime(5064):     at java.lang.reflect.Method.invokeNative(Native Method)
04-21 19:34:30.742: E/AndroidRuntime(5064):     at java.lang.reflect.Method.invoke(Method.java:515)
04-21 19:34:30.742: E/AndroidRuntime(5064):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-21 19:34:30.742: E/AndroidRuntime(5064):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
04-21 19:34:30.742: E/AndroidRuntime(5064):     at dalvik.system.NativeStart.main(Native Method)
Sign up to request clarification or add additional context in comments.

4 Comments

Sir you answer solved my problem. Does the logcat particularly shows that the NullpointerException is due to context?
Yes, it does. Ignore the text in the beginning - it's just a timestamp and some extra info. Read the things after that. Whenever you have issues, just look there for exceptions and you will figure it out. I edited my answer to be clearer for you.
It doesn't clearly say it's due to the context. It says which line and file it occurred on (at com.example.testthis.MainActivity$HttpAsyncTask.onPreExecute(MainActivity.java:88)). I just looked at what you're doing on the line.
The existence of that variable is highly suspect anyway. An inner class could typically use MainActivity.this where it needs a reference to the instance of the containing Activity.

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.